Determine whether the specified program is running when Nsis installs or uninstalls the program

Prepare

Download the NsProcess plugin

Version 1.6
plugin address
insert image description here

place plugin

  1. Open the Nsis installation directory; unzip the plug-in package;
  2. The nsProcess.nsh in the plug-in package Include is moved to the nsis Include
  3. nsProcess.dll in the plug-in package Plugin is moved to Plugins\x86-ansi of nsis
  4. nsProcessW.dll in the plugin package Plugin is moved to Plugins\x86-unicode of nsis
  5. nsProcessW.dll name changed to nsProcess.dll

use

import header file

!include "nsProcess.nsh"

Query whether the process is running

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

close process

nsProcess::_KillProcess "xxx.exe"

Nsis callback function

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

example

Note: Modify the file format to 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

Guess you like

Origin blog.csdn.net/weixin_43796392/article/details/131130527