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
);
For the most documentation on this
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
Following comment:
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):
UCHARis defined as a C macrou_bytewhich is anunsigned byte. But, in Java, we don’t have anunsigned bytetype, just a signed byte from thebytetype. Don’t worry, to get anunsigned bytein Java, we use this trick:((int)mybyte & 0xFF)PUCHARis defined as a C macroPOINTER(u_byte)which is a C pointer,unsigned byte *that points to an unsigned byte array. The reason is to have a dynamic array.But, if you use
byte[]orchar[]in JNAStructure, JNA will complain,Array fields must be initializedfor uninitializedbyte[]orchar[]field. In your case, it defeats the purpose of havingdwKeyLengthfield to define the size of dynamic unsigned byte arraypucKeyData.The right JNA type for the
pucKeyDataisPointer. But, you need to find a way to assign an array to thisPointerfield forpucKeyDatafield based on the size given bydwKeyLengthlength in WlanHostedNetworkSetSecondaryKey structure.To assign an initialized unsigned byte array to a
Pointer, we need to use directByteBuffer. Remember to release this direct buffer manually after use since it is no longer managed by Java GC…If you get bad parameters from JNA exception, it means one or more parameters of your JNA method are using incorrect data types.
JNA provides a few WinDef class types like
DWORD. But a few WinDef types likePUCHARare not included. But, to assign an integer value to aDWORDtype and retrieve it back, you need to do like this:Note: this post is based on JNA platform version 3.3.0