Using the Raw Native Interface Previous
Previous
Introduction
Introduction
Next
Next

Object Creation

Basic Objects , Creating Arrays , Creating Strings

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


Basic Objects

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:

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

The first param should always be NULL, the second parameter is the full class name but using '/' 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) and finally you pass the arguments. The return value will either be a pointer to the newly constructed Java object or NULL if an error occurred.


Creating Arrays

To allocate an array you can use ClassArrayAlloc, for example to create an array of 200 bytes you would use:

    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:

    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 allocated individually.

Here's a table 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

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 to create a Java string of "Hello" you would use:

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

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

Top© 1996 Microsoft Corporation. All rights reserved.