SWIG c++转Java

传递 char*(char*,int)时,使用,并将char*更改为byteBuff,byteBuff{char*,int}

一、.i文件

%include "typemaps.i"

%include "carrays.i"
%{
#include "TDFClient.h"
%}

%module tdfapi
%pragma(java) jniclasscode=%{ 
  static { 
    try { 
        System.loadLibrary("tdfapi_jni"); 
    } catch (UnsatisfiedLinkError e) { 
      System.err.println("Native code library failed to load. \n" + e); 
      System.exit(1); 
    } 
  } 
%}

%apply (char *STRING, int LENGTH) { (char *data, int size) };
%typemap(newfree) char * "delete[] ($1);";

%typemap(out) ByteBuff {
  size_t n = ((ByteBuff)result).len;
  char* buf = ((ByteBuff)result).buff;
   printf("n=%d,buf=%s\n",n,buf);
  if(buf)
  {
 jresult = jenv->NewByteArray(n);
 if(jresult)
 jenv->SetByteArrayRegion(jresult,0,n, (jbyte*)buf);
 delete[] buf;
  }
}

%typemap(jni)      ByteBuff "jbyteArray"
%typemap(jtype)    ByteBuff "byte[]"
%typemap(jstype)   ByteBuff "byte[]"
%typemap(javaout)  ByteBuff { return $jnicall; }

%include "TDFClient.h"


c++端

ByteBuff GetAsynMsg(int timeout){};

void  SendMsg(char *data, int size){};


二、编译代码(c++ to java): D:\Tools\swigwin-2.0.11\swig(swig安装路径) -java -c++ -package cn.com.w -outdir src/cn/com/w  -I.\  -o tdfapi_wrap.cxx(形成的文件名) tdfapi.i(swig的.i文件)


你可能感兴趣的:(SWIG c++转Java)