Archive: detecting and closing previous instances


detecting and closing previous instances
Hi. I've searched about this in many places but couldn't find anything useful. So, how can I detect and close previous instances of my application from the NSIS installer, just like the Winamp installer does?
Thanks a lot.


Did you try it with FindWindowEx API?

There you can use your CLASS to dectect your Application?

Winamp uses FindWindow API with the parameter "Winamp v1.0" as the window title.

Maybe you should do that, too.

:)


FindWindow syntax:

FindWindow user_var(hwnd output) windowclass [windowtitle] [windowparent] [childafter]
Example:
FindWindow $0 "#32770" "" $HWNDPARENT

This works nicely:


!define Title "MapUpdater"
!define Version "v0.2b"

Function .onInit
## Check MapUpdater is not already running
Push $R0
FindWindow $R0 "" "${Title} ${Version}" $HWNDPARENT
StrCmp $R0 0 +3
MessageBox MB_OK|MB_ICONSTOP "${Title} is already running!"
Quit
Pop $R0
FunctionEnd


-Stu

If your application is a non-windowed application, use a combo of FindProc and KillProc.
FindProc will tell you if the application is running.
You can then inform the user and use KillProc to kill the process if necessary.


I read the warning in the KillProc description, and now I have a question: Why use TerminateProcess when ExitProcess sounds like it would be the prefered method?

Thanks.

Dave


Because it is guaranteed to kill the process.
ExitProcess may result in a deadlock.
See ExitProcess.