Search in sources :

Example 21 with LONG

use of com.sun.jna.platform.win32.WinDef.LONG 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)

Example 22 with LONG

use of com.sun.jna.platform.win32.WinDef.LONG in project intellij-community by JetBrains.

the class WinProcessManager method getProcessPid.

/**
   * Returns {@code pid} for Windows process
   * @param process Windows process
   * @return pid of the {@code process}
   */
public static int getProcessPid(Process process) {
    if (process.getClass().getName().equals("java.lang.Win32Process") || process.getClass().getName().equals("java.lang.ProcessImpl")) {
        try {
            long handle = ReflectionUtil.getField(process.getClass(), process, long.class, "handle");
            Kernel32 kernel = Kernel32.INSTANCE;
            WinNT.HANDLE winHandle = new WinNT.HANDLE();
            winHandle.setPointer(Pointer.createConstant(handle));
            return kernel.GetProcessId(winHandle);
        } catch (Throwable e) {
            throw new IllegalStateException(e);
        }
    } else {
        throw new IllegalStateException("Unknown Process implementation");
    }
}
Also used : WinNT(com.sun.jna.platform.win32.WinNT) Kernel32(com.sun.jna.platform.win32.Kernel32)

Aggregations

VARIANT (com.sun.jna.platform.win32.Variant.VARIANT)6 Pointer (com.sun.jna.Pointer)5 Date (java.util.Date)5 LONG (com.sun.jna.platform.win32.WinDef.LONG)4 Test (org.junit.Test)4 ComObject (com.sun.jna.platform.win32.COM.util.annotation.ComObject)3 DATE (com.sun.jna.platform.win32.OaIdl.DATE)3 SAFEARRAY (com.sun.jna.platform.win32.OaIdl.SAFEARRAY)3 BSTR (com.sun.jna.platform.win32.WTypes.BSTR)3 BYTE (com.sun.jna.platform.win32.WinDef.BYTE)3 CHAR (com.sun.jna.platform.win32.WinDef.CHAR)3 SHORT (com.sun.jna.platform.win32.WinDef.SHORT)3 IntByReference (com.sun.jna.ptr.IntByReference)3 Memory (com.sun.jna.Memory)2 COMException (com.sun.jna.platform.win32.COM.COMException)2 IConnectionPoint (com.sun.jna.platform.win32.COM.util.IConnectionPoint)2 VARIANT_BOOL (com.sun.jna.platform.win32.OaIdl.VARIANT_BOOL)2 VT_BSTR (com.sun.jna.platform.win32.Variant.VT_BSTR)2 VT_DATE (com.sun.jna.platform.win32.Variant.VT_DATE)2 FILETIME (com.sun.jna.platform.win32.WinBase.FILETIME)2