原文鏈接 http://www.flighty.cn/html/bushu/20110402_115.html
如果我們要安裝或升級的程序正在運行,文件肯定會替換失敗,以下代碼可以提示用戶結束正在運行的程序。
需要使用插件FindProcDLL.dll,下載路徑:http://nsis.sourceforge.net/FindProcDLL_plug-in
http://files.cnblogs.com/files/z5337/FindProc.zip
http://files.cnblogs.com/files/z5337/KillProcDll%26FindProcDll.zip
開始安裝時檢查flighty.exe是否正在運行:
Function .onInit ;關閉進程 Push $R0 CheckProc: Push "flighty.exe" ProcessWork::existsprocess Pop $R0 IntCmp $R0 0 Done MessageBox MB_OKCANCEL|MB_ICONSTOP "安裝程序檢測到 ${PRODUCT_NAME} 正在運行。$\r$\n$\r$\n點擊 “確定” 強制關閉${PRODUCT_NAME},繼續安裝。$\r$\n點擊 “取消” 退出安裝程序。" IDCANCEL Exit Push "flighty.exe" Processwork::KillProcess Sleep 1000 Goto CheckProc Exit: Abort Done: Pop $R0 FunctionEnd
開始卸載時檢查flighty.exe是否正在運行:
Function un.onInit MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 "您確實要完全移除 $(^Name) ,及其所有的組件?" IDYES +2 Abort ;檢測程序是否運行 FindProcDLL::FindProc "flighty.exe" Pop $R0 IntCmp $R0 1 0 no_run MessageBox MB_ICONSTOP "卸載程序檢測到 ${PRODUCT_NAME} 正在運行,請關閉之后再卸載!" Quit no_run: FunctionEnd
順便提一句:如果你的程序被360誤殺過,可以用這個提示用戶關閉360才可以進行安裝。