简体   繁体   English

JNA在Windows平台上映射LPCSTR

[英]JNA mapping LPCSTR on windows platform

I am working on call one DLL api for C/C++ with JNA. 我正在使用JNA调用C / C ++的一个DLL API。 The function API in DLL is short DKT_init(LPCSTR name). DLL中的函数API是DKT_init(LPCSTR名称)的缩写。 I made the corresponding java method as public short DKT_init(String name); 我将相应的java方法设置为public short DKT_init(String name); But when I call it, the DLL API return a parameter error. 但是当我调用它时,DLL API返回一个参数错误。 I wonder how to map LPCSTR in JNA? 我想知道如何在JNA中映射LPCSTR? As LPCSTR is cons char * but String is char *. 由于LPCSTR是cons char *,而String是char *。

String is the appropriate mapping for LPCSTR. 字符串是LPCSTR的适当映射。 JNA will convert the modified UTF16 characters into a NUL-terminated buffer of bytes using the default platform encoding. JNA将使用默认平台编码将修改后的UTF16字符转换为NUL终止的字节缓冲区。

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, eg 您可以尝试传入一个显式字节数组来代替(使用上面建议的替代方法映射),这样可以消除出现编码错误的可能性,例如

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

You can alter the encoding used by setting the system property "jna.encoding". 您可以通过设置系统属性“ jna.encoding”来更改使用的编码。

You should also eliminate the possibility that "LPCSTR" is actually an incorrect type; 您还应该消除“ LPCSTR”实际上是错误类型的可能性; 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. 如果函数期望它可以写入的缓冲区,则String将不起作用,并且如果它实际上是LPTCSTR并且您正在使用UNICODE,则需要传递WString来代替。

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());

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

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