This week's book giveaway is in the Java in General forum. We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line! See this thread for details.
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
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.