import com.sun.jna.platform.win32.User32; import com.sun.jna.platform.win32.WinDef; . . . //execute process Process p = Runtime.getRuntime().exec(" \"c:/.../ExternalAppforWin.exe\""); //wait for return value int res = p.waitFor(); //if we have a valid return code begin waiting for window to be closed if(res == 0) { //define a window handle variable WinDef.HWND windowHandle = null; do { //sleep a little while before polling the value try{Thread.sleep(100);}catch(InterruptedException e){} //try to fetch the window by title windowHandle = User32.INSTANCE.FindWindow(null, "<Window Title>"); //if the handle is not null, the window is still open so sleep and then try try again }while(windowHandle != null && windowHandle.getPointer() != Pointer.NULL); //continue on with your code }
mouseEventListener.mouseMove(new Point(lParam.pt.x, lParam.pt.y)); } else if (code == WM_MOUSEWHEEL) { long hwndValue = Pointer.nativeValue(lParam.hwnd.getPointer()); long hwndValue = Pointer.nativeValue(lParam.hwnd.getPointer());
public WinLockListener() { // define new window class final WString windowClass = new WString("MyWindowClass"); final WinDef.HMODULE hInst = Kernel32.INSTANCE.GetModuleHandle(""); WinUser.WNDCLASSEX wClass = new WinUser.WNDCLASSEX(); wClass.hInstance = hInst; wClass.lpfnWndProc = WinLockListener.this; wClass.lpszClassName = windowClass.toString(); // register window class User32.INSTANCE.RegisterClassEx(wClass); getLastError(); // create new window final WinDef.HWND hWnd = User32.INSTANCE.CreateWindowEx(User32.WS_EX_TOPMOST, windowClass.toString(), "'hidden helper window to catch Windows events", 0, 0, 0, 0, 0, null, // WM_DEVICECHANGE contradicts parent=WinUser.HWND_MESSAGE null, hInst, null); getLastError(); Log.debug("window sucessfully created! window hwnd: " + hWnd.getPointer().toString()); Wtsapi32.INSTANCE.WTSRegisterSessionNotification(hWnd, Wtsapi32.NOTIFY_FOR_THIS_SESSION); WinUser.MSG msg = new WinUser.MSG(); while (User32.INSTANCE.GetMessage(msg, hWnd, 0, 0) != 0) { User32.INSTANCE.TranslateMessage(msg); User32.INSTANCE.DispatchMessage(msg); } /// This code is to clean at the end. You can attach it to your custom application shutdown listener Wtsapi32.INSTANCE.WTSUnRegisterSessionNotification(hWnd); User32.INSTANCE.UnregisterClass(windowClass.toString(), hInst); User32.INSTANCE.DestroyWindow(hWnd); Log.debug("program exit!"); }