ctp交易平台轉java介面轉換

來源:互聯網
上載者:User

標籤:協助   ida   put   distrib   bsp   row   amp   tor   sizeof   

首先感謝倪材@csdn的部落格,給了我很大協助。

http://blog.csdn.net/pjjing/article/details/53186394

http://blog.csdn.net/pjjing/article/details/53187469

 

這裡著重說一下轉換中遇到的問題。

 

首先swig中轉換 char *str[] 和char **時會轉換成SWIGTYPE_p_p_char類型,這裡需要在編寫.i檔案之前,先編寫various.i檔案,內容如下

/* ----------------------------------------------------------------------------- * 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 are for C String arrays which are NULL terminated. *   char *values[] = { "one", "two", "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", "two", "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(in) char **STRING_ARRAY (jint size) {    int i = 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 *));#endif        strcpy($1[i], c_string);        JCALL2(ReleaseStringUTFChars, jenv, j_string, c_string);        JCALL1(DeleteLocalRef, jenv, j_string);    }    $1[i] = 0;}%typemap(freearg) char **STRING_ARRAY {    int i;    for (i=0; i<size$argnum-1; i++)#ifdef __cplusplus      delete[] $1[i];    delete[] $1;#else      free($1[i]);    free($1);#endif}%typemap(out) char **STRING_ARRAY {    int i;    int len=0;    jstring temp_string;    const jclass 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 are 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(in) 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 are input typemaps for mapping a Java byte[] array to a C char array. * Note that as a Java array is used and thus passeed by reference, the C routine  * can return data to Java via the parameter. * * 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(in) 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 ""

然後將various.i引入自己編寫的.i檔案:

%module(directors="1") thosttradeapi%include "various.i"%apply char **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交易平台轉java介面轉換

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.