我到处都找遍了。我在这个网站上尝试了一些技巧。但无济于事。
我有一个c++全局函数char* squid( char* buff, int len );
创建一个。I文件
%module Crabby
%include "arrays_java.i"
%{
/* headers here are included in the wrapper code */
#include "sponge.h"
%}
%typemap(jtype) (const signed char *arr, size_t sz) "byte[]"
%typemap(jstype) (const signed char *arr, size_t sz) "byte[]"
%typemap(jni) (const signed char *arr, size_t sz) "jbyteArray"
%typemap(javain) (const signed char *arr, size_t sz) "$javainput"
%typemap(in) (const signed char* arr, size_t sz) {
$1 = JCALL2(GetByteArrayElements, jenv, $input, NULL);
const size_t sz = JCALL1(GetArrayLength, jenv, $input);
$2 = $1 + sz;
}
%typemap(freearg) (const signed char *arr, size_t sz) {
// Or use 0 instead of ABORT to keep changes if it was a copy
JCALL3(ReleaseByteArrayElements, jenv, $input, $1, JNI_ABORT);
}
%apply (const signed char* arr, size_t sz) { (const unsigned char* buff, int len) }
%apply (const signed char* arr, size_t sz) { (const unsigned char* query, int queryLen) }
%include "sponge.h"
无论我做什么,界面总是
public static String Squid(String buff, int len)
如果我删除unsigned,我会在cxx包装器中得到非法转换
这是Swig 2.0.1
您的界面很接近,但是有以下问题:
-
const
对%apply
的影响 - 您需要精确匹配
buff
的有符号/无符号限定符(在您显示的声明中没有限定符)。 - 你的intypemap需要
numinputs=1
来压缩它到一个Java输入。 - 将指针的大小设置为计算指针没有多大意义。
所以固定界面看起来像:
%module Crabby
%include "arrays_java.i"
%{
/* headers here are included in the wrapper code */
#include "sponge.h"
%}
%typemap(jtype) (const signed char *arr, size_t sz) "byte[]"
%typemap(jstype) (const signed char *arr, size_t sz) "byte[]"
%typemap(jni) (const signed char *arr, size_t sz) "jbyteArray"
%typemap(javain) (const signed char *arr, size_t sz) "$javainput"
%typemap(in,numinputs=1) (const signed char* arr, size_t sz) {
$1 = JCALL2(GetByteArrayElements, jenv, $input, NULL);
const size_t sz = JCALL1(GetArrayLength, jenv, $input);
$2 = sz;
}
%typemap(freearg) (const signed char *arr, size_t sz) {
// Or use 0 instead of ABORT to keep changes if it was a copy
JCALL3(ReleaseByteArrayElements, jenv, $input, $1, JNI_ABORT);
}
%apply (const signed char* arr, size_t sz) { ( char* buff, int len) }
%include "sponge.h"