cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
ritulranjan
Level 5

How to detect running application using CreateMutex() ???

Hi,

I am trying to detect if an application is currently running, through the mutex created by that application.

Following is the Installscript code I am using:
function OnBegin()
BOOL bReturn;
NUMBER nError, hMutex;
begin
hMutex = Kernel32.CreateMutex(NULL, FALSE, "MY_TEST_MUTEX");
nError = GetLastError();
if (hMutex != 0 && nError == 183) then
MessageBox("MY_TEST_MUTEX found, TestApp running!", 0);
endif;
if (hMutex != 0) then
Kernel32.CloseHandle(hMutex);
endif;
end;

However, the code doesn't really seem to help. The mutex always gets created. Using "global mutexes" (e.g. "Global\\MY_TEST_MUTEX") also doen't help. Interestingly enough, the equivalent C++ code works fine!

Any ideas why its not working? Am I doing something wrong here?

regards,
RR.
Labels (1)
0 Kudos
(9) Replies
Christopher_Pai
Level 16

I'm not really sure, I haven't coded a mutex since the IS5 days. I'd step through the debugger to see if a handle is being created and/or what nError is being set to.
0 Kudos
RobertDickau
Flexera Alumni

If nothing else, if the C++ code is working, you could build it into a DLL and call that from InstallScript...
0 Kudos
RobertDickau
Flexera Alumni

But for what it's worth, I've had luck with code like this (waiting for an MSI's Execute sequence to finish):
prototype Kernel32.OpenMutex(NUMBER, BOOL, BYVAL STRING);
prototype Kernel32.WaitForSingleObject(HWND, NUMBER);
prototype Kernel32.ReleaseMutex(HWND);

function OnBegin( )
HWND hMutex;
NUMBER result;
begin

hMutex = OpenMutex(READ_CONTROL | SYNCHRONIZE, FALSE, "_MSIExecute");

if (hMutex) then
MessageBox("Found it!", INFORMATION);
result = WaitForSingleObject(hMutex, INFINITE);

if (result==0) then
MessageBox("Done!", INFORMATION);
ReleaseMutex(hMutex);
endif;
else
MessageBox("Can't find it!", WARNING);
endif;

end;
0 Kudos
m_rudolph
Level 6

I've had good success with the function available at installsite.org: InstallScript samples > External programs and shell > List and Shut Down Running Applications.

It has a function that detects and another that shuts down.
0 Kudos
RobertDickau
Flexera Alumni

And if you know a particular executable file name, Is(FILE_LOCKED, "W:\\here\\file\\is.exe") can help.
0 Kudos
ritulranjan
Level 5

Christopher Painter wrote:
I'm not really sure, I haven't coded a mutex since the IS5 days. I'd step through the debugger to see if a handle is being created and/or what nError is being set to.


Hi Christopher,

Well, I did step through the debugger; the conclusion being - the mutex/handle gets created and nError remains ZERO.
0 Kudos
ritulranjan
Level 5

RobertDickau wrote:
If nothing else, if the C++ code is working, you could build it into a DLL and call that from InstallScript...


Ok, but if I make the DLL, shouldn't I take care of deploying it to some temporary location so that it can be used by InstallShield during run-time?

Pardon me, I am a little new to IS.
0 Kudos
Christopher_Pai
Level 16

Not needed. Type 1 CA's stored in the binary table automatically get streamed to a temp file for execution by Windows Installer.
0 Kudos
ritulranjan
Level 5

Using Is(FILE_LOCKED, "FILE PATH")

Thanks, Robert! 🙂
0 Kudos