簡體   English   中英

SWIG在Java中將c ++字符串包裝為byte []

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

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;
  }
};
%}

爪哇

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

到這里為止一切都很好,但是我想要獲得的是此java返回值(byte [])作為參數。 喜歡

public void foo(byte[] example) {}

為了獲得該功能,我嘗試了很多可能的修改,但是沒有辦法,而且我已經超出了想象力。 任何建議,我將不勝感激。 非常感謝你。

使用SWIG與數組進行交互的正確方法中的技術很可能適用。 24.8.5 Binary data vs Strings中記錄了該技術。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM