JNA 中 BSTR 数据类型的映射

发布于 2024-10-16 06:31:47 字数 659 浏览 3 评论 0原文

在DLL的头文件中,我需要包装使用BSTR数据类型作为I/O参数。我需要在 JNA 中创建它的映射。我发现了以下示例:

class BSTR extends PointerType {
  public BSTR() { }
  public BSTR(String value) {
    super(new Memory(value.length()*2+6).share(4));  
    getPointer().setInt(-4, value.length()*2);
    getPointer().setString(0, value, true);
 }
 public String toString() {
    int length = getPointer().getInt(-4);
    char[] data = getPointer().getCharArray(0, length/2);
    return new String(data);
 }

}

但在 JNA 方法调用中使用它后,结果为空(= 长度为 0 并且没有数据)。您对如何为 BSTR 创建正确的映射以将其用作函数的 I/O 参数有什么建议吗?看起来 BSTR 不是通过引用 DLL 方法传递的,因此结果仍然为空,但这只是我的假设。也许映射是正确的,但在方法调用中错误地使用了。预先感谢您的任何建议。

in header file of DLL I need to wrap is used BSTR data type as I/O parameter. I need to create its mapping in JNA. I have found following example:

class BSTR extends PointerType {
  public BSTR() { }
  public BSTR(String value) {
    super(new Memory(value.length()*2+6).share(4));  
    getPointer().setInt(-4, value.length()*2);
    getPointer().setString(0, value, true);
 }
 public String toString() {
    int length = getPointer().getInt(-4);
    char[] data = getPointer().getCharArray(0, length/2);
    return new String(data);
 }

}

but after using it in JNA method call the result is empty (= length is 0 and no data). Do you have please any suggestions how to create correct mapping for BSTR to use it as I/O param of the function? It looks like the BSTR is not passed by reference to the DLL method so result is still empty but it is only my supposition. Maybe the mapping is correct but is wrongly used in method call. Thank in advance for any suggestion.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恍梦境° 2024-10-23 06:31:47

我找不到正确的“Type for Type”映射,但为了确定,您可以尝试这种方式并返回一个字符串(长度> 0)。

[编辑:参见 Technomage 评论]
注意:在使用Memory对象时,您应该更加更加小心。当 java 对象被垃圾收集时,它们会在本机级别被释放。这意味着您的代码 super(new Memory(value.length()*2+6).share(4)); 只是浪费时间,因为您的 new Memory(.. ) 在您传递该行后立即消失,因为 .share(4) 给出了一个新的独立 Pointer

class BSTRUtils {
  private BSTR() { }
  public static Memory toNative(String value) {
    Memory m = new Memory(value.length()*2+6);  
    m.setInt(0, value.length()*2);
    m.setString(4, value, true);
    return m;
  }
  public static String toString(PointerByReference pbr) {
    return toString(pbr.getValue());
  }
  public static String toString(Pointer p) {
    int length = p.getInt(0);
    char[] data = p.getCharArray(4, length/2);
    return new String(data);
  }
}

I can't find a correct "Type for Type" mapping but just to be sure, can you try it this way and get back a string (length > 0).

[Edit : see Technomage Comment]
ATTENTION : You should be much much more careful when using Memory objects. They get free'd at the native level when the java object gets garbadge collected. This means that your code super(new Memory(value.length()*2+6).share(4)); is just a waste of time because your new Memory(..) disappear the very moment after you pass the line since the .share(4) give a new independant Pointer

class BSTRUtils {
  private BSTR() { }
  public static Memory toNative(String value) {
    Memory m = new Memory(value.length()*2+6);  
    m.setInt(0, value.length()*2);
    m.setString(4, value, true);
    return m;
  }
  public static String toString(PointerByReference pbr) {
    return toString(pbr.getValue());
  }
  public static String toString(Pointer p) {
    int length = p.getInt(0);
    char[] data = p.getCharArray(4, length/2);
    return new String(data);
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文