Microsoft SDK for Java

Calling Java Methods

The most common method for calling a Java API from native code is execute_java_dynamic_method (see also execute_java_dynamic_method64 and execute_java_dynamic_methodV). The following example shows how to call the move method on a Rectangle object:

    execute_java_dynamic_method(NULL, phRectangle, "move", "(II)V", x, y);

The first parameter must be NULL, the second parameter is the object whose method you want to call, the third parameter is the method name, and the fourth parameter is the method signature.

Pass the parameters. The signature specifies the parameter types, surrounded by parentheses, followed by the return type. In this case, "(II)V" translates to "param 1 is an integer, parameter 2 is an integer, and the return type is void."

The following is a table of how each type maps to its signature character.

Parameter

Signature Char

array [
boolean Z
byte B
char C
double D
float F
int I
long J
object L
short S
void V

For arrays, the signature char is followed by the signature char of the type of the array. For example, an array of bytes would be "[B".

For objects, the signature is followed by the object class name and ends with a semicolon(;). For example, the signature for a Rectangle is "Ljava/awt/Rectangle;".

To call a static method, you can use execute_java_static_method (execute_java_static_method64 or execute_java_static_methodV). For example, to call System.gc( ), write the following:

    ClassClass* pcc = FindClass(NULL, "java/lang/System", FALSE);
    execute_java_static_method(NULL, pcc, "gc", "()V");

FindClass( ) is used to get a pointer to the class object. The first parameter should be NULL, the second is the class name, and the third should be FALSE. You can call execute_java_static_method with the returned class pointer. The first parameter should be NULL, the second is the class pointer, the third is the method name, the fourth is the method signature. Finally, call any arguments (in this case, there aren't any).

Repeated Calls

For performing repeated calls to the same method of a particular object, you can use the more low-level do_execute_java_method API to avoid repeated name lookups. The following is an example of moving a rectangle 10 times:

    struct methodblock *pmb = get_methodblock(phRectangle, "move", "(II)V");

    for (i = 0; i < 10; i++)
    {
        do_execute_java_method(NULL, phRectangle, NULL, NULL, pmb, FALSE, x, y);
    }

To get a pointer to a cached data structure that represents the object and the method, use get_methodblock. The first parameter is the object pointer, the second is the method name, and the third is the method signature. You can call do_execute_java_method with the returned methodblock pointer. The first parameter should be NULL, the second is the object pointer, the third and fourth parameters should also be NULL, the fifth is the methodblock pointer, the sixth should be TRUE for a static call and FALSE for a non-static call. Finally, pass any arguments.

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