简体   繁体   中英

How can I get HWND from String?

I have many windows with same names, For example Calculator.

User32Extra.INSTANCE.FindWindow(null,"Calculator")

The above Script gives me HWND of first Calculator found.

I have used EnumWindows to find all the HWNDs.

final HashMap<HWND,String> hm=new HashMap<HWND,String>();  
User32.INSTANCE.EnumWindows(new User32.WNDENUMPROC() {

     @Override
     public boolean callback(Pointer hWnd, Pointer arg) {
         HWND hWnd2 = new HWND(hWnd);
        byte[] windowText = new byte[512];
        User32.INSTANCE.GetWindowTextA(hWnd, windowText, 512);
        String wText = Native.toString(windowText).trim();

        if (!wText.isEmpty() && User32.INSTANCE.IsWindowVisible(hWnd2) && wText.equals("Calculator")) {
            hm.put(hWnd2,wText);
        }
        return true;
     }
  }, null);

for(HWND hwnd:hm.keySet()){
   System.out.println(hwnd.toString());
}

The above script gives output as

native@0x20816

native@0x50362

native@0x1206ae

I don't have access of this HashMap in another program. I know only the String value of HWND. Is it possible to cast native@0x20816 String to HWND?

If so How can I do that? Please help..

If you are coding entirely in this Java program then it is pointless to convert the window handle to a text representation. Simply use the HWND value directly.

If in fact the issue is that you send the text to some other party, and need to obtain the numeric value of the window handle, then simply convert the text to a numeric value. I see little point in prefixing the text with native@ and the hexadecimal prefix 0x is probably also superfluous. The two parties can simple agree that the value will always be passed as hexadecimal. Presumably you know enough about Java and the other programming languages that are involved to be able to convert between numeric values and hexadecimal text.

String hwndString = "native@0xa021c";
HWND temp = new HWND();
temp.setPointer(new Pointer(Long.decode(hwndString.substring(7))));
if(User32.INSTANCE.IsWindow(temp)){
     //This is a valid window
}
else{
     // Invalid Window
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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