Solved

How to determine which process created a named mutex?

Posted on 2002-04-16
9
530 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now