gpt4 book ai didi

java - 使用 Windows 服务调用使用 JNI 和 LoadLibrary 的 Java 程序的问题

转载 作者:可可西里 更新时间:2023-11-01 10:28:07 25 4
gpt4 key购买 nike

我正在创建一个调用 java 程序的 Windows 服务程序。

这是部分代码,hModule是一个全局变量,在ServiceStart中调用了LoadLibrary,之后调用了invokeJVM。我设法启动了该服务并且它运行良好,但是,每当我停止该服务时,它都会给我一个错误:Windows 无法停止本地计算机上的服务错误 1067:Windows 服务意外终止

添加了额外的日志记录后,发现意外终止错误发生的地方是invokeJVM函数的返回。当我检查事件查看器时,它给了我一些 BEX 错误,谷歌搜索表明这是一个堆栈溢出错误,但我无法确定它的原因,知道为什么吗?

HMODULE hModule;

VOID ServiceStart ( DWORD dwArgc, LPTSTR *lpszArgv )
{
// Let the service control manager know that the service is
// initializing.
if ( !ReportStatus( SERVICE_START_PENDING, NO_ERROR, 3000 ) )
//goto cleanup;
return;

hModule = LoadLibrary( TEXT( "C:\\Program Files (x86)\\Java\\jre6\\bin\\client\\jvm.dll" ) );

// Create a Stop Event
if ( !( hServerStopEvent = CreateEvent( NULL, TRUE, FALSE, NULL) ) )
goto cleanup;

lpszJavaArgs = getJavaArgs( &lpszJavaArgs, &dwJLen, dwArgc, lpszArgv );
lpszAppArgs = getAppArgs( &lpszAppArgs, &dwALen, dwArgc, lpszArgv );
wrkdir = getWorkingDirectory( dwArgc, lpszArgv );

if ( !ReportStatus( SERVICE_RUNNING, NO_ERROR, 0 ) )
goto cleanup;

// After the initialization is complete (we've checked for arguments) and
// the service control manager has been told the service is running, invoke
// the Java application. If clients are unable to access
// the server, check the event log for messages that should indicate any errors
// that may have occured while firing up Java...

invokeJVM( NULL );

// Wait for the stop event to be signalled.
WaitForSingleObject( hServerStopEvent, INFINITE );

cleanup:
( VOID ) ReportStatus( SERVICE_STOPPED, 0, 0 );
if ( hServerStopEvent )
CloseHandle( hServerStopEvent );
( *vm ) -> DestroyJavaVM( vm );
FreeLibraryAndExitThread( hModule, 0 );

return;
}

VOID invokeJVM( VOID *dummy )
{
jint res;
jclass cls;
jmethodID mid;
jstring jstr;
jobjectArray args;
JavaVMInitArgs vm_args;
JavaVMOption options[ MAX_OPTIONS ];
jint ( *createJavaVM )( JavaVM **, void **, void * ) =
( jint ( * )( JavaVM **, void **, void * ) ) GetProcAddress( hModule, "JNI_CreateJavaVM" );
char buf[256];
jclass cls2;
jmethodID mid2;
UINT uIdx;

if ( wrkdir )
{
if ( !SetCurrentDirectory( wrkdir ) )
AddToMessageLog( TEXT( "Unable to change working directory." ) );
}

if(dwJLen > MAX_OPTIONS)
{
AddToMessageLog( TEXT( "Max. number of Java args exceeded." ) );
return;
}

// Assign the arguments for the JVM, such as the classpath,
// RMI codebase, etc.
for ( uIdx = 0; uIdx < dwJLen; ++uIdx )
options[ uIdx ].optionString = lpszJavaArgs[ uIdx ]; // PROBLEM HERE

vm_args.version = JNI_VERSION_1_6;
vm_args.options = options;
vm_args.nOptions = dwJLen;
vm_args.ignoreUnrecognized = TRUE;

//res = JNI_CreateJavaVM( &vm, ( void ** ) &env, &vm_args );
res = ( *createJavaVM )( &vm, ( void ** ) &env, &vm_args );
if ( res < 0 )
{
AddToMessageLog( TEXT( "Cannot create Java VM." ) );
return;
}

// Get the main class
if ( !( cls = ( *env ) -> FindClass( env, SZMAINCLASS ) ) )
{
AddToMessageLog( TEXT( "Cannot find main class." ) );
return;
}

// Get the method ID for the class's main(String[]) function.
if ( !( mid = ( *env ) -> GetStaticMethodID( env, cls, "main", "([Ljava/lang/String;)V" ) ) )
{
AddToMessageLog( TEXT( "Cannot find main method." ) );
return;
}

// If there are arguments, create an ObjectArray sized to contain the
// argument list, and then scan the list, inserting each argument into
// the ObjectArray.
if( dwALen > 0 )
{
if ( !( args = ( *env ) -> NewObjectArray( env, dwALen, ( *env ) -> FindClass( env, "java/lang/String" ), NULL ) ) )
{
AddToMessageLog( TEXT( "Out of Memory!" ) );
return;
}

for( uIdx = 0; uIdx < dwALen; ++uIdx )
{
if ( !( jstr = ( *env ) -> NewStringUTF( env, lpszAppArgs[ uIdx ] ) ) )
{
AddToMessageLog( TEXT( "Out of Memory!" ) );
return;
}
( *env ) -> SetObjectArrayElement( env, args, uIdx, jstr );
}
}
// Otherwise, create an empty array. This is needed to avoid
// creating an overloaded main that takes no arguments in the Java
// app, and then getting a different method ID to the no-argument
// main() method in this invoker code.
else
{
args = ( *env ) -> NewObjectArray( env, 0, ( *env ) -> FindClass( env, "java/lang/String" ), NULL );
}

//Now, get the class of the java SCMEventManager
if ( !( cls2 = ( *env ) -> FindClass( env, SZSCMEVENTMANAGER ) ) )
{
AddToMessageLog( TEXT( "Cannot find SCMEventManager class." ) );
goto finished;
}

//Get the method ID for SCMEventManager.getInstance()
sprintf( buf, "()L%s;", SZSCMEVENTMANAGER );
if ( !( mid2 = ( *env ) -> GetStaticMethodID( env, cls2, "getInstance", buf ) ) )
{
AddToMessageLog( TEXT( "Cannot find SCMEventManager.getInstance." ) );
goto finished;
}

//Call SCMEventManager.getInstance() and save the returned object
//We'll use this later on.

if ( !( jobj = ( *env ) -> NewGlobalRef( env, ( *env ) -> CallStaticObjectMethod( env, cls2, mid2 ) ) ) )
{
AddToMessageLog( TEXT( "Cannot call SCMEventManager.getInstance." ) );
goto finished;
}

finished:
// Run the main class...
( *env ) -> CallStaticVoidMethod( env, cls, mid, args );
SetConsoleCtrlHandler( logoffHandler, TRUE );

return;
}

最佳答案

您可能使用了错误的调用约定。如果你尝试这样的事情会发生什么:

jint (__stdcall *createJavaVM )( JavaVM **, void **, void * ) = ( jint ( __stdcall * )( JavaVM **, void **, void * ) ) GetProcAddress( hModule, "JNI_CreateJavaVM" );

附言如果不是 stdcall,找出它是哪个。

关于java - 使用 Windows 服务调用使用 JNI 和 LoadLibrary 的 Java 程序的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7964411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com