I'm working on an NSIS installer, and trying to check if a certain application is running before uninstalling. So, I use kernel32::CreateMutexA call. Here is the chunk:

System::Call 'kernel32::CreateMutexA(i 0, i 0, t "cmd.exe") i .r1 ?e' Pop $R0 StrCmp $R0 0 +3 MessageBox MB_USERICON "The application is already running." Abort 

I put it into un.onInit. Trouble is, the process (cmd.exe here) is never detected.

Did I miss something?

Tx.

2

4 Answers

I found a simple solution; using FindProcDLL plugin.

So:

FindProcDLL::FindProc "cmd.exe" Pop $R0 StrCmp $R0 0 +3 MessageBox MB_USERICON "The application is already running." IDOK Abort 

P.S. FindProcDLL.dll must be copied into /Plugins.

1

All you are doing is creating a mutex with the global name "cmd.exe". From the MSDN article for CreateMutex:

If lpName matches the name of an existing event, semaphore, waitable timer, job, or file-mapping object, the function fails and the GetLastError function returns ERROR_INVALID_HANDLE. This occurs because these objects share the same name space.

So unless cmd.exe creates a handle to one of those types of objects with the name "cmd.exe", this call will simply create a new mutex with that name and return you the (non-erronous) handle.

You're probably using the wrong Win32 API function. Your CreateMutex tries to create a named mutex "something.exe". If there isn't one with that name it will succeed, so unless the process you're trying to check is creating a mutex with that name, you won't get the result you're after.

What you want is probably to enumerate all running processes and see if the one you're after is there. You can do this with ToolHelp32 from Win32 API - see sample here. I don't know how easy it will be to convert it to 'pure' NSIS so you might want to write a DLL plugin, or check if there's an existing solution floating around the NSIS community.

4

For this kind of thing, I've used either the KillProcess or Find-Close-Terminate plugins for NSIS - see here

Documentation is pretty straightforward, hopefully this does what you need - with a fairly minimal overhead.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.