JNA在windows平台上映射LPCSTR

发布于 2024-10-02 12:44:26 字数 212 浏览 0 评论 0原文

我正在使用 JNA 为 C/C++ 调用一个 DLL api。 DLL中的函数API是短DKT_init(LPCSTR name)。我将相应的java方法设置为public Short DKT_init(String name);但是当我调用它时,DLL API 返回参数错误。我想知道如何在 JNA 中映射 LPCSTR?因为 LPCSTR 是 cons char * 而 String 是 char *。

I am working on call one DLL api for C/C++ with JNA.
The function API in DLL is short DKT_init(LPCSTR name). I made the corresponding java method as public short DKT_init(String name); But when I call it, the DLL API return a parameter error. I wonder how to map LPCSTR in JNA? As LPCSTR is cons char * but String is char *.

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

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

发布评论

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

评论(2

最初的梦 2024-10-09 12:44:26

字符串是 LPCSTR 的适当映射。 JNA 将使用默认平台编码将修改后的 UTF16 字符转换为以 NUL 结尾的字节缓冲区。

您可以尝试传入显式字节数组(使用上面建议的替代方法映射),这将消除潜在的不正确编码问题,例如

byte[] arg = { (byte)'f', (byte)'o', (byte)'o', (byte)0 };

您可以通过设置系统属性“jna.encoding”来更改使用的编码。

您还应该消除“LPCSTR”实际上是错误类型的可能性;如果函数需要一个可以写入的缓冲区,则 String 将不起作用,如果它实际上是 LPTCSTR 并且您使用的是 UNICODE,那么您需要传递一个 WString。

String is the appropriate mapping for LPCSTR. JNA will convert the modified UTF16 characters into a NUL-terminated buffer of bytes using the default platform encoding.

You might try passing in an explicit byte array instead (using the suggested alternate method mapping above), which would eliminate the potential of an incorrect encoding issue, e.g.

byte[] arg = { (byte)'f', (byte)'o', (byte)'o', (byte)0 };

You can alter the encoding used by setting the system property "jna.encoding".

You should also eliminate the possibility that "LPCSTR" is actually an incorrect type; if the function is expecting a buffer it can write to, String will not work, and if it's actually LPTCSTR and you're using UNICODE, then you need to pass a WString instead.

满意归宿 2024-10-09 12:44:26

您是否尝试过将其映射到字节数组,如下所示:

short DKT_INIT(byte [] nameAsByteArray);
//now you should be able to obtain it like this:
System.out.println(new String(nameAsByteArray).trim());

Have you tried mapping it to a byte array, like this:

short DKT_INIT(byte [] nameAsByteArray);
//now you should be able to obtain it like this:
System.out.println(new String(nameAsByteArray).trim());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文