Link to home
Start Free TrialLog in
Avatar of Miknosoft
Miknosoft

asked on

Problem with 2nd call to JNI method CreateJavaVM

Hi all,
I have written a little MS Win32 DLL that makes use of some Java API I've packed into a jar using the JNI mechanism. I've released this DLL to a customer who has the requirement due to requests from other parts of his program to load and unload my DLL for each call. He's doing something like this:

LoadLibrary(...);
GetProcAddress(...);
(*proc)(...); // call my DLL method which will forward the request to a Java VM
FreeLibrary(...);

In my DLL the public Java VM to use is searched in the registry etc. Then a call to the JNI method CreateJavaVM() is made. This call returns the address of the Java VM to use, some environment structures and sets the classpath extension in order to find my jar. Then the Java class and method are searched and finally called. After that I call DetachCurrentThread() (works without) and DestroyJavaVM().

This all works fine for the first time. But when executed the seconde time the call to CreateJavaVM() returns with JNI_ERR (-1).  Loading the DLL once, calling the Java method as often as desired and then freeing the DLL once works fine. But doing this the 2nd time leads to the same error.

Does anybody know what to do in order to enable my DLL to be loaded and freed as often as desired and still get the VM created successfully? This would be of great help for me. Any suggestions are welcome.

Thanx in advance
Miknosoft

Avatar of petmagdy
petmagdy
Flag of Canada image

Hi Miknosoft,

I think u don't need to call this function twice, DLLs are loaded only once

Cheers!
Avatar of Miknosoft
Miknosoft

ASKER

Hi petmagdy,
as I've written I cannot influence the way my DLL is processed. To do it as described is a request from the customer. Would be too much work to change the current behaviour of his application.
Thankx anyway,
Miknosoft
ASKER CERTIFIED SOLUTION
Avatar of petmagdy
petmagdy
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi petmagdy,

your hint doesn't work because static areas are reset when the DLL is unloaded. Thus, the flag gets lost. Anyway, your hint was valuable. Now I use a little trick to avoid the unloading of my DLL: I load it once by myself and never unload it. So my static area stays intact and I don't have to load and unload the Java VM all the time, which after all, should lead to better response time, besides the fact, that it doesn't lead to the expected results.

Though you didn't really give me the solution, I'll accept your answer!

Thanks for your interest in my question.

Miknosoft