CTP API Development of the second: the production of CTP Java version API

Source: Internet
Author: User

The current technical CTP system provides the version of the API version C++

SWIGis a C/C++ tool that can be used to convert interfaces to other languages, which can now be supported Python,Java,R .
This article introduces the use of the Swig tool to convert the CTP C + + interface API into a Java callable interface under the Windows 32/64-bit platform.

1, download the latest API package from the CTP website, including 32-bit and 64-bit package. API File Package Checklist:

2. Download and install Swig software:

3. Create thostapi.i and various.i files in the API package, THOSTAPI.I is an interface file that tells Swig which classes and methods to create interfaces for

various.i is a tool class for converting array parameters in C + + interfaces to Java

%module (directors="1") Thosttraderapi%include"various.i"%applyChar**string_array {Char*ppinstrumentid[]}%{#include"ThostFtdcMdApi.h"#include"ThostFtdcTraderApi.h"%} %feature ("Director") Cthostftdcmdspi; %include"ThostFtdcUserApiDataType.h"%include"ThostFtdcUserApiStruct.h"%include"ThostFtdcMdApi.h"%feature ("Director") Cthostftdctraderspi;%include"ThostFtdcTraderApi.h"

4. Generate Java Interface:

Create a SRC/CTP folder in the current folder to place the generated Java files

.. \.. \swigwin-2.0. \swig.exe-c++-java-package ctp.thosttraderapi-outdir src-o thosttraderapi_wrap.cpp thostapi.i

After the run is complete, you can see the files in the current folder that are used to wrap C++ the original interface:

5. Get Java callable Dynamic Library through C + +

Create a C + + project, application type Select DLL, add the following files to the project:

Put a \Java\jdk1.8.0_111\include jni.h total of win32 jni_md.h, jawt_md.h three files under the DK directory and folders

Copy to the install VS's include directory \Microsoft Visual Studio 12.0\VC\include .

Because the thosttraderapi_wrap.cpp file contains a <jni.h> library file that is used to generate a callable Java interface.

Comment out the following 8 functions, which involve converting a string to a char type, with a problem:

Java_ctp_thosttraderapi_thosttradeapijni_thost_1ftdc_1vtc_1bankbanktofuture_1get Java_ctp_thosttraderapi_ Thosttradeapijni_thost_1ftdc_1vtc_1bankfuturetobank_1get java_ctp_thosttraderapi_thosttradeapijni_thost_1ftdc_ 1vtc_1futurebanktofuture_1get JAVA_CTP_THOSTTRADERAPI_THOSTTRADEAPIJNI_THOST_1FTDC_1VTC_1FUTUREFUTURETOBANK_ 1get Java_ctp_thosttraderapi_thosttradeapijni_thost_1ftdc_1ftc_1banklaunchbanktobroker_1get Java_ctp_ Thosttraderapi_thosttradeapijni_thost_1ftdc_1ftc_1brokerlaunchbanktobroker_1get Java_ctp_thosttraderapi_ Thosttradeapijni_thost_1ftdc_1ftc_1banklaunchbrokertobank_1get Java_ctp_thosttraderapi_thosttradeapijni_thost_ 1ftdc_1ftc_1brokerlaunchbrokertobank_1get

Then compile to generate the Java Callable Dynamic library file Thosttraderapi_wrap.dll:

6. Create a Java project, copy the three dynamic libraries and the previously generated SRC/CTP package to the project, and load the dynamic library in:

This Java API is ready for Java development.

Attached VARIOUS.I:

/** 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 __cplusplus delete[] $1[i]; Delete[] $1;#elseFree ($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""

CTP API Development of the second: the production of CTP Java version API

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.