Howt to check if process is running under given username? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
4k3or3et
Posts: 35
Joined: 23 Jan 2019, 13:58

Howt to check if process is running under given username?

08 Aug 2020, 03:26

Hi All

Do you know of any AHK code that would check if process, lets say abc.exe is running under given username?

Thank you.
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Howt to check if process is running under given username?

08 Aug 2020, 04:14

Code: Select all

Process, Exist, notepad.exe
msg := (Errorlevel != 0) ? "Process 'notepad' exist!" : "Process 'notepad' doesn't exist ATM" 
MsgBox % "Dear " A_UserName "`n ..." msg 
:?:
Rohwedder
Posts: 7616
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Howt to check if process is running under given username?

08 Aug 2020, 05:00

Hallo,
try:

Code: Select all

Process, Exist, abc.exe
IF ErrorLevel
{
	UserName := GetProcessUser(ErrorLevel)
	MsgBox,% "UserName: " UserName
}
Else
	MsgBox, Process doesn't exist
Return
GetProcessUser(PID)
{
	VarSetCapacity(UN, 24, 0)
	UserName := ComObject(0x400C, &UN) ; combination of VT_BYREF and VT_VARIANT.
	For Process in ComObjGet("winmgmts:\\.\root\cimv2").ExecQuery("Select * from Win32_Process Where ProcessId = '" . PID . "'")
	Process.GetOwner(UserName)
	Return UserName[]
}
4k3or3et
Posts: 35
Joined: 23 Jan 2019, 13:58

Re: Howt to check if process is running under given username?

08 Aug 2020, 05:03

BoBo wrote:
08 Aug 2020, 04:14

Code: Select all

Process, Exist, notepad.exe
msg := (Errorlevel != 0) ? "Process 'notepad' exist!" : "Process 'notepad' doesn't exist ATM" 
MsgBox % "Dear " A_UserName "`n ..." msg
:?:
Thank you for you reply.

Your code checks process under user where you execute it. I have multiple users logged in (Windows Server). What i am trying to achieve is to detect every few minutes if process is still running under user1, user2 etc. and if not perform an action...
4k3or3et
Posts: 35
Joined: 23 Jan 2019, 13:58

Re: Howt to check if process is running under given username?

08 Aug 2020, 07:30

Rohwedder wrote:
08 Aug 2020, 05:00
Hallo,
try:

Code: Select all

Process, Exist, abc.exe
IF ErrorLevel
{
	UserName := GetProcessUser(ErrorLevel)
	MsgBox,% "UserName: " UserName
}
Else
	MsgBox, Process doesn't exist
Return
GetProcessUser(PID)
{
	VarSetCapacity(UN, 24, 0)
	UserName := ComObject(0x400C, &UN) ; combination of VT_BYREF and VT_VARIANT.
	For Process in ComObjGet("winmgmts:\\.\root\cimv2").ExecQuery("Select * from Win32_Process Where ProcessId = '" . PID . "'")
	Process.GetOwner(UserName)
	Return UserName[]
}
Your code is interesting. It gives msgbox that abc.exe is running by user2 while it is running under 3 other users also. Do you know to transform that code so it would work like "if user1 is running abc.exe then do action", "if user2 is running abc.exe then do action"?

Thank you.
teadrinker
Posts: 4325
Joined: 29 Mar 2015, 09:41
Contact:

Re: Howt to check if process is running under given username?

08 Aug 2020, 07:37

4k3or3et wrote: if process, lets say abc.exe is running under given username?
Several processes with the same names but under different users are possible in the system at the same time.
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Howt to check if process is running under given username?

08 Aug 2020, 07:59

I get the idea that this is an administrative task that should be handled using Powershell ?? :think:
teadrinker
Posts: 4325
Joined: 29 Mar 2015, 09:41
Contact:

Re: Howt to check if process is running under given username?

08 Aug 2020, 08:37

Code: Select all

#Persistent
exeName := "notepad.exe"
userFunc := "MyFunc"

