[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 524
  • Last Modified:

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

0
Miknosoft
Asked:
Miknosoft
  • 2
  • 2
1 Solution
 
petmagdyCommented:
Hi Miknosoft,

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

Cheers!
0
 
MiknosoftAuthor Commented:
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
0
 
petmagdyCommented:
Miknosoft,

Try this, in ur dll put a static flag that indicates that CreateJavaVM is already called so it will not be called again, is this fisible to you?

Cheers!
0
 
MiknosoftAuthor Commented:
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
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now