Kill mutex (mutant) of an other process

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
Rigel
Posts: 1
Joined: 26 Feb 2016, 09:09

Kill mutex (mutant) of an other process

26 Feb 2016, 10:11

Kill mutex of an other process is not so simple to do with script language. The solutions to do this operation are few and sometimes obsolete, the most famous one is to use Sysinternal Process Explorer, you find the handle of a mutex (mutant) and kill it manually!
Now I find an other solution you can merge with autohotkey or all the script languages, also a batch file, depends on what you need.

You need to use a command line software from Sysinternals, Handle.
First of all you need 3 info of the mutex:
- The name
- The process id, owner of that handle
- The hex code of the handle
If the name of the handle is for sure unique you can skip the process id

https://technet.microsoft.com/en-us/sys ... andle.aspx

Using Handle:
Handle original istruction from Sysinternal

- handle.exe -p 2080 -a _circledock_
Search for an handle called "_circledock_" where the owner is the process 2080, -a is used to show all types of handles, mutex type is not in normal view

- handle.exe -a _circledock_
Search for an handle called "_circledock_" in all the processes, -a is used to show all types of handles, mutex type is not in normal view

You will have a list like this:
Handle output
*More pid if you search with only the mutex name or more result with the same pid if the name is a fragment of the entire name


Now you need to get the standard output and isolate the handle hex code, the one between "type: Mutant" and :"\Sessions\1\BaseNamedObjects\_CircleDock_", path of the handle. If you have not used the pid you need to isolate also it.

Last operation, kill the mutex:
handle.exe -p 8308 -c 2C8 -y
-p pid
-c hex handle id
-y Don't prompt for close handle confirmation.

Here is my code "no pid version", I no need of a better code so is simple, written fast to do what I want, no more so the getHandle not work with both with and without pid and the can close handle when the result is only one, theoretically the regex can works only on the first row so can works :P theoretically XD

Code: Select all

closehandle("_circleDock_")

closehandle(name){
	pid := 0
	handle := getHandle(pid,name)
	if (handle = "") {
		return ; handle not found
	}
	command := "C:\Users\Astryd\Risorse\_CircleDocks\handle.exe -p " . pid . " -c " . handle . " -y"
	Run, %comspec% /c %command%,, Hide
	}    
}

getHandle(ByRef pid,name) {
	command := "C:\Users\Astryd\Risorse\_CircleDocks\handle.exe -a " . name
	stdout := runStdout(command)
	needle := "No matching" ;when Handle found nothing return in standard output "No matching handles found."
	IfInString, stdout, %needle%
		{
		return ""
		}	
	handle := RegExReplace(stdout, "s).*(...): \\Sessions\\1\\BaseNamedObjects\\_CircleDock_.*", "$1")
	pid := RegExReplace(stdout, "s).*pid: (\d*).*", "$1")
	Return handle
}

; Run a command and return standard output
runStdout(command) {
    shell := comobjcreate("wscript.shell")
    exec := (shell.exec(comspec " /c " command))
    stdout := exec.stdout.readall()   
    Return stdout
}

just me
Posts: 9219
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Kill mutex (mutant) of an other process

26 Feb 2016, 10:31

Why should I want to kill the poor mutant? ;)
Balem
Posts: 1
Joined: 18 Mar 2016, 07:18

Re: Kill mutex (mutant) of an other process

18 Mar 2016, 07:33

Hello Rigel
Your script work for me! Thanks man !
To be perfect, I would like to hide the command prompt, which is not happening.
I'm newbie, Can you help me?

Gratz!!
User avatar
masheen
Posts: 295
Joined: 06 Dec 2016, 14:10

Re: Kill mutex (mutant) of an other process

06 Dec 2016, 14:16

Hi Rigel plz help me i want to kill this how i cant to do this?


Image
Gold512
Posts: 2
Joined: 26 Jan 2021, 06:05

Re: Kill mutex (mutant) of an other process

26 Jan 2021, 06:11

is it possible to search a process for a mutex hex by its name?
Gold512
Posts: 2
Joined: 26 Jan 2021, 06:05

Re: Kill mutex (mutant) of an other process

27 Jan 2021, 06:08

Gold512 wrote:
26 Jan 2021, 06:11
is it possible to search a process for a mutex hex by its name?
oops nvm i didnt read the code properly

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 45 guests