Is this even possible? Check what spawned a process? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
JeezLouise

Is this even possible? Check what spawned a process?  Topic is solved

22 Apr 2018, 01:02

So far I learned thanks to forums and some documentation how to check if a process exists. I use:

Code: Select all

If ProcessExist("plantsched.exe")
	msgbox, plantsched.exe exists.
else
	msgbox, plantsched.exe is not running

ProcessExist(Name){
	Process,Exist,%Name%
	return Errorlevel
}
return
esc::exitapp
I would like to find a way to check what launched my exe (plantsched.exe), the name of the program / process.

In a program called Process Explorer it has a feature but it does not always work or work well for me. There, I can select my exe (plantsched.exe) right click, select properties and go to the image tab.

Anybody here can think of a way to do it in AHK in script form?

:thumbup: Thank you
JeezLouise

Re: Is this even possible? Check what spawned a process?

22 Apr 2018, 03:03

That code did not work for me, however, I found some other code and wondering if you can tell me how to test it.

I found it here https://autohotkey.com/boards/viewtopic ... 9115#p9115 credit to Wanker and Chef?

Code: Select all

/* 
	Credit to Wanker
	
	author 	: Chef
	link 	: http://ahkscript.org/boards/memberlist.php?mode=viewprofile&u=55319
	date 	: 14 January 2014

	description

		Process-related functions
*/	

process_property(pid, property)
{
	/*
		available properties:

			Caption
			CommandLine
			CreationClassName
			CreationDate
			CSCreationClassName
			CSName
			Description
			ExecutablePath
			ExecutionState
			Handle
			HandleCount
			InstallDate
			KernelModeTime
			MaximumWorkingSetSize
			MinimumWorkingSetSize
			Name
			OSCreationClassName
			OSName
			OtherOperationCount
			OtherTransferCount
			PageFaults
			PageFileUsage
			ParentProcessId
			PeakPageFileUsage
			PeakVirtualSize
			PeakWorkingSetSize
			Priority
			PrivatePageCount
			ProcessId
			QuotaNonPagedPoolUsage
			QuotaPagedPoolUsage
			QuotaPeakNonPagedPoolUsage
			QuotaPeakPagedPoolUsage
			ReadOperationCount
			ReadTransferCount
			SessionId
			Status
			TerminationDate
			ThreadCount
			UserModeTime
			VirtualSize
			WindowsVersion
			WorkingSetSize
			WriteOperationCount
			WriteTransferCount
		
		msdn
			
			http://msdn.microsoft.com/en-us/library/aa394372%28v=vs.85%29.aspx
	*/
	for process in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Process where ProcessId=" pid)
		return, process[property]
}

process_isFirstInstance()
{
	if !a_isCompiled
	{
		msgBox, 0x40010,% a_thisFunc "()",This function can only be used with compiled scripts!`n`n"%a_scriptName%"
		return
	}
	scriptPID := DllCall( "GetCurrentProcessId" )
	process, exist, %A_Scriptname%
	ifequal,scriptPID,%errorLevel%,return 1
}

process_instanceCount(name)
{
	for process in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Process where Name='" name "'")
		count := a_index
	return count
}
So far here is what I did but when I launch my exe from another exe I just get a blank msgbox instead of showing me the id of the exe that launched it. I suspect I am messing up this line badly

return, process[property] ;see my code below, dont laugh or cry please

Code: Select all

q::

WinGet, pidmy, PID, ahk_exe plantsched.exe   ; i added
spawnedby := process_property(pidmy, ParentProcessId) ; i added

msgbox, %spawnedby%  ;i added

/* 
	Credit to Wanker
	
	author 	: Chef
	link 	: http://ahkscript.org/boards/memberlist.php?mode=viewprofile&u=55319
	date 	: 14 January 2014

	description

		Process-related functions
*/	

process_property(pid, property)
{
	/*
		available properties:

			Caption
			CommandLine
			CreationClassName
			CreationDate
			CSCreationClassName
			CSName
			Description
			ExecutablePath
			ExecutionState
			Handle
			HandleCount
			InstallDate
			KernelModeTime
			MaximumWorkingSetSize
			MinimumWorkingSetSize
			Name
			OSCreationClassName
			OSName
			OtherOperationCount
			OtherTransferCount
			PageFaults
			PageFileUsage
			ParentProcessId
			PeakPageFileUsage
			PeakVirtualSize
			PeakWorkingSetSize
			Priority
			PrivatePageCount
			ProcessId
			QuotaNonPagedPoolUsage
			QuotaPagedPoolUsage
			QuotaPeakNonPagedPoolUsage
			QuotaPeakPagedPoolUsage
			ReadOperationCount
			ReadTransferCount
			SessionId
			Status
			TerminationDate
			ThreadCount
			UserModeTime
			VirtualSize
			WindowsVersion
			WorkingSetSize
			WriteOperationCount
			WriteTransferCount
		
		msdn
			
			http://msdn.microsoft.com/en-us/library/aa394372%28v=vs.85%29.aspx
	*/
	for process in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Process where ProcessId=" pid)
		return, process ;[property] ; i tried leaving it as process[property] but got error
}

process_isFirstInstance()
{
	if !a_isCompiled
	{
		msgBox, 0x40010,% a_thisFunc "()",This function can only be used with compiled scripts!`n`n"%a_scriptName%"
		return
	}
	scriptPID := DllCall( "GetCurrentProcessId" )
	process, exist, %A_Scriptname%
	ifequal,scriptPID,%errorLevel%,return 1
}

process_instanceCount(name)
{
	for process in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Process where Name='" name "'")
		count := a_index
	return count
}
User avatar
Masonjar13
Posts: 1555
Joined: 20 Jul 2014, 10:16
Location: Не Россия
Contact:

Re: Is this even possible? Check what spawned a process?

22 Apr 2018, 13:27

The following works:

Code: Select all

pid:=5984 ; replace as necessary
for process in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Process where ProcessId=" pid)
    parent:=process["ParentProcessId"]

msgbox % parent
Alternatively, if you want to keep your script as you have it, just make ParentProcessId a string. spawnedby := process_property(pidmy, "ParentProcessId") It was interpreting ParentProcessId as a variable, which was empty.
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
JeezLouise

Re: Is this even possible? Check what spawned a process?

22 Apr 2018, 22:01

Masonjar13 wrote:The following works:

Code: Select all

pid:=5984 ; replace as necessary
for process in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Process where ProcessId=" pid)
    parent:=process["ParentProcessId"]

msgbox % parent
Alternatively, if you want to keep your script as you have it, just make ParentProcessId a string. spawnedby := process_property(pidmy, "ParentProcessId") It was interpreting ParentProcessId as a variable, which was empty.
:thumbup: :thumbup: that fixed it! Thank YOUU!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: TAC109 and 321 guests