?
Solved

How to determine which process created a named mutex?

Posted on 2002-04-16
9
Medium Priority
?
549 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 5

Accepted Solution

by:
robpitt earned 400 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

762 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