Search in sources :

Example 1 with WNDENUMPROC

use of com.sun.jna.platform.win32.WinUser.WNDENUMPROC in project HearthStats.net-Uploader by HearthStats.

the class ProgramHelperWindows method getWindowHandle.

private HWND getWindowHandle() {
    // Cache the window handle for five seconds to reduce CPU load and (possibly) minimise memory leaks
    long currentTime = System.currentTimeMillis();
    if (currentTime < lastWindowsHandleCheck + 5000) {
        // It has been less than five seconds since the last check, so use the cached value
        return windowHandle;
    } else {
        debugLog.debug("Updating window handle ({}ms since last update)", currentTime - lastWindowsHandleCheck);
        lastWindowsHandleCheck = currentTime;
        windowHandle = null;
    }
    User32.INSTANCE.EnumWindows(new WNDENUMPROC() {

        @Override
        public boolean callback(HWND hWnd, Pointer arg1) {
            int titleLength = User32.INSTANCE.GetWindowTextLength(hWnd) + 1;
            char[] title = new char[titleLength];
            User32.INSTANCE.GetWindowText(hWnd, title, titleLength);
            String wText = Native.toString(title);
            if (wText.isEmpty()) {
                return true;
            }
            PointerByReference pointer = new PointerByReference();
            User32DLL.GetWindowThreadProcessId(hWnd, pointer);
            Pointer process = Kernel32.OpenProcess(Kernel32.PROCESS_QUERY_INFORMATION | Kernel32.PROCESS_VM_READ, false, pointer.getValue());
            Psapi.GetModuleBaseNameW(process, null, baseNameBuffer, STRING_BUFFER_LENGTH);
            String baseNameString = Native.toString(baseNameBuffer);
            // see https://github.com/JeromeDane/HearthStats.net-Uploader/issues/66#issuecomment-33829132
            User32.INSTANCE.GetClassName(hWnd, classNameBuffer, STRING_BUFFER_LENGTH);
            String classNameString = Native.toString(classNameBuffer);
            if (baseNameString.equals(processName) && classNameString.equals("UnityWndClass")) {
                windowHandle = hWnd;
                if (windowHandleId == null) {
                    windowHandleId = windowHandle.toString();
                    if (lastKnownWindowHandleId == null || lastKnownWindowHandleId != windowHandleId) {
                        // The window handle has changed, so try to find the location the HearthStats executable. This is used to
                        // find the HS log file. Only compatible with Windows Vista and later, so we skip for Windows XP.
                        lastKnownWindowHandleId = windowHandleId;
                        if (Environment.isOsVersionAtLeast(6, 0)) {
                            debugLog.debug("Windows version is Vista or later so the location of the Hearthstone is being determined from the process");
                            Kernel32.QueryFullProcessImageNameW(process, 0, processFileNameBuffer, lpdwSize);
                            String processFileNameString = Native.toString(processFileNameBuffer);
                            if (processFileNameString != null) {
                                int lastSlash = processFileNameString.lastIndexOf('\\');
                                hearthstoneProcessFolder = processFileNameString.substring(0, lastSlash);
                            }
                        }
                    }
                    _notifyObserversOfChangeTo("Hearthstone window found with process name " + processName);
                }
            }
            return true;
        }
    }, null);
    // notify of window lost
    if (windowHandle == null && windowHandleId != null) {
        _notifyObserversOfChangeTo("Hearthstone window with process name " + processName + " closed");
        windowHandleId = null;
    }
    return windowHandle;
}
Also used : HWND(com.sun.jna.platform.win32.WinDef.HWND) PointerByReference(com.sun.jna.ptr.PointerByReference) WNDENUMPROC(com.sun.jna.platform.win32.WinUser.WNDENUMPROC) Pointer(com.sun.jna.Pointer)

Aggregations

Pointer (com.sun.jna.Pointer)1 HWND (com.sun.jna.platform.win32.WinDef.HWND)1 WNDENUMPROC (com.sun.jna.platform.win32.WinUser.WNDENUMPROC)1 PointerByReference (com.sun.jna.ptr.PointerByReference)1