how to create object of a C++ class in my servlet ?
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Manojaba_Banerjee
Posted On:   Wednesday, December 12, 2001 05:05 AM

i am facing a problem regarding call functions from C++ files.
Actually am developing a SMS sending application using java servlets & Nokia datasuite 3.0 software. the API of this software is in C++.
Now i need to create an object of some classes of datasuite & call some functions.
One more proble mis there, the datasuite API is in the form of .dll files, not as .exe or .jar. so, i can't set those values in classpath also.

Can neone help me out ?
thanx in advance..

Re: how to create object of a C++ class in my servlet ?

Posted By:   Christopher_Schultz  
Posted On:   Wednesday, December 12, 2001 07:57 AM

Oh, one more thing: Some SMS servers are capable of translating between JMS messages (which already has a Java API) and SMS messages. I think this would be an ideal solution if you are able to use it.



-chris

Re: how to create object of a C++ class in my servlet ?

Posted By:   Christopher_Schultz  
Posted On:   Wednesday, December 12, 2001 07:53 AM

Ugh.



This gets ugly, since you're not just calling discrete API functions from a Java wrapper (which is the case most of the time with JNI).



The short answer is: if you don't know how to do what you're trying to do, you probably shouldn't be doing it.



That being said...



What you'll have to do is create a series of native methods, each mirroring the methods in your C++ class. In addition, you'll need to have a 'createObjectOnHeap' method and a 'disposeObjectOnHeap' method.



In the constructor of your Java object, you'll have to call your 'createObjectOnHeap' function to actually instantiate your C++ object.



After you are done with your Java object, take care to call a 'dispose' method on it, which will call the 'disposeObjectOnHeap' function and destroy your C++ object.



Having never done this, I don't know where you'll keep the pointer to the C++ object between method calls... that's up to you.



Each member method in your Java class will either call or itself be a native method, which will simply make the appropriate call to your C++ object:



Java:

public native void doSomething();



C:


void JNICall (etc...)
doSomething(....)
{
myObjPtr->doSomething();
}



Doing all this is likely to get you very deep into trouble, and you may not be able to instantiate more than one of these things at a time, depending on the way you implement this.



My advise is to either re-write the C++ class in Java, or provide another way to manupulate these object (say, using a C++-based server process to which you can connect via a socket or HTTP-style request).



Good luck,

-chris
About | Sitemap | Contact