Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6801209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:04:19+00:00 2026-05-26T19:04:19+00:00

I’m creating a windows service program which invokes a java program. Here is part

  • 0

I’m creating a windows service program which invokes a java program.

Here is part of the code, hModule is a global variable and LoadLibrary is called in ServiceStart after which it invoked invokeJVM. I manage to start the service and it runs fine, however, whenever I stop the service, it gives me an error:
Windows could not stop the service on the local computer
Error 1067: The windows service terminated unexpectedly

After adding additional logging, I found that the place where the unexpected termination error occurs is the return of the invokeJVM function.
When I check the event viewer it gives me some BEX error, which on googling, says that it is a stack overflow error, but I could not determine the cause of it, any idea why?

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:04:19+00:00Added an answer on May 26, 2026 at 7:04 pm

    You might be using the wrong calling convention. What happens if you try something like this:

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

    P.S.
    If it’s not stdcall, find out which one is it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have thousands of HTML files to process using Groovy/Java and I need to
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.