[1666] in java-interest

home help back first fref pref prev next nref lref last post

Re: Native methods and strructures

daemon@ATHENA.MIT.EDU (Chuck McManis)
Wed Sep 13 16:42:26 1995

Date: Wed, 13 Sep 1995 09:37:49 -0700
From: cmcmanis@scndprsn.Eng.Sun.COM (Chuck McManis)
To: java-interest@java.Eng.Sun.COM, "kl@hi02c342"@hic334.decnet.bosch.de

I apologize if I've misunderstood your question, but here is my take on
it.

>I want to write a native method which handles a pointer of a
>class representing a data structure to a C-Program.
> 
>for example:
> 
>Data Structure(Class) in Java
>package demo;
>    public class data{
>      int.x
>     I int.y
>     }

OK I will assume that you've defined a class data, and it has two
instance variables x and y. Your text above is a bit garbled so I
ask would this be an accurate representation ?

	public class data {
	    int x;
	    int y;
	}

>     
>My native method should be something like:
[... deleted ...]
>     public void native send_data_to_c(data d);
[... deleted ...]

This would work fine. Now the tricky bit is that your C code
gets a pointer to an object handle. If you want to pass the
C code this:
	struct foo {
	    int x;
	    int y;
	}

Then you can do it as follows (note I coded it at data.java and
yy.java as the test class):
#0) compile data.java and yy.java

#1) Run javah on your data class to get data.h:
	javah data

#2) Run javah -stubs to generate a C stub for yy's method
	javah -stubs yy	; generate yy.c
	javah yy 	; generate yy.h

#3) In your native method implementation (in C) write:
	/* yyimpl.c */
	#include "data.h"
	#include "yy.h"

	struct foo {
	    int x;
	    int y;
	}

	void yy_send_data_to_c(struch Hyy *this, struct Hdata *datap) {
	    struct foo tmp;
	    struct Classdata *myData = unhand(datap); /* real pointer */
	
	    /* Convert Java class data into the "expected" C structure. */
	    tmp.x = myData->x;	/* get X from class data */
	    tmp.y = myData->y;  /* get y from class data */
	    clibraryfunction(&tmp);
	    return;
	}

#4) Compile yy.c and yyimpl.c and link them into your shared object
    (.DLL or .so file) and you're done.


Additional comments:

This example assumed that you are effectively "wrapping" a pre-existing
C language call with a Java call (hence the use of the tmp structure).
If you were also writing the clibrayfunction() above you could write
it to take a Classdata structure and not bother with the conversion.

The name 'Classdata' is in fact the combination of the word "Class" with
the name of the class "data" it is not generic. If you had named your
class with x and y Point the data structure name would have been ClassPoint.

If your classes are in packages (have a package statement at the top)
the structure names get the package name prepended to them. So if 
yy was in package demo then the function call yy_send_data_to_c()
would be called demo_yy_send_data_to_c(). Underscores replace '.' in
package names (package my.demo -> my_demo_xxxx).

Hope this helps,
--Chuck

-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post