Utilties similar to GetTasks

Post Reply
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Utilties similar to GetTasks

Post 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
Regards

G. N. Rao.
Hyderabad, India
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Post by Antonio Linares »

Thanks! :-)
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Rick Lipkin
Posts: 2397
Joined: Fri Oct 07, 2005 1:50 pm
Location: Columbia, South Carolina USA

Post 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
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Post 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.
Regards

G. N. Rao.
Hyderabad, India
User avatar
Rick Lipkin
Posts: 2397
Joined: Fri Oct 07, 2005 1:50 pm
Location: Columbia, South Carolina USA

Post 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
Post Reply