Coinitialize ex is failing inside a WMI call in a JNI program stub

1

Environment:

I have a JNI program which is made of the following pieces

  1. C++ : Here I make use of WMI to return the running process details. The program is similar to the example in http://msdn.microsoft.com/en-us/library/aa390423(v=vs.85).aspx I have encompassed the entire code in the example with a JNI wrapper (One of the parameters I send to the program is of type Integer). Below is the signature of that function
    JNIEXPORT jdouble JNICALL Java_handlemonitor_NativeSytem_getSystemDetails(JNIEnv *env, jclass cls, jint pId)

The generated DLL is kept in the bin folder inside JDK directory

  1. Java : Here I just call the function inside the dll and consume the returned value. I also generate the .h file using the javah program and I have included it in the C++ program.

Issue: When I run the program, I get an error msg in the C++ program that CoInitializeEx failed with the error code 0x80010106.

following is the line where the program fails.

hres = CoInitializeEx(0,COINIT_MULTITHREADED).

I tried increasing the heapSize still it continues to fail.

Any particular reason why CoInitializeEx is failing?

Thanks in advance

java-native-interface
wmi
asked on Stack Overflow Jun 2, 2011 by user781085

1 Answer

3

The error 0x80010106 means "HRESULT - 0x80010106 - Cannot change thread mode after it is set."

The problem is basically that a thread can Initialize its COM mode (and apartment type, STA/MTA) only once. Once it is set, you will get this error if you attempt to initialize again with a different value.

Often what happens with this error is that some other 3rd-party code will cause COM to become initialized before your code is executed. If this is the case, then you have a couple of choices:

1) Try to find a way to have your code execute first, so you can set the COM apartment type yourself. This can be tricky, and might cause side effects if the 3rd-party code expects an STA

2) Put your code on its own thread, where you can explicitly set the apartment type

If you actually don't care about the apartment type, then you can just change the call to CoInitializeEx to use COINIT_APARTMENTTHREADED (STA) instead of COINIT_MULTITHREADED (MTA).

Hope that helps,

John

answered on Stack Overflow Jun 2, 2011 by JohnD

User contributions licensed under CC BY-SA 3.0