Page 1 of 1

Howt to check if process is running under given username?

Posted: 08 Aug 2020, 03:26
by 4k3or3et
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.

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

Posted: 08 Aug 2020, 04:14
by BoBo

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 
:?:

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

Posted: 08 Aug 2020, 05:00
by Rohwedder
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[]
}

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

Posted: 08 Aug 2020, 05:03
by 4k3or3et
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...

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

Posted: 08 Aug 2020, 07:30
by 4k3or3et
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.

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

Posted: 08 Aug 2020, 07:37
by teadrinker
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.

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

Posted: 08 Aug 2020, 07:59
by BoBo
I get the idea that this is an administrative task that should be handled using Powershell ?? :think:

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

Posted: 08 Aug 2020, 08:37
by teadrinker

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
}

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

Posted: 08 Aug 2020, 09:54
by Rohwedder
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[]
}

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

Posted: 08 Aug 2020, 12:25
by 4k3or3et
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!

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

Posted: 09 Aug 2020, 00:26
by Rohwedder
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

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

Posted: 09 Aug 2020, 04:00
by 4k3or3et
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!