Java JNA UCHAR和PUCHAR [英] Java JNA UCHAR and PUCHAR
问题描述
我正在寻找Java字符串到' WlanHostedNetworkSetSecondaryKey '和' WlanHostedNetworkSetProperty '.一个人想要一个带有CHAR []的结构中的一个结构,而第一个想要一个PUCHAR.我尝试使用 String,char [],byte []和Memory ,但是它们将继续产生相同的错误(错误的参数或错误的概要文件).可以通过JNA调试更多方法(可能不是:()吗? 除了不是ANSI,我在任何地方都看不到所使用的characterencoding.任何帮助都将非常有用!
I'm looking to get a Java string to 'WlanHostedNetworkSetSecondaryKey' and 'WlanHostedNetworkSetProperty'. One wants a struct within a struct with a CHAR[] and the first one wants a PUCHAR. I tried using String, char[], byte[] and Memory, but they will keep producing me the same errors (Bad parameters or Bad profile something for the first). Any way maybe to debug more with JNA (probably not :()? I also can't read anywhere the characterencoding which is used, except that it's not ANSI.. Any help would be great!
* DWORD WINAPI WlanHostedNetworkSetSecondaryKey(
__in HANDLE hClientHandle,
__in DWORD dwKeyLength,
__in PUCHAR pucKeyData,
__in BOOL bIsPassPhrase,
__in BOOL bPersistent,
__out_opt PWLAN_HOSTED_NETWORK_REASON pFailReason,
__reserved PVOID pvReserved
* DWORD WINAPI WlanHostedNetworkSetProperty(
__in HANDLE hClientHandle,
__in WLAN_HOSTED_NETWORK_OPCODE OpCode,
__in DWORD dwDataSize,
__in PVOID pvData,
__out_opt PWLAN_HOSTED_NETWORK_REASON pFailReason,
__reserved PVOID pvReserved
);
有关此文档的更多信息
http://msdn.microsoft.com /en-us/library/dd439496(v=VS.85).aspx
http://jna.java.net/javadoc/overview-summary. html#pointers
http://en.wikipedia.org/wiki/Java_Native_Access
以下评论:
String buffer = "test";
ByteBuffer buf = ByteBuffer.allocateDirect(buffer.length()); buf.put(buffer.getBytes());
Pointer pucKeyData = Native.getDirectBufferPointer(buf);
System.out.println(
CLibrary.INSTANCE.WlanHostedNetworkSetSecondaryKey(handle.getValue(), 5, pucKeyData, 0, 0, reason, reserved));
推荐答案
我要重述到目前为止的评论(加上一些更正):
I am rephrasing what I have commented so far (plus some corrections):
UCHAR
定义为C宏u_byte
,它是unsigned byte
.但是,在Java中,我们没有unsigned byte
类型,只有byte
类型的带符号字节.不用担心,要获取Java中的unsigned byte
,请使用以下技巧:((int)mybyte & 0xFF)
UCHAR
is defined as a C macro u_byte
which is an unsigned byte
. But, in Java, we don't have an unsigned byte
type, just a signed byte from the byte
type. Don't worry, to get an unsigned byte
in Java, we use this trick: ((int)mybyte & 0xFF)
PUCHAR
被定义为C宏POINTER(u_byte)
,它是C指针,unsigned byte *
指向无符号字节数组.原因是有一个动态数组.
PUCHAR
is defined as a C macro POINTER(u_byte)
which is a C pointer, unsigned byte *
that points to an unsigned byte array. The reason is to have a dynamic array.
但是,如果在JNA Structure
中使用byte[]
或char[]
,则JNA会抱怨,Array fields must be initialized
表示未初始化 byte[]
或char[]
字段.在您的情况下,它违背了使用dwKeyLength
字段定义动态无符号字节数组pucKeyData
的大小的目的.
But, if you use byte[]
or char[]
in JNA Structure
, JNA will complain, Array fields must be initialized
for uninitialized byte[]
or char[]
field. In your case, it defeats the purpose of having dwKeyLength
field to define the size of dynamic unsigned byte array pucKeyData
.
pucKeyData
的正确JNA类型是Pointer
.但是,您需要找到一种方法,根据 WlanHostedNetworkSetSecondaryKey 结构中dwKeyLength
长度给出的大小,为pucKeyData
字段分配一个数组给Pointer
字段.
The right JNA type for the pucKeyData
is Pointer
. But, you need to find a way to assign an array to this Pointer
field for pucKeyData
field based on the size given by dwKeyLength
length in WlanHostedNetworkSetSecondaryKey structure.
要将初始化的无符号字节数组分配给Pointer
,我们需要使用直接ByteBuffer
.请记住,在使用后直接释放该直接缓冲区,因为它不再由Java GC管理...
To assign an initialized unsigned byte array to a Pointer
, we need to use direct ByteBuffer
. Remember to release this direct buffer manually after use since it is no longer managed by Java GC...
String buffer = "1234567890";
DWORD dwKeyLength = new DWORD(buffer.length());
ByteBuffer buf = ByteBuffer.allocateDirect(dwKeyLength.intValue());
buf.put(buffer.getBytes());
Pointer pucKeyData = Native.getDirectBufferPointer(buf);
System.out.println("pucKeyData data:" + pucKeyData.getString(0));
buf = null;
pucKeyData = null;
如果您从JNA异常中获取了错误的参数,则意味着您的JNA方法的一个或多个参数使用了错误的数据类型.
If you get bad parameters from JNA exception, it means one or more parameters of your JNA method are using incorrect data types.
JNA提供了一些WinDef类类型,例如DWORD
.但是不包括诸如PUCHAR
的一些WinDef类型.但是,要将整数值分配给DWORD
类型并取回它,您需要执行以下操作:
JNA provides a few WinDef class types like DWORD
. But a few WinDef types like PUCHAR
are not included. But, to assign an integer value to a DWORD
type and retrieve it back, you need to do like this:
DWORD dwKeyLength = new DWORD(5);
System.out.println("dwKeyLength integer value: " + dwKeyLength.intValue());
注意:这篇文章基于JNA平台版本3.3.0
Note: this post is based on JNA platform version 3.3.0
这篇关于Java JNA UCHAR和PUCHAR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!