What is the correct/preferred way to handle parameters of type PWSTR in JNA on Windows

62 views
Skip to first unread message

Kustaa Nyholm

unread,
Mar 13, 2016, 4:36:56 AM3/13/16
to jna-...@googlegroups.com
For example CM_Get_Device_ID:

https://msdn.microsoft.com/en-us/library/windows/hardware/ff538405(v=vs.85).aspx



CMAPI
CONFIGRET
WINAPI CM_Get_Device_ID(
_In_ DEVINST dnDevInst,
_Out_ PWSTR Buffer,
_In_ ULONG BufferLen,
_In_ ULONG ulFlags
);



My current mapping is

int CM_Get_Device_IDA(int dnDevInst, byte[] Buffer, int BufferLen, int ulFlags);

which works.

However this leaves open the question of character encoding for the returned string (Buffer)
and what happens if it contains wide characters (don't know if in this particular instance this
is worry).

I can of course use the CM_Get_Device_IDW

int CM_Get_Device_IDW(int dnDevInst, byte[] Buffer, int BufferLen, int ulFlags);

or perhaps:

int CM_Get_Device_IDW(int dnDevInst, char[] Buffer, int BufferLen, int ulFlags);

but what is the preferred way, I read that JNA handles wide ASCII/UNICODE conversion

automatically (awesome) but I can't figure out all the details, this must be documented
somewhere but I can't find it. I found this:

https://github.com/java-native-access/jna/issues/377

but it doesn't really clarify this for me. Maybe it is just me.

Am I correct that if I specify W32APIOptions.UNICODE_OPTIONS when loading the library
I can always map PWSTR to char[] if the function is writing that parameter or
use String if the function is just taking the string in or returns it?

So how do I turn this (char[]) into a String in the preferred way?

JNA Native has toString(char[]) function but some Windows API functions take
an oversize buffer and return the length of the string but there is no

Native.toString(char[] chars,int offset, int len)

Of course there is

String(char[] chars,int offset, int len)

but as no encoding is mentioned I'm left in doubt. I suppose JNA maybe
clever enough to handle the encoding (if it is an issue) when it
passes and retrieves the char[] parameters?


cheers Kusti









This e-mail may contain confidential or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden. We will not be liable for direct, indirect, special or consequential damages arising from alteration of the contents of this message by a third party or as a result of any virus being passed on or as of transmission of this e-mail in general.

Timothy Wall

unread,
Mar 13, 2016, 10:29:20 AM3/13/16
to jna-...@googlegroups.com
You can use `char[]` with `Native.toString(char[])` or `Memory` with `Pointer.getWideString(0)`. In either case, JNA will terminate the string when it finds a NUL terminator.

PWSTR is likely _always_ defined to `wchar_t*`, so you probably don’t have to worry about byte strings. When you’re using wchar_t, there is no encoding involved.

If you should happen upon a byte string that needs decoding, `Native.toString()` has a variant where you can provide an encoding name.
> --
> You received this message because you are subscribed to the Google Groups "Java Native Access" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to jna-users+...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages