dcsimg
A too complex parameter to be pass throw JNI,Help
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Jeffrey_rui
Posted On:   Friday, July 19, 2002 02:35 AM

I want to get current myclass instance from CPP,the class include a array of structure called myPattern, and the structure include a array of long. It seem quite complex,i am going to mad.: < Help! source code below class myclass { &nbsp&nbspint t1; &nbsp&nbspchar t2[21]; &nbsp&nbspPmypattern t3; &nbsp&nbspvoid func1(); &nbsp&nbspstring fun2(int para); }; Pmypattern t3; typedef struct _myPattern { &nbsp&nbsplong p1; &nbsp&nbspchar p2; &nbsp&nbsplong p3[4]; &nbsp&nbspint p4; }myPattern, *Pmypatt   More>>

I want to get current myclass instance from CPP,the class include a array of structure called myPattern, and the structure include a array of long. It seem quite complex,i am going to mad.: <
Help! source code below

class myclass {

&nbsp&nbspint t1;

&nbsp&nbspchar t2[21];

&nbsp&nbspPmypattern t3;

&nbsp&nbspvoid func1();

&nbsp&nbspstring fun2(int para);

};

Pmypattern t3;

typedef struct _myPattern {

&nbsp&nbsplong p1;

&nbsp&nbspchar p2;

&nbsp&nbsplong p3[4];

&nbsp&nbspint p4;

}myPattern, *Pmypattern;

   <<Less

Re: A too complex parameter to be pass throw JNI,Help

Posted By:   Alexander_Krapf  
Posted On:   Tuesday, July 23, 2002 04:35 PM

Jeffrey,


That's why sane people usually don't write JNI interfaces with complex types :-)


But seriously, even if you get this right after debugging all little JNI issues that cost you a day each, do you really want to maintain this code?


A typical pattern for exposing a complex C++ object to Java is to have a Java class that mirrors the structure in layout and has a constructor and two methods as in:



MyWrapper( long ptr );

private native void initFromCpp( long ptr );

private native void writeToCpp( long ptr );

The native methods are implemented as taking the "ptr" argument and casting it to a poiner to an instance of "myclass". Now you can get at the values of the C++ object and set them into the Java instance's fields, one by one.


Not trivial and very error prone. Alternatively check out JunC++ion at www.codemesh.com which allows you to implement your native method in straight C++, making assignments to Java fields etc. as you would in C++.


Hope this helps,


Alex

About | Sitemap | Contact