Nsis安装或卸载程序时判断指定程序是否在运行

准备

下载NsProcess插件

版本1.6
插件地址
在这里插入图片描述

放置插件

  1. 打开Nsis安装目录;解压插件包;
  2. 插件包Include中的nsProcess.nsh移动到nsis的Include中
  3. 插件包Plugin中的nsProcess.dll移动到nsis的Plugins\x86-ansi中
  4. 插件包Plugin中的nsProcessW.dll移动到nsis的Plugins\x86-unicode中
  5. nsProcessW.dll名称修改为nsProcess.dll

使用

引入头文件

!include "nsProcess.nsh"

查询进程是否处于运行状态

nsProcess::_FindProcess "xxx.exe" 
查询到返回0

关闭进程

nsProcess::_KillProcess "xxx.exe"

Nsis回调函数

.onInit 安装时回调
un.onInit 卸载时回调

示例

注意:修改文件格式为UTF-8 BOM

!include "LogicLib.nsh"
!include "nsProcess.nsh"
Unicode true
Name "Guide"
OutFile "Basic.exe"
InstallDir $Desktop\Test

Section
    SetOutPath $INSTDIR
    File *.exe
    File *.dll
    File /r MonoBleedingEdge
    File /r UGUI_Data
    WriteUninstaller $INSTDIR\Uninstall.exe
    CreateDirectory "$SMPROGRAMS\A Test"
    CreateShortcut "$DESKTOP\UGUI.lnk" "$INSTDIR\UGUI.exe"
    CreateShortcut "$SMPROGRAMS\A Test\UGUI.lnk" "$INSTDIR\UGUI.exe"
    CreateShortcut "$SMPROGRAMS\A Test\Uninstall.lnk" "$INSTDIR\Uninstall.exe"

    WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\UGUI" "DisplayName" "UGUI"
    WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\UGUI" "UninstallString" "$INSTDIR\Uninstall.exe"
SectionEnd

Section "Uninstall"
    RMDIR /r $INSTDIR 
    RMDIR /r "$SMPROGRAMS\A Test"
    Delete "$DESKTOP\UGUI.lnk"
    DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\UGUI"
SectionEnd


Function .onInit
    StrCpy $1 "UGUI.exe"
    nsProcess::_FindProcess "$1" 
    Pop $R0
    ${
    
    If} $R0 = 0
      MessageBox MB_OK|MB_ICONSTOP "程序检测到应用正在运行,请关闭应用!" IDOK
      Abort
    ${
    
    EndIf}
FunctionEnd

Function un.onInit
    StrCpy $1 "UGUI.exe"
    nsProcess::_FindProcess "$1" 
    Pop $R0
    ${
    
    If} $R0 = 0
      MessageBox MB_OK|MB_ICONSTOP "程序检测到应用正在运行,请关闭应用!" IDOK
      Abort
    ${
    
    EndIf}
FunctionEnd

猜你喜欢

转载自blog.csdn.net/weixin_43796392/article/details/131130527