File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Persistence in JNI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Persistence in JNI" Watch "Persistence in JNI" New topic

Persistence in JNI

Will Ross

Joined: Apr 10, 2003
Posts: 4
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,
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
Wow - this is a tough one. I will move it to Advanced and see if it gets more exposure.

"JavaRanch, where the deer and the Certified play" - David O'Meara
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

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.

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
I agree. Here's the link:
subject: Persistence in JNI
It's not a secret anymore!