• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Persistence in JNI

 
Will Ross
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All, I have a question about JNI that I'm hoping someone can shed some light on.
Background: I'm writing a small Java program that interacts with Windows COM objects. The program invokes native methods in a .dll (that I wrote), which then uses MFC to interact with a COM object.
I have successfully written native methods that can create a COM object (given its CLSID), invoke a method on the COM object, and destroy the COM object.
I would like to keep a handle to a COM object in the native code. So when the method that creates a COM object is called, it obtains a pointer to an IDispatch interface and keeps it. When another method is called later to invoke a method on the COM object, the method is invoked on the instance that was created previously.
My Problem: The handle that is maintained in the native code seems to be global to the process (i.e. the VM). I can't figure out how to associate the pointer to the IDispatch interface with a specific instance of my Java object that is calling the native methods.

If any of that description is unclear or if you need more information, let me know! I'm new to JNI programming, so I might have left out information unintentionally.
Thanks in advance,
Will
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow - this is a tough one. I will move it to Advanced and see if it gets more exposure.
 
Joel McNary
Bartender
Posts: 1824
Eclipse IDE Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I am understanding your problem right, you want to create handles to different IDispatch instances and use the appropriate one depending on the java object invoking the method. Is this accurate?
The way I would solve this would be to maintain the handle in the Java side of the code and pass it to the native code when invoking the method; this way, you are sure that the appropriate handle is being used.
Remember, a handle is essentially nothing more than a pointer to a pointer. As such, you can pass it back to the Java side as a long. You code might look like:

If you are using C++ as your native-language, I recomend looking into cxxwrap, a free utility that will take you C++ classes and generate the appropriate Java classes to use your native C++ code. At the very least, looking at how it handles linking the Java-side instance of an object to the corresponding native-side instance should clarify the approach I outlined above.
Hope this helps.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic