Using the Raw Native Interface Previous
Previous
Introduction
Introduction
Index
Index
Next
Next

Accessing Fields

In many real-world examples, the Java class will have fields that we want to modify from native code; this is fairly straightforward. Imagine we have a class that looks like the following:

    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

Here, the ClassFieldDemo structure defines fields for modifying x, y, and z. So, for example, if we wanted to set x, y, z to 42, 43, and 44, we'd write the following:

    void cdecl FieldDemo_SetFields(struct HFieldDemo *phThis)
    {
        phThis->x = 42;
        phThis->y = 43;
        phThis->z = 44;
    }

Note how the Java "int" maps to a "long" on the native side. The following table shows how all the types map:
Java C
boolean long
byte long
char long
double double
float float
int long
long int64_t
short long


Top© 1997 Microsoft Corporation. All rights reserved. Legal Notices.