Microsoft SDK for Java

Object Creation

These sections describe the creation of objects, arrays, and strings.

Basic Objects

Creating Java objects from native code is simple using the execute_java_constructor API. For example, to effectively create a "new Rectangle(0, 0, 10, 10)", write the following:

    HObject *phobj = execute_java_constructor(NULL, "java/awt/Rectangle", NULL,
   "(IIII)", 0, 0, 10, 10);

The first parameter should always be NULL, the second parameter is the full class name, but uses "/" instead of "." as the package delimiter. The third parameter should be NULL. The fourth parameter is the signature of the particular constructor to call (because there may be many), but without the return type (because constructors can't return values). Finally, pass any arguments. The return value will either be a pointer to the newly constructed Java object or NULL if an error has occurred.

Creating Arrays

To allocate an array, you can use ClassArrayAlloc. For example, to create an array of 200 bytes, you would use the following:

    HObject *phobj = ClassArrayAlloc(T_BYTE, 200, NULL);

The first parameter is the type of array to allocate, the second is the number of elements to allocate, and the third is only used when allocating arrays of non-primitive objects. For example, to allocate an array of 200 Rectangles, use the following:

    HObject *phobj = ClassArrayAlloc(T_CLASS, 200, "java/awt/Rectangle");

Note   This just allocates space for the array itself. Each object in the array needs to be individually allocated.

The following list shows all the types supported by ClassArrayAlloc:

T_BOOLEAN

T_BYTE

T_CHAR

T_CLASS

T_DOUBLE

T_FLOAT

T_INT

T_LONG

T_SHORT

Creating Strings

Because Java string creation is such a common operation in native code, some string-specific functions exist to help you do so. For example, use the following to create a Java string of "Hello":

    HJava_lang_String *phString = makeJavaString("Hello", 5);

The first parameter is the C character array to use and the second parameter is the number of characters in the string (not including the terminating NULL).

Be aware that the JDK makeCString function is not supported in the Microsoft RNI API. It is recommended that you use the javaString2CString function instead of makeCString.

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