1 message in net.java.dev.jna.users[jna-users] Re: about JNA encoding
FromSent OnAttachments
Michael Brewer-DavisOct 30, 2008 9:31 am 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:[jna-users] Re: about JNA encodingActions...
From:Michael Brewer-Davis (mich@tech4learning.com)
Date:Oct 30, 2008 9:31:55 am
List:net.java.dev.jna.users

Hi lefish,

I'm forwarding this response to the jna-users list as well--that's generally the best route to answers.

The default User32 implementation uses Unicode methods unless asked to do otherwise (see the W32API class). This means that it will use the underlying GetWindowTextW version of GetWindowText, which takes wchar_t* arguments.

To support this in your code, you need to treat the output as a Java char[], rather than byte[]. Two ways that might work: a) Add a function to User32 which takes a char[] argument instead of a byte[] argument. Then call it with: char[] buffer = new char[100]; user32.GetWindowText(hwnd, buffer, 100); String windowText = Native.toString(buffer);

b) Use the byte[] as buffer, but set the JNA encoding to UTF-16. System.setProperty("jna.encoding", "UTF-16"); // that may need to be "LittleEndianUnmarked" instead of "UTF-16"? ... byte[] buffer = new byte[100]; user32.GetWindowText(hwnd, buffer, 50); // note half the size, since the underlying function is using two bytes per character String windowText = Native.toString(buffer);

Hope that helps some. michael

bao bao wrote:

hey, sir, I know you from http://markmail.org/message/znrwrx3ggbviznmc#query:jna%20GetWindowText+page:1+mid:z6cfyrrzn2d2hjgy+state:results, now i met some questiones on JNA. I tried to use GetWindowText to get all windows, but when the window title is chinese, it will be confused, can you give me some suggestion, thanks. Regards, lefish

import com.sun.jna.Native; import com.sun.jna.Pointer; import com.sun.jna.examples.win32.User32; import com.sun.jna.examples.win32.User32.WNDENUMPROC; import com.sun.jna.examples.win32.W32API.HWND; import com.sun.jna.win32.StdCallLibrary; public class TestEnumWindows { public TestEnumWindows() { } public interface Kernel32 extends StdCallLibrary { Kernel32 INSTANCE = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class); } static void enumWindows() { //System.setProperty("jna.encoding", "ISO-8859-1"); user32.EnumWindows(new WNDENUMPROC() { @Override public boolean callback(HWND hWnd, Pointer data) { eCallBack(hWnd, data); return true; } }, null); } protected static void eCallBack(HWND hWnd, Pointer data) { try { prtWindowTitle(hWnd); } catch (Exception e) { e.printStackTrace(); } } private static void prtWindowTitle(HWND hWnd) throws Exception { byte[] b = new byte[200]; user32.GetWindowText(hWnd, b, 300); char[] c = new char[b.length]; int j = 0; for (int i = 0; i < b.length; i++) { if (b[i] != 0) { c[j] = (char) (b[i] & 0xff); j++; } } String s = Native.toString(c); if (s.indexOf("ppt") != -1 && s.indexOf("Java") != -1) { System.out.println(s); System.out.println(); for (int i = 0; i < c.length - 150; i++) { System.out.print(Integer.toHexString(c[i])); } System.out.println(); for (int i = 0; i < c.length - 150; i++) { System.out.print(Integer.toHexString(Character .reverseBytes(c[i]) >>> 8)); } System.out.println(); System.out.println(Native.toString(c)); } } public static void main(String[] args) { enumWindows(); } static void prtChar(String s) { // String s="Java与设计模式.ppt"; for (int i = 0; i < s.length(); i++) { System.out.print((int) (s.charAt(i)) + "\t"); } System.out.println(); } private static User32 user32 = User32.INSTANCE; }