Solved

How to determine which process created a named mutex?

Posted on 2002-04-16
9
539 Views
Last Modified: 2013-12-03
Is it possible to determine which process created a named mutex?

For example, let's say you have 5 applications (or perhaps 5 instances of the same app). Each one attempts to create a named mutex. Obviously only one can succeed. Is it possible to tell from another application which one of the 5 created the mutex? Is it possible to tell which (if any) has the mutex locked?

The number of apps/instances doesn't matter (just that it's more than one).
0
Comment
Question by:Melange
9 Comments
 
LVL 5

Accepted Solution

by:
robpitt earned 100 total points
ID: 6946226
If an application calls CreateMutex for a named mutex and that mutex already exists then a handle to the existing mutex is returned. This scenario can be detected by testing whether GetLastError()==ERROR_ALREADY_EXISTS

See the CreateMutex documentation for more info.

Does that help?
0
 
LVL 86

Expert Comment

by:jkr
ID: 6946452
There is no way to do that. I checked the NT Native API, and even NT does not keep track of that (IOW: 'ZwQueryObject()' does not provide any info about the creator of an object).
0
 
LVL 10

Expert Comment

by:makerp
ID: 6947274
Easy, as robpit said if the mutes already exists then it returns ERROR_ALREADY_EXISTS. So if you don’t get this back then you are the first process to call CreateMutex with that name. So in your code if this is the case set a bool to true indicating you created it. if you do get back ERROR_ALREADY_EXISTS set the bool to false. now using some sort of process communication you could ask each process if they created the mutex. alternativley using sockets broadcast a 'Who created the mutex' message, the client who did could send back a message with their process-id in (they would not have to send their IP as you can get that from the Recv call in the sockaddr_in struct). if all prcesses are on the same machine then have the mutex creator to write a process-id file to a known location on disk...

there is one serious limintaion of my suggestion, that limitation being you must have the source code for the applications so you can code this in.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 5

Expert Comment

by:robpitt
ID: 6947458
Yeah I assumed that he realy didn't need to know exactly which other process created the mutex... if you need that then as JKR says you need an additional interprocess mechanism (e.g. shared memory to which you could write a process id - see CreateFileMapping for info)
0
 

Expert Comment

by:dpodvaln
ID: 6980785
makerp : you not necessarily need a source code to do it. You may inject your dll into all processes and override CreateMutex function with something as :

shared_processId = GetProcessId()
OriginalCreateMutex();
this technique described in the Richman's book.
0
 
LVL 3

Author Comment

by:Melange
ID: 6980951
Yeah, I was specifically looking for which process created a mutex as opposed to if it existed. That's easy with OpenMutex.

Unfortunately in the situation I was looking for neither interprocess communication or dll injection would work. I was looking at this as a diagnostic tool to see why some apps were hanging or causing problems (that naturally seem to occur only at customer sites).

Dll injection wouldn't work because the mutex would already exist by the time I got to take a look at it. Interprocess communication wouldn't work because the app might not be responsive.

At least I may have indirectly figured out what the hangups were. Seems it wasn't even related to the mutex after all. But, it's one of those situations where I won't know if I succeeded in solving it or not unless it just doesn't happen again.

I'm still interested to see if there is a way to get this kind of diagnostic information. But, if the OS doesn't save the creating process id anywhere, then I guess it's not possible.
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question