Checking if the application is running in NSIS before uninstalling
Asked Answered
D

4

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.

Deadwood answered 2/4, 2010 at 5:58 Comment(0)
A
14

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}
Anton answered 23/10, 2012 at 3:11 Comment(0)
A
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... :)

Alundum answered 14/4, 2010 at 3:38 Comment(1)
what does $R4 mean ?Elanorelapid
L
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
Ladylove answered 2/4, 2010 at 13:50 Comment(3)
FindProcDLL won't work with the last version of NSIS as of this comment, 2.46, released in 2009.Overarch
Another option is this: ExecWait TaskKill /IM program_name /FVanhook
+1 for FindWindow, as it is lightweight (no plugin, no direct syscalls) and "just works" for simple cases.Sloan
D
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
Datura answered 8/11, 2017 at 7:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.