JNA 中 BSTR 数据类型的映射
在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找不到正确的“Type for Type”映射,但为了确定,您可以尝试这种方式并返回一个字符串(长度> 0)。
[编辑:参见 Technomage 评论]
注意:在使用
Memory
对象时,您应该更加更加小心。当 java 对象被垃圾收集时,它们会在本机级别被释放。这意味着您的代码 super(new Memory(value.length()*2+6).share(4)); 只是浪费时间,因为您的 new Memory(.. ) 在您传递该行后立即消失,因为.share(4)
给出了一个新的独立Pointer
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 codesuper(new Memory(value.length()*2+6).share(4));
is just a waste of time because yournew Memory(..)
disappear the very moment after you pass the line since the.share(4)
give a new independantPointer