简体   繁体   English

SWIG在Java中将c ++字符串包装为byte []

[英]SWIG wrap a c++ string into a byte[] in java

swig.i swig.i

%typemap(jtype) bool foo "byte[]"
%typemap(jstype) bool foo "byte[]"
%typemap(jni) bool foo "jbyteArray"
%typemap(javaout) bool foo { return $jnicall; }
%typemap(in, numinputs=0) std::string& out (std::string temp) "$1=&temp;"
%typemap(argout) std::string& out {
  $result = JCALL1(NewByteArray, jenv, $1->size());
  JCALL4(SetByteArrayRegion, jenv, $result, 0, $1->size(), (const jbyte*)$1->c_str());
}
// Optional: return NULL if the function returned false
%typemap(out) bool foo ""

%inline %{
struct Bar {
  bool foo(std::string& out) {
    std::string s;
    s.push_back('H');
    s.push_back('o');
    s.push_back(0);
    s.push_back('l');
    s.push_back('a');
    out = s;
    return true;
  }
};
%}

Java 爪哇

public byte[] foo() { return NativeISecurityProviderJNI.Bar_foo(swigCPtr, this); }

Till here everything fine, but what I am trying to get is this java return (byte[]) as an argument. 到这里为止一切都很好,但是我想要获得的是此java返回值(byte [])作为参数。 like 喜欢

public void foo(byte[] example) {}

I've tried a lot of possible modifications in order to get that, but there is no way and I've ran out of imagination. 为了获得该功能,我尝试了很多可能的修改,但是没有办法,而且我已经超出了想象力。 I would appreciate whatever suggestion. 任何建议,我将不胜感激。 Thanks you very much. 非常感谢你。

The technique at Correct way to interact with arrays using SWIG likely applies. 使用SWIG与数组进行交互的正确方法中的技术很可能适用。 The technique is documented at 24.8.5 Binary data vs Strings . 24.8.5 Binary data vs Strings中记录了该技术。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM