From 7691c276426860cc0c9ec8274949b5596b6a46de Mon Sep 17 00:00:00 2001 From: "Hong Jen Yee (PCMan)" Date: Tue, 2 Feb 2016 13:33:19 +0800 Subject: [PATCH] Wait for the termination of existing server process before uninstallation. --- PIMELauncher/PIMELauncher.cpp | 11 ++++++++++- installer/installer.nsi | 8 ++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/PIMELauncher/PIMELauncher.cpp b/PIMELauncher/PIMELauncher.cpp index 0bb609e13..87cb11ab4 100644 --- a/PIMELauncher/PIMELauncher.cpp +++ b/PIMELauncher/PIMELauncher.cpp @@ -129,7 +129,16 @@ int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hprev, LPSTR cmd, int show) { server_hwnd = FindWindow(wnd_class_name, NULL); // find existing instance if (server_hwnd) { if (quit) { // ask the existing instance to terminate by closing its window - SendMessage(server_hwnd, WM_CLOSE, 0, 0); + DWORD pid; + HANDLE process = INVALID_HANDLE_VALUE; + if (GetWindowThreadProcessId(server_hwnd, &pid)) { + process = OpenProcess(SYNCHRONIZE, FALSE, pid); + } + PostMessage(server_hwnd, WM_CLOSE, 0, 0); // post a request to close the window + if (process != INVALID_HANDLE_VALUE) { + WaitForSingleObject(process, 10000); // wait for the existing instance to terminate + CloseHandle(process); + } } return 0; // only one instance of PIME launcher is allowed } diff --git a/installer/installer.nsi b/installer/installer.nsi index cd3a79c27..a689c53cf 100644 --- a/installer/installer.nsi +++ b/installer/installer.nsi @@ -35,7 +35,7 @@ AllowSkipFiles off ; cannot skip a file ; icons of the generated installer and uninstaller !define MUI_ICON "${NSISDIR}\Contrib\Graphics\Icons\orange-install.ico" !define MUI_UNICON "${NSISDIR}\Contrib\Graphics\Icons\orange-uninstall.ico" -!define PRODUCT_VERSION "git" +!define PRODUCT_VERSION "0.03" !define PRODUCT_UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\PIME" !define HOMEPAGE_URL "https://github.com/EasyIME/" @@ -186,6 +186,11 @@ LangString DESC_SecMain ${LANG_ENGLISH} "A test section." ; What's this?? ;Uninstaller Section Section "Uninstall" + ; Try to terminate running PIMELauncher and the server process + ; Otherwise we cannot replace it. + ExecWait '"$INSTDIR\PIMELauncher.exe" /quit' + Delete "$INSTDIR\PIMELauncher.exe" + ; Unregister COM objects (NSIS UnRegDLL command is broken and cannot be used) ExecWait '"$SYSDIR\regsvr32.exe" /u /s "$INSTDIR\x86\PIMETextService.dll"' ${If} ${RunningX64} @@ -197,7 +202,6 @@ Section "Uninstall" RMDir /r "$INSTDIR\x86" RMDir /r "$INSTDIR\server" RMDIR /r "$INSTDIR\python" - Delete "$INSTDIR\PIMELauncher.exe" DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\PIME" DeleteRegValue HKLM "Software\Microsoft\Windows\CurrentVersion\Run" "PIMELauncher"