WMI := ComObjGet("winmgmts:")
sink := ComObjCreate("WbemScripting.SWbemSink")
ComObjConnect(sink, new EventSink(Func(userFunc)))
WMI.ExecNotificationQueryAsync(sink, "select * from __InstanceCreationEvent Within 1 "
                                             . "Where TargetInstance ISA 'Win32_Process' "
                                             . "and TargetInstance.Name = '" . exeName . "'")

class EventSink
{
   __New(userFunc) {
      this.userFunc := userFunc
   }
   
   OnObjectReady(obj) {
      static type := (VT_VARIANT := 0xC) | (VT_BYREF := 0x4000)
      Process := obj.TargetInstance
      VarSetCapacity(variant, 24, 0)
      User := ComObject(type, &variant)
      Process.GetOwner(User)
      timer := this.userFunc.Bind(Process.ProcessID, User[])
      SetTimer, % timer, -10
   }
}

MyFunc(PID, owner) {
   MsgBox, % "PID: " . PID . "`nOwner: " . owner
}
Rohwedder
Posts: 7616
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Howt to check if process is running under given username?  Topic is solved

08 Aug 2020, 09:54

Sorry, but there are almost no MSDN tutorials in my mother tongue.

Code: Select all

MsgBox,% Running("abc.exe","user2")

Running(exeName,User)
{ ;1:yes   0:no
	For Proc in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Process")
		IF (Proc.Name = exeName)
			IF (User = GetProcessUser(Proc.ProcessId))
				Return, True
	Return, False
}
GetProcessUser(PID)
{
	VarSetCapacity(UN, 24, 0)
	UserName := ComObject(0x400C, &UN) ; combination of VT_BYREF and VT_VARIANT.
	For Proc in ComObjGet("winmgmts:\\.\root\cimv2").ExecQuery("Select * from Win32_Process Where ProcessId = '" . PID . "'")
	Proc.GetOwner(UserName)
	Return UserName[]
}
4k3or3et
Posts: 35
Joined: 23 Jan 2019, 13:58

Re: Howt to check if process is running under given username?

08 Aug 2020, 12:25

Rohwedder wrote:
08 Aug 2020, 09:54
Sorry, but there are almost no MSDN tutorials in my mother tongue.

Code: Select all

MsgBox,% Running("abc.exe","user2")

Running(exeName,User)
{ ;1:yes   0:no
	For Proc in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Process")
		IF (Proc.Name = exeName)
			IF (User = GetProcessUser(Proc.ProcessId))
				Return, True
	Return, False
}
GetProcessUser(PID)
{
	VarSetCapacity(UN, 24, 0)
	UserName := ComObject(0x400C, &UN) ; combination of VT_BYREF and VT_VARIANT.
	For Proc in ComObjGet("winmgmts:\\.\root\cimv2").ExecQuery("Select * from Win32_Process Where ProcessId = '" . PID . "'")
	Proc.GetOwner(UserName)
	Return UserName[]
}
This is awesome! Works as it should.

Could anyone help me with transforming that code to actually perform my lines of code instead of displaying message box?

Let's say i would like to run c:\yes.exe for yes and c:\no.exe for no...

Thank's a lot for all replies here!
Rohwedder
Posts: 7616
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Howt to check if process is running under given username?

09 Aug 2020, 00:26

Then replace MsgBox,% Running("abc.exe","user2") by:

Code: Select all

IF Running("abc.exe","user2")
	Run, c:\yes.exe
Else
	Run, c:\no.exe
4k3or3et
Posts: 35
Joined: 23 Jan 2019, 13:58

Re: Howt to check if process is running under given username?

09 Aug 2020, 04:00

Rohwedder wrote:
09 Aug 2020, 00:26
Then replace MsgBox,% Running("abc.exe","user2") by:

Code: Select all

IF Running("abc.exe","user2")
	Run, c:\yes.exe
Else
	Run, c:\no.exe
Awesome! Many thanks for your help!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mcd, NullRefEx and 123 guests