First of all thank Kan @csdn blog, gave me a lot of help.
http://blog.csdn.net/pjjing/article/details/53186394
http://blog.csdn.net/pjjing/article/details/53187469
Here is a focus on the problems that are encountered in the conversion.
First, converting Char *str[] and char * * in Swig will be converted to the Swigtype_p_p_char type, where you need to write the various.i file before writing the. i file, which reads as follows
/*-----------------------------------------------------------------------------* See the LICENSE file for Information on copyright, usage and redistribution * of SWIG, and the README file for authors-http://www.swig.org/release.html.* * various.i * * SWIG typemap Library for Java. * various useful typemaps. *--------------------------------------- -------------------------------------- *//** Char **string_array typemaps. * These typemaps is for C String arrays which is NULL terminated. * Char *values[] = {"One", "one", "one", "three", NULL}; Note NULL * char * * is mapped to a Java string[]. * * Example Usage Wrapping: *%apply char **string_array {char **input}; * char * * FOO (char **input); * * Java Usage: * String numbers[] = {"One", "one", "one", "three"}; * string[] ret = Modulename.foo (Numbers}; */%typemap (JNI)Char**string_array"Jobjectarray"%typemap (Jtype)Char**string_array"string[]"%typemap (Jstype)Char**string_array"string[]"%typemap (inch)Char**String_array (jint size) {inti =0; Size=JCALL1 (Getarraylength, jenv, $input); #ifdef __cplusplus $1=New Char*[size+1];#else $1= (Char**)calloc(size+1,sizeof(Char*));#endif for(i =0; i<size; i++) {jstring j_string=(jstring) JCALL2 (getobjectarrayelement, jenv, $input, i); Const Char*c_string = JCALL2 (Getstringutfchars, jenv, j_string,0); #ifdef __cplusplus $1[I] =New Char[Strlen (c_string) +1];#else $1[I] = (Char*)calloc(Strlen (c_string) +1,sizeof(Const Char*));#endifstrcpy ($1[i], c_string); JCALL2 (Releasestringutfchars, jenv, j_string, c_string); JCALL1 (Deletelocalref, jenv, j_string); } $1[I] =0;}%typemap (Freearg)Char**String_array {inti; for(i=0; i<size$argnum-1; i++) #ifdef __cplusplusDelete[] $1[i]; Delete[] $1;#else Free($1[i]); Free($1);#endif}%typemap ( out)Char**String_array {inti; intlen=0; Jstring temp_string; ConstJclass clazz = JCALL1 (Findclass, Jenv,"java/lang/string"); while($1[Len]) len++; Jresult=JCALL3 (Newobjectarray, jenv, Len, Clazz, NULL); /*exception checking omitted*/ for(i=0; i<len; i++) {temp_string= JCALL1 (Newstringutf, jenv, *result++); JCALL3 (Setobjectarrayelement, jenv, Jresult, I, temp_string); JCALL1 (Deletelocalref, jenv, temp_string); }}%typemap (Javain)Char**string_array"$javainput"%typemap (Javaout)Char**String_array {return$jnicall; }/** Char **string_out typemaps. * These is typemaps for returning strings when using a C char * * parameter type. * The returned string appears in the 1st element of the passed in Java string array. * * Example usage wrapping: * void foo (char **string_out); * * Java Usage: * String stringoutarray[] = {""}; * Modulename.foo (Stringoutarray); * SYSTEM.OUT.PRINTLN (stringoutarray[0]); */%typemap (JNI)Char**string_out"Jobjectarray"%typemap (Jtype)Char**string_out"string[]"%typemap (Jstype)Char**string_out"string[]"%typemap (Javain)Char**string_out"$javainput"%typemap (inch)Char**string_out ($*1_ltype Temp) { if(!$input) {swig_javathrowexception (jenv, Swig_javanullpointerexception,"array null"); return$NULL; } if(JCALL1 (Getarraylength, jenv, $input) = =0) {swig_javathrowexception (jenv, Swig_javaindexoutofboundsexception,"Array must contain at least 1 element"); return$NULL; } $1= &temp;}%typemap (Argout)Char**string_out {jstring jnewstring=NULL; if($1) {jnewstring= JCALL1 (Newstringutf, jenv, *$1); } JCALL3 (Setobjectarrayelement, jenv, $input,0, jnewstring); }/** Char *byte typemaps. * These is input typemaps for mapping a Java byte[] array to a C char array. * Note that as a Java array are used and thus passeed by reference, the C-routine * can return data to Java via the Parame ter. * * Example usage wrapping: * void foo (char *array); * * Java usage: * byte b[] = new BYTE[20]; * Modulename.foo (b); */%typemap (JNI)Char*byte"Jbytearray"%typemap (Jtype)Char*byte"byte[]"%typemap (Jstype)Char*byte"byte[]"%typemap (inch)Char*BYTE {$1= (Char*) JCALL2 (getbytearrayelements, jenv, $input,0); }%typemap (Argout)Char*BYTE {JCALL3 (releasebytearrayelements, Jenv, $input, (Jbyte*) $1,0); }%typemap (Javain)Char*byte"$javainput"/*Prevent default Freearg typemap from being used*/%typemap (Freearg)Char*byte""
Then introduce various.i to the. I file you wrote:
%module (directors="1") Thosttradeapi%include"various.i"%applyChar**string_array {Char*ppinstrumentid[]}%{#include"ThostFtdcTraderApi.h"#include"ThostFtdcMdApi.h"%} %feature ("Director") Cthostftdctraderspi; %include"ThostFtdcUserApiDataType.h"%include"ThostFtdcUserApiStruct.h"%include"ThostFtdcTraderApi.h"%feature ("Director") Cthostftdcmdspi; %include"ThostFtdcMdApi.h"
CTP Trading platform Switch to Java interface