Page 1 of 1

Utilties similar to GetTasks

Posted: Wed Jan 30, 2008 4:57 am
by nageswaragunupudi
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

Posted: Wed Jan 30, 2008 9:52 am
by Antonio Linares
Thanks! :-)

Posted: Wed Jan 30, 2008 1:29 pm
by Rick Lipkin
nageswaragunupudi

Question .. the GetExeUsers() function .. I am looking for a program that will tell me 'whohas' what executable runing on a Windows 200(x) server.. looking for users who may have a program running.

Example .. navigate to the server folder where the executable is located and ( launch a program ) to tell me who has it open ??

Thanks
Rick Lipkin

Posted: Wed Jan 30, 2008 5:26 pm
by nageswaragunupudi
Mr Rick Liptin

First as to the above function, this is working on 2003 servers. When executed by user having administrative privileges, this function lists all users using the exe. Here is the improved version.

Code: Select all

FUNCTION GetExeUsers( cExe )

   local oWmi, oList, oProc
   local cUser, cDomain
   local aList := {}

   oWmi     := WmiService()
   oList    := oWmi:ExecQuery( "select * from Win32_Process where Name = '" + cExe + "'" )
   for each oProc in oList
      if ( oProc:GetOwner( @cUser, @cDomain ) ) == 0
         cUser = cDomain + "\" + cUser
         if AScan( aList, cUser ) == 0
            AAdd( aList, cUser )
         endif
      endif
   next

return aList

If we ourselves ( with administrative privileges) login to the server , there are many easier ways.

1) This is the simple thing I do. Open TaskManager, check "Show Processes from All Users" check box, sort the list on Image name in Processes tab and look for the executable name. We can see names of all users executing that program.

2) Run Adminstrative Toops -> Computer Management. Go to Shared Folders -> Open Files. You see all open executables/dlls and users who are using them.

I am not sure if I answered you what you are looking for.

Posted: Thu Jan 31, 2008 12:02 am
by Rick Lipkin
NageswaraRao

I got your vbs script in my private mail .. I will try it in the morning .. and let you know

Rick