10

I am new to NSIS, and I need to know that in the uninstaller, how I can check if the application (which is in C++) is running and close it before uninstalling.

Mahkameh
  • 101
  • 1
  • 1
  • 3

4 Answers4

13

Here is a slightly more friendly version for using NSProcess that requests the app to close rather than terminates it (Owen's answer)

${nsProcess::FindProcess} "${APP_EXE}" $R0

${If} $R0 == 0
    DetailPrint "${AppName} is running. Closing it down"
    ${nsProcess::CloseProcess} "${APP_EXE}" $R0
    DetailPrint "Waiting for ${AppName} to close"
    Sleep 2000  
${Else}
    DetailPrint "${APP_EXE} was not found to be running"        
${EndIf}    

${nsProcess::Unload}
fiat
  • 15,501
  • 9
  • 81
  • 103
7

Use the NsProcess plugin. Download it here -> NSProcess
How to use it? As simple as:

${nsProcess::KillProcess} "${APP_EXE}" $R4

where APP_EXE is the name of your application...

The download will also tell you how to use it... :)

juergen d
  • 201,996
  • 37
  • 293
  • 362
Owen
  • 4,063
  • 17
  • 58
  • 78
2

Depending on the application, you have a couple of choices:

  • If your application has a window with a somewhat unique class name, you could use FindWindow
  • If your application creates a named kernel object (Mutex etc) you can check for it by calling the correct native win32 API with the system plugin
  • Use a 3rd party plugin like FindProcDLL
Anders
  • 97,548
  • 12
  • 110
  • 164
  • FindProcDLL won't work with the last version of NSIS as of this comment, 2.46, released in 2009. – Rex Dec 12 '11 at 11:45
  • 5
    Another option is this: `ExecWait TaskKill /IM program_name /F` – juergen d Mar 23 '12 at 12:36
  • +1 for [FindWindow](https://nsis.sourceforge.io/Docs/Chapter4.html#findwindow), as it is lightweight (no plugin, no direct syscalls) and "just works" for simple cases. – ojdo Sep 16 '19 at 13:16
1

Just make sure that the first thing that install or un-install does is to delete all xyz.tmp files in %TEMP (or any other app writable directory) before the below for loop runs. No plugins required.

!macro IsRunning 
  ExecWait "cmd /c for /f $\"tokens=1,2$\" %i in ('tasklist') do (if /i %i EQU xyz.exe fsutil file createnew $TEMP\xyz.tmp 0)"
  IfFileExists $TEMP\xyz.tmp 0 notRunning
   ;we have atleast one main window active
   MessageBox MB_OK|MB_ICONEXCLAMATION "XYZ is running. Please close all instances and retry." /SD IDOK
   Abort
  notRunning:
!macroEnd
Neelabh Mam
  • 300
  • 6
  • 10