编程语言
首页 > 编程语言> > 利用SWIG将CTP封装成JAVA版本

利用SWIG将CTP封装成JAVA版本

作者:互联网

重点参考景色大佬文章: 

 CTP JAVA API(JCTP)编译(利用Swig封装C++动态库)windows版_景色正好的博客-CSDN博客

 Swig转换C++接口中文乱码解决方案_景色正好的博客-CSDN博客

其中第二篇文章里,直接用文中给出的i修改方式会报错,原因在文章的评论区。

 

 

最后可用的 .i 文件全文如下:(将市场接口和行情接口打包到一个jar中)

%module(directors="1") CTP4Java 
%include "various.i"
%apply char **STRING_ARRAY { char *ppInstrumentID[] }


%{ 
#include "ThostFtdcMdApi.h"
#include "ThostFtdcTraderApi.h"

#include <codecvt>
#include <locale>
#include <vector>
#include <string>
using namespace std;
#ifdef _MSC_VER
const static locale g_loc("zh-CN");
#else    
const static locale g_loc("zh_CN.GB18030");
#endif
%}
 

%typemap(out) char[ANY], char[] {
    const std::string &gb2312($1);
    std::vector<wchar_t> wstr(gb2312.size());
    wchar_t* wstrEnd = nullptr;
    const char* gbEnd = nullptr;
    mbstate_t state = {};
    int res = use_facet<codecvt<wchar_t, char, mbstate_t> >
        (g_loc).in(state,
            gb2312.data(), gb2312.data() + gb2312.size(), gbEnd,
            wstr.data(), wstr.data() + wstr.size(), wstrEnd);
 
    if (codecvt_base::ok == res)
    {
        wstring_convert<codecvt_utf8<wchar_t>> cutf8;
        std::string result = cutf8.to_bytes(wstring(wstr.data(), wstrEnd));       
        $result=JCALL1(NewStringUTF,jenv,result.c_str());    }
    else
    {
        std::string result;
        $result=JCALL1(NewStringUTF,jenv,result.c_str());    }
}

%feature("director") CThostFtdcMdSpi;
%ignore THOST_FTDC_VTC_BankBankToFuture;
%ignore THOST_FTDC_VTC_BankFutureToBank;
%ignore THOST_FTDC_VTC_FutureBankToFuture;
%ignore THOST_FTDC_VTC_FutureFutureToBank;
%ignore THOST_FTDC_FTC_BankLaunchBankToBroker;
%ignore THOST_FTDC_FTC_BrokerLaunchBankToBroker;
%ignore THOST_FTDC_FTC_BankLaunchBrokerToBank;
%ignore THOST_FTDC_FTC_BrokerLaunchBrokerToBank;
%include "ThostFtdcUserApiDataType.h"
%include "ThostFtdcUserApiStruct.h" 
%include "ThostFtdcMdApi.h"
%feature("director") CThostFtdcTraderSpi;
%include "ThostFtdcTraderApi.h"

  

标签:JAVA,FTDC,SWIG,THOST,ignore,result,CTP,include,data
来源: https://www.cnblogs.com/tugeler/p/16527839.html