[JAVA] JNA 실행중인 윈도우 핸들 모두 얻기(EnumWindows)

[JAVA] JNA 실행중인 윈도우 핸들 모두 얻기(EnumWindows)

현재 실행중인 모든 윈도우 핸들의 정보를 출력하는 JAVA JNA 코드.

Github 주소 : https://github.com/thkmon/BBShowAllHwndInfos.git

package com.thkmon.bbwin.main;

import java.util.ArrayList;

import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinDef.RECT;
import com.sun.jna.platform.win32.WinUser.WNDENUMPROC;
import com.sun.jna.ptr.IntByReference;

public class BBShowAllHwndInfos {
    privatestatic ArrayList<String> hwndInfoList = null;

    publicstaticvoid main(String[] args) {
        try {
            System.out.println(
“[INFORMATION OF CURRENT WINDOW HANDLES]”);
            System.out.println(
“”);
            
            hwndInfoList =
new ArrayList<String>();
            
            BBShowAllHwndInfos instance =
new BBShowAllHwndInfos();
            instance.printAllHwndInformations();
            
            int count = hwndInfoList.size();
            for (int i=0; i<count; i++) {
                System.out.print(
“(“ + (i+1) + “/” + count + “) “);
                System.out.println(hwndInfoList.get(i));    
            }
            
            System.out.println(
“END”);

        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            // wait for 10 minutes after run
            Thread.sleep(1000 * 60 * 10);
        }
catch (Exception e) {
            // ignore
        }
    }

    privatevoid printAllHwndInformations() throws Exception {
        try {
            User32.INSTANCE.EnumWindows(new WNDENUMPROC() {
                publicboolean callback(HWND hwnd, Pointer arg1) {
                    char[] windowText =
new char[512];
                    User32.INSTANCE.GetWindowText(hwnd, windowText, 512);
                    String wText = Native.toString(windowText);

                    RECT rectangle = new RECT();
                    User32.INSTANCE.GetWindowRect(hwnd, rectangle);

                    // 숨겨져 있는 창은 제외하고 찾기
                    if (wText.isEmpty() || !(User32.INSTANCE.IsWindowVisible(hwnd))) {
                        returntrue;
                    }
                    
                    // 최소화 여부
                    boolean bMinimized = false;
                    if (rectangle.left <= -32000) {
                        bMinimized =
true;
                    }

                    // 핸들(hwnd)의 클래스 네임 가져오기
                    char[] c = new char[512];
                    User32.INSTANCE.GetClassName(hwnd, c, 512);
                    String clsName = String.valueOf(c).trim();

                    // 핸들(hwnd)의 pid 가져오기
                    IntByReference pidByRef = new IntByReference(0);
                    User32.INSTANCE.GetWindowThreadProcessId(hwnd, pidByRef);
                    int pid = pidByRef.getValue();

                    StringBuilder buff = new StringBuilder();
                    buff.append(
“PID : “).append(pid);
                    buff.append(
“\n”).append(“CLASSNAME : “).append(clsName);
                    buff.append(
” / “).append(“TEXT : “).append(wText);
                    buff.append(
“\n”).append(“MINIMIZED : “).append(bMinimized);
                    buff.append(
” / “).append(“POSITION : (“).append(rectangle.left).append(“,”).append(rectangle.top).append(“)”);
                    buff.append(
“~(“).append(rectangle.right).append(“,”).append(rectangle.bottom).append(“)”);
                    buff.append(
“\n”);
                    
                    hwndInfoList.add(buff.toString());
                    
                    returntrue;
                }
            }, null);

        } catch (Exception e) {
            throw e;
        }
    }
}

결과 예시

[INFORMATION OF CURRENT WINDOW HANDLES]

(1/4) PID : 8164
CLASSNAME : SWT_Window0 / TEXT : BBShowAllWindowInfos – BBShowWindowInfos/src/com/thkmon/bbwin/main/BBShowWindowInfos.java – Eclipse
MINIMIZED : false / POSITION : (-8,-8)~(1928,1054)

(2/4) PID : 10056
CLASSNAME : IEFrame / TEXT : 글쓰기, 흑곰의 유익한 블로그 : 네이버 블로그 – Internet Explorer
MINIMIZED : false / POSITION : (101,442)~(1576,1258)

(3/4) PID : 5496
CLASSNAME : ConsoleWindowClass / TEXT : C:\WINDOWS\system32\cmd.exe
MINIMIZED : false / POSITION : (156,156)~(1149,675)

(4/4) PID : 14240
CLASSNAME : Chrome_WidgetWin_1 / TEXT : Google 번역 – Chrome
MINIMIZED : true / POSITION : (-32000,-32000)~(-31801,-31966)

END