Utilties similar to GetTasks
Posted: Wed Jan 30, 2008 4:57 am
Here are some utilties to get the processes running, kill a process, get users who are running an exe, for any one who may be inerested. Limitations are that these utilties are based on WMI services and work only with xHarbour. I do not know how to make them work with Harbour.
Code: Select all
FUNCTION GetProcesses()
local oWmi, oList, oProc
local cUser, cDomain
local aList := {}
oWmi := WmiService()
oList := oWmi:ExecQuery( "select * from Win32_Process" )
for each oProc in oList
if ( oProc:GetOwner( @cDomain, @cUser ) ) == 0
// Windows Documentation says the parameters are user, domain
// but I am getting the params in reverse order
// domain, user
AAdd( aList, { oProc:Caption, oProc:Name, cUser, cDomain } )
endif
next
return aList
FUNCTION KillProcess( cExe )
local oWmi, oList, oProc
oWmi := WmiService()
oList := oWmi:ExecQuery( "select * from Win32_Process where Name = '" + cExe + "'" )
for each oProc in oList
oProc:Terminate()
next
return nil
FUNCTION GetExeUsers( cExe )
local oWmi, oList, oProc
local cUser, cDomain
local aList := {}
cExe := upper( alltrim( cExe ) )
oWmi := WmiService()
oList := oWmi:ExecQuery( "select * from Win32_Process" )
for each oProc in oList
if cExe == upper( oProc:Caption )
if ( oProc:GetOwner( @cDomain, @cUser ) ) == 0
if AScan( aList, cUser ) == 0
AAdd( aList, cUser )
endif
endif
endif
next
return aList
FUNCTION WMIService()
// It would be useful to keep this function in the library
static oWMI
local oLocator
if oWMI == nil
oLocator := CREATEOBJECT( "wbemScripting.SwbemLocator" )
oWMI := oLocator:ConnectServer()
endif
return oWMI