3

我到处看了看。我已经尝试了这个网站上的一些技术。无济于事。

我有一个 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)

如果我删除未签名的,我会在 cxx 包装器中得到非法转换

这是 Swig 2.0.1

4

1 回答 1

2

您的界面已关闭,但存在以下问题:

  1. const事关%apply
  2. 您需要完全匹配有符号/无符号限定符buff(您显示的声明中没有限定符。
  3. 您的 in 类型映射需要numinputs=1将其压缩为一个 Java 输入。
  4. 将大小设置为计算指针没有多大意义。

所以固定界面看起来像:

%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"
于 2013-07-14T17:54:24.767 回答