NSIS制作的安装包杀掉正在运行进程的方法

当我们在卸载程序或者更新安装程序时,我们需要先将运行的进程杀掉再进行安装或者卸载程序。NSIS本身并没有提供杀掉进程的脚本,但是有一个插件可以实现此功能,插件的名字叫KillProcDLL,插件下载地址为http://nsis.sourceforge.net/KillProcDLL_plug-in。插件的用法请参考插件页面所提供的内容。下面给出在安装包中的代码:



;This callback will be called when the installer is nearly finished initializing. If the '.onInit' function calls Abort, the installer will quit instantly
Function .onInit
KillProcDLL::KillProc "myprogram.exe"
FunctionEnd


;This callback is called when the user hits the 'cancel' button after the install has failed (if it could not extract a file, or the install script used the Abort command).
Function .onInstFailed
ExecShell "open" "$INSTDIRmyprogram.exe"
FunctionEnd


;This callback will be called when the uninstaller is nearly finished initializing. If the 'un.onInit' function calls Abort, the uninstaller will quit instantly. Note that this function can verify and/or modify $INSTDIR if necessary.
Function un.onInit
  MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 "Are you sure you want to uninstall $(^Name) and its componments?" IDYES NoAbort IDYES +2
  Abort
NoAbort:
KillProcDLL::KillProc "myprogram.exe"
FunctionEnd




  • .onInit为安装程序启动时触发的事件,此处杀掉进程是防止更新安装时不能更新主程序的问题
  • .onInstFailed为安装失败时触发的事件,此处启动程序文件是为防止安装失败后本该运行着的程序因为前面被杀掉不能继续运行
  • un.onInit为初始化卸载过程时触发的事件,在此事件的对话框选Yes的时候,即确认要卸载的时候跳转到NoAbort并杀掉进程,选No的时候不做任何操作。

  本文中的方法仅供参考,在使用过程中请自行修改此脚本。


转载原文地址:http://bcoder.com/others/kill-process-when-install-or-uninstall-programs-by-the-package-made-by-nsis

猜你喜欢

转载自blog.csdn.net/niejiangshuai/article/details/77528760