Using the Raw Native Interface |
![]() Previous |
![]() Introduction |
![]() Next |
In many real-world examples the Java class will have fields that we want to modify from native code and this is pretty straightforward. Imagine we have a class that looks like:
class FieldDemo { int x; int y; int z; public native void SetFields(); }
This is the 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
So here the ClassFieldDemo structure defines fields for modifying x, y, z so for example if we wanted to set x, y, z to 42, 43 and 44 we'd write:
void cdecl FieldDemo_SetFields(struct HFieldDemo *phThis) { phThis->x = 42; phThis->y = 43; phThis->z = 44; }
One thing to note is how the Java "int" maps to a "long" on the native side, here's a table of how all the types map.
Java | C |
boolean | long |
byte | long |
char | long |
double | double |
float | float |
int | long |
long | int64_t |
short | long |