• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Persistence in JNI

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow - this is a tough one. I will move it to Advanced and see if it gets more exposure.
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
A day job? In an office? My worst nightmare! Comfort me tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic