Microsoft SDK for Java

PrepareThreadForJava

Thread entry function for calls into the virtual machine.

Syntax

BOOL __cdecl PrepareThreadForJava(PVOID pThreadEntryFrame);

Return Value

Returns TRUE if successful; otherwise, returns FALSE.

Parameters

pThreadEntryFrame An instance of a thread entry frame. This pointer is maintained by the Microsoft VM and the same pointer should be used in UnprepareThreadForJava when exiting the Microsoft VM.

Remarks

This method prepares a Java thread for entering the Microsoft VM so that calls can be safely made from the thread to native methods.

Allocate ThreadEntryFrames on the associated thread's stack rather than in the heap. The Microsoft virtual machine treats the ThreadEntryFrame as a stack frame on the associated thread's stack. All other stack frames are internally allocated on the associated thread's stack. Some code assumes that stack frame addresses are in monotonically increasing order. Linking in a ThreadEntryFrame that violates that assumption may cause problems. The ThreadEntryFrame should be a local variable in the method that calls PrepareThreadForJava.

Note that garbage collection is disabled between PrepareThreadforJava and UnprepareThreadForJava. An application can hang if each thread waits until it is about to exit before calling UnprepareThreadforJava.

The following code sample demonstrates the use of invocation APIs.

//  This code demonstrates the use of the PrepareThreadForJava and 
//  UnprepareThreadForJava invocation APIs.


#include <windows.h>
#include <stdio.h>
#include <native.h>

void main(int argc, char *argv[])
{
    ThreadEntryFrame threadEntryFrame;
    ClassClass* pClass;

    //  Call this API before calling into the Microsoft VM to allow it
    //  to allocate any per-thread structures and to do any first-time
    //  initialization. After return from this call, Java objects may 
    // be accessed or RNI APIs may be called.
    if (PrepareThreadForJava(&threadEntryFrame)) {

        pClass = FindClass(NULL, "TestClass", TRUE);

        if (pClass != NULL) {
            execute_java_static_method(NULL, pClass, "someMethod", "()V");
        } else {
            printf("Failed to find class!\n");
        }

        //  Detaches the "entry frame" from this thread. After return 
        //  from this call, it is no longer safe to directly touch Java 
        //  objects or call RNI APIs.
        UnprepareThreadForJava(&threadEntryFrame);

    } else {
        printf("Failed to initialize thread!\n");
    }
}

© 1999 Microsoft Corporation. All rights reserved. Terms of use.