Using the Raw Native Interface |
![]() Previous |
![]() Introduction |
![]() Index |
![]() Next |
These sections describe the creation of objects, arrays, and strings.
Creating Java objects from native code is simple using execute_java_constructor(). For example, to effectively do "new Rectangle(0, 0, 10, 10), you would 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 NULL. The fourth parameter is the signature of the particular constructor to call (since there may be many), but without the return type (since constructors can't return values) Finally, you pass the arguments. The return value will either be a pointer to the newly constructed Java object or NULL if an error has occurred.
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, you would write the following:
HObject *phobj = ClassArrayAlloc(T_CLASS, 200, "java/awt/Rectangle");
Note that this just allocates space for the array itself. Each object in the array will have to be individually allocated.
The following is a table that 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 |
Since Java string creation is such a common operation in native code, there are some string specific functions to make life a little easier. 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 string 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 Raw Native Interface API. It is recommended that you use the javaString2Cstring function in place of this.
© 1997 Microsoft Corporation. All rights reserved. Legal Notices.