Accessing Fields
In many real-world examples, the Java class will have fields that you want to modify from native code; this is fairly straightforward. The following simple class demonstrates this.
class FieldDemo
{
int x;
int y;
int z;
public native void SetFields();
}
Produces the following msjavah generated header file:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <native.h>
/* Header for class FieldDemo */
#ifndef _Included_FieldDemo
#define _Included_FieldDemo
typedef struct ClassFieldDemo {
#pragma pack(push,1)
int32_t MSReserved;
long x;
long y;
long z;
#pragma pack(pop)
} ClassFieldDemo;
#define HFieldDemo ClassFieldDemo
#ifdef __cplusplus
extern "C" {
#endif
__declspec(dllexport) void __cdecl FieldDemo_SetFields(struct HFieldDemo *);
#ifdef __cplusplus
}
#endif
#endif
The ClassFieldDemo structure defines fields for modifying x, y, and z. If you wanted to set x, y, z to 42, 43, and 44, you would call the following function.
void cdecl FieldDemo_SetFields(struct HFieldDemo *phThis)
{
phThis->x = 42;
phThis->y = 43;
phThis->z = 44;
}
Notice how the Java int type maps to a long type on the native side. The following table shows how all the types map from Java to C.
Java
| C
|
boolean
| long
|
byte
| long
|
char
| long
|
double
| double
|
float
| float
|
int
| long
|
long
| int64_t
|
short
| long
|