AttachCurrentThread not working w/ Windows services

I have a Windows service that calls a C++ DLL which invokes a Java class through JNI. With this configuration (running as a service), AttachCurrentThread crashes. But, if I run the app as a standalone client, everything is well.

Is there anything about services that makes attaching the current thread not work?

So, my C++ DLL (which is called by the service) makes a call to initialize JVM. Then the call is made to convert( ) where AttachCurrentThread( ) seems to fail. The reason why I know it's AttachCurrentThread that is failing is because I put print statements all around it and it never prints anything after the call.

Here is caller's pseudocode:

For iNum = 0 to x

'Successful initialization will return 0

If Init_JVM() = 0 then

Convert()

End If

Next

So, the first time it initializes the JVM, it's ok because it creates a new instance. The second time, it uses GetCreatedJavaVMs to get the instance loaded. When it attaches the JVM here, is where it crashes.

Here's the C++ DLL that invokes the Java class:

// global jvm

static JavaVM *jvm = NULL;

extern"C" JNIEXPORT jint JNICALL Init_JVM( )

{

JNIEnv *env;

JavaVM *vms = NULL;

JavaVMInitArgs vm_args;

JavaVMOption options[1];

char *pszClasspath =newchar[1024];

jint res = 0;

jsize buf_len = 1;

jsize vmsCount = 0;

// builds the classpath string

pszClasspath = build_Classpath();

if (pszClasspath !="")

{

// arguments

options[0].optionString = pszClasspath;

vm_args.version = JNI_VERSION_1_4;

vm_args.options = options;

vm_args.nOptions = 1;

vm_args.ignoreUnrecognized = JNI_TRUE;

// see if there are loaded VMs. If so, use it instead of creating a new one

res = JNI_GetCreatedJavaVMs(&vms, 1, &vmsCount);

// if no errors and 0 VMs are loaded

if(res == 0 && vmsCount == 0)

{

// Create the Java VM

res = JNI_CreateJavaVM(&jvm, (void **)&env, &vm_args);

}

return res;

}

else

{

// Return a non-0. Caller will handle error.

return -1;

}

}

extern"C" JNIEXPORT BSTR JNICALL Convert( )

{

JNIEnv *env;

BSTR bstrRet;

// ********************* THIS IS WHERE IS CRASHES *********************

// Retrieve the env value from the global jvm

if (jvm->AttachCurrentThread((void **)&env, NULL) < 0)

{

// check to see if an exception was thrown

bstrRet = CheckJNIException(env);

}

else

{

// work to invoke Java class

}

return bstrRet;

}

[4007 byte] By [yatingga] at [2007-10-3 2:04:12]
# 1
env is an input to AttachCurrentThread not an output.Right now you have garbage in it.
jschella at 2007-7-14 19:03:00 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2
If the syntax is incorrect, how come it works when I run this code in a standalone app, but not in a Windows service?
yatingga at 2007-7-14 19:03:00 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 3

> If the syntax is incorrect, how come it works when I

> run this code in a standalone app, but not in a

> Windows service?

If you think that your code is correct than continue using it.

Alternatively you could recognize that although your env is undefined it still has something in it. And if that something happens to be a correct env then the code works.

jschella at 2007-7-14 19:03:00 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 4
Yes, the code works in a standalone app, but it won't for the Windows service. That's what I've been wanting to know, why not in a Windows service environment?
yatingga at 2007-7-14 19:03:00 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 5
Hi,I had some problems with generic service wrapper from Microsoft, since I use Java Service Wrapper from http://wrapper.tanukisoftware.org/doc/english/introduction.html those problems went away...--Marc ( http://jnative.sf.net)
mdentya at 2007-7-14 19:03:01 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 6
Marc, which method did you use?
yatingga at 2007-7-14 19:03:01 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 7
Hi,I use Method 3, it's the best (see http://wrapper.tanukisoftware.org/doc/english/integrate.html)--Marc ( http://jnative.sf.net)
mdentya at 2007-7-14 19:03:01 > top of Java-index,Java HotSpot Virtual Machine,Specifications...