|
|
|||||||||||||||
Object CreationThese sections describe the creation of objects, arrays, and strings. Basic ObjectsCreating 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)", you 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, 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 ArraysTo 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 use 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 needs to be individually allocated. The following is a table that shows all the types supported by ClassArrayAlloc:
Creating StringsSince 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 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 Raw Native Interface API. It is recommended that you use the javaString2Cstring function in place of this.
|
© 1998 Microsoft Corporation. All rights reserved. Terms of use. |