Anyway - I'm using some Java classes via JNI in unmanaged C++, then call the unmanaged C++ class from managed C++.
What I want to be able to do is create an object that uses JNI, then destroy it, then create and use another.
The problem is that the second time around funcCreateJavaVM returns -1 (unknown error). I figure I'm not disposing it properly the first time, but I'm not sure what I'm doing wrong. The call to DestroyJavaVM is in the destructor of the unmanaged class.
The unmanaged class looks something like this:
Expand|Select|Wrap|Line Numbers
- //Path to jvm.dll
- #define JVMDLLPATH "C:\\Program Files\\Java\\jdk1.6.0_04\\jre\\bin\\client\\jvm.dll"
- //Function type for JNI_CreateJavaVM
- typedef UINT (CALLBACK* CreateJavaVM)(JavaVM**,void**,JavaVMInitArgs*);
- class edoc
- {
- JNIEnv *env;
- JavaVM *jvm;
- HINSTANCE jvmdll;
- JavaVMInitArgs vmargs;
- public:
- void init()
- {
- env = (JNIEnv*)malloc(sizeof(JNIEnv));
- jvm = (JavaVM*)malloc(sizeof(JavaVM));
- //Load jvm.dll
- jvmdll = LoadLibrary(JVMDLLPATH);
- //Initialize function variable for JNI_CreateJavaVM
- CreateJavaVM funcCreateJavaVM;
- funcCreateJavaVM = (CreateJavaVM)GetProcAddress(jvmdll,"JNI_CreateJavaVM");
- //Start JVM
- JavaVMOption vmoptions[1];
- vmargs.version=JNI_VERSION_1_6;
- vmargs.nOptions=2;
- vmoptions[0].optionString=VMOPTIONCLASSPATH; //capitalized are macros containing paths
- vmoptions[1].optionString=VMOPTIONPATH;
- vmargs.options = vmoptions;
- vmargs.ignoreUnrecognized = JNI_FALSE;
- funcCreateJavaVM(&jvm, (void**)&env, &vmargs);
- //multiple calls to JVM via env go here - initialising jclass, jmethodID variables and the like
- }
- ~edoc()
- {
- jvm->DestroyJavaVM();
- FreeLibrary(jvmdll);
- }
- };
- The managed class looks something like this:
- public ref class clrEDOC
- {
- edoc* ed;
- public: clrEDOC()
- {
- ed = new edoc();
- ed->init();
- }
- ~clrEDOC()
- {
- ed->~edoc(); //used to be "delete ed;"
- }
- };
- And the code that calls it is as follows (edocjni is the namespace):
- void main()
- {
- edocjni::clrEDOC^ clred = gcnew edocjni::clrEDOC(); //executes properly
- clred->~clrEDOC(); //used to be "delete clred;"
- edocjni::clrEDOC^ ed = gcnew edocjni::clrEDOC(); //unknown error while creating JVM
- }