Sysinternals Homepage
Forum Home Forum Home > Windows Discussions > Development
  New Posts New Posts RSS Feed - Close Mutex of another process
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Close Mutex of another process

 Post Reply Post Reply Page  123 6>
Author
Message
hot.dok View Drop Down
Newbie
Newbie


Joined: 12 June 2008
Status: Offline
Points: 22
Post Options Post Options   Thanks (0) Thanks(0)   Quote hot.dok Quote  Post ReplyReply Direct Link To This Post Topic: Close Mutex of another process
    Posted: 12 June 2008 at 6:40am
Hi,
I need to close mutex created by another appication. Here is my code:

bReturnVal = CreateProcess(
            NULL,
            "C:\\Program Files\\Skype\\Phone\\Skype.exe",
            NULL,
            NULL,
            FALSE,
            dwCreationFlags,
            NULL,
            NULL,
            &si,
            &process_info);

    if (bReturnVal = 0)
    {
        return 1;
    }

    Sleep(4000);

    HANDLE skypeOriginalMutex = OpenMutex( MUTEX_ALL_ACCESS, false, "SkypeMutex" );
    if ( skypeOriginalMutex != NULL)
    {
        HANDLE res;
        DuplicateHandle(process_info.hProcess, skypeOriginalMutex, GetCurrentProcess(), &res, 0, false, DUPLICATE_CLOSE_SOURCE);
        CloseHandle(res);
        GetLastErrorLoc();
        MessageBox(NULL, "Mutex released", "WOW", MB_OK);
    }

The problem is that original handle is not closed. Please explain how can I close it?
Regards
Back to Top
molotov View Drop Down
Moderator Group
Moderator Group
Avatar

Joined: 04 October 2006
Status: Offline
Points: 17516
Post Options Post Options   Thanks (0) Thanks(0)   Quote molotov Quote  Post ReplyReply Direct Link To This Post Posted: 12 June 2008 at 7:15am
Hi hot.dok,
 
Just at a first glance... what does DuplicateHandle return?  If failure, what does GetLastError() return?
 
Look at the doc for the hSourceHandle param:
Quote hSourceHandle [in]
The handle to be duplicated. This is an open object handle that is valid in the context of the source process. ...


Edited by molotov - 12 June 2008 at 7:17am
Daily affirmation:
net helpmsg 4006
Back to Top
ring0 View Drop Down
Newbie
Newbie
Avatar

Joined: 09 June 2008
Location: India
Status: Offline
Points: 18
Post Options Post Options   Thanks (0) Thanks(0)   Quote ring0 Quote  Post ReplyReply Direct Link To This Post Posted: 12 June 2008 at 7:31am
Cheers
- Ring0
Back to Top
hot.dok View Drop Down
Newbie
Newbie


Joined: 12 June 2008
Status: Offline
Points: 22
Post Options Post Options   Thanks (0) Thanks(0)   Quote hot.dok Quote  Post ReplyReply Direct Link To This Post Posted: 12 June 2008 at 7:40am
molotov,
DuplicateHandle returns non zero value. And getlasterror return 0. The handle is connected to my process but not removed from skype.exe

ring0,
will check out

thanks you guys
Back to Top
molotov View Drop Down
Moderator Group
Moderator Group
Avatar

Joined: 04 October 2006
Status: Offline
Points: 17516
Post Options Post Options   Thanks (0) Thanks(0)   Quote molotov Quote  Post ReplyReply Direct Link To This Post Posted: 12 June 2008 at 7:52am
It looks like you duplicated and closed a handle in Skype.exe, that had the vaue returned by OpenMutex in your process.  (In other words, it looks like you're duping / closing some random handle in skype.exe.)
Daily affirmation:
net helpmsg 4006
Back to Top
hot.dok View Drop Down
Newbie
Newbie


Joined: 12 June 2008
Status: Offline
Points: 22
Post Options Post Options   Thanks (0) Thanks(0)   Quote hot.dok Quote  Post ReplyReply Direct Link To This Post Posted: 12 June 2008 at 7:57am
The handle is duplicated to my process but not closed on skype.exe. But with process explorer I can close the skype handle with no problem. Thus I such technique exists but possibly I'm on the wrong way.
Back to Top
hot.dok View Drop Down
Newbie
Newbie


Joined: 12 June 2008
Status: Offline
Points: 22
Post Options Post Options   Thanks (0) Thanks(0)   Quote hot.dok Quote  Post ReplyReply Direct Link To This Post Posted: 12 June 2008 at 7:59am
One more thing. Due to MSDN:
DUPLICATE_CLOSE_SOURCE - Closes the source handle. This occurs regardless of any error status returned. So the skype mutex have to be closed but it is not.
Back to Top
molotov View Drop Down
Moderator Group
Moderator Group
Avatar

Joined: 04 October 2006
Status: Offline
Points: 17516
Post Options Post Options   Thanks (0) Thanks(0)   Quote molotov Quote  Post ReplyReply Direct Link To This Post Posted: 12 June 2008 at 8:02am
Quote (In other words, it looks like you're duping / closing some random handle in skype.exe.)
 
Verify using PE that the handle you duplicate, is the handle you expect to be duplicating (the named mutex "SkypeMutex").
 
Say OpenMutex in your process returns 0x7c0.  You pass hSkype, 0x7c0, GetCurrentProcess(), &hDup to DuplicateHandle.  But, in hSkype, 0x7c0 was a handle to a registry key.  You've just duped that registry key handle in your process and closed it in Skype.  You need to get the source handle in skype.exe, not your process.
Daily affirmation:
net helpmsg 4006
Back to Top
hot.dok View Drop Down
Newbie
Newbie


Joined: 12 June 2008
Status: Offline
Points: 22
Post Options Post Options   Thanks (0) Thanks(0)   Quote hot.dok Quote  Post ReplyReply Direct Link To This Post Posted: 12 June 2008 at 8:16am
The mutex that is attached to my process after duplicatehandle executed is "SkypeMutex". Also I see that reference count to SkypeMutex in skype.exe process is incremented. That's the only ways I can verify it's needed handle.
Back to Top
hot.dok View Drop Down
Newbie
Newbie


Joined: 12 June 2008
Status: Offline
Points: 22
Post Options Post Options   Thanks (0) Thanks(0)   Quote hot.dok Quote  Post ReplyReply Direct Link To This Post Posted: 12 June 2008 at 8:17am
Also when I do OpenMutex it gets existing mutex created by skype. If skype is not run it returns error.
Back to Top
 Post Reply Post Reply Page  123 6>
  Share Topic   

Forum Jump Forum Permissions View Drop Down