WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

System UDFs

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

How to list all NT services

 Keywords:  Service display name list NT

Question:

How can I list all the running services on a specific NT machine? I've looked in the help files and can't find anything.?

Answer:

Here is a UDF that can do it.
Syntax:  
        MyServiceList(listtype)

Parameters:  
(i) list type    0 - tab delimited list of service names
                 1 - tab delimited list of service display names

Returns: 
(s) a tab delimited list of service/display names


Example: 


#DefineFunction MyServiceList(list-type) 
dllname=StrCat(DirWindows(1),"Advapi32.dll")
kerneldllname =StrCat(DirWindows(1),"kernel32.dll")

;OpenSCManager
;The OpenSCManager function establishes a connection to the service control manager on the specified computer and opens the specified service control manager database. 
;
;SC_HANDLE OpenSCManager(
;  LPCTSTR lpMachineName,   // computer name
;  LPCTSTR lpDatabaseName,  // SCM database name
;  DWORD dwDesiredAccess    // access type
;);
SC_MANAGER_ENUMERATE_SERVICE = 4
SC_HANDLE=DllCall(dllname,long:"OpenSCManagerA",lpstr:"",lpnull,long:SC_MANAGER_ENUMERATE_SERVICE)


;EnumServicesStatus
;The EnumServicesStatus function enumerates services in the specified service 
;control manager database. The name and status of each service are provided. 
;
;This function has been superseded by the EnumServicesStatusEx function. 
;It returns the same information EnumServicesStatus returns, plus the process 
;identifier and additional flags for the service. In addition, 
;EnumServicesStatusEx enables you to enumerate services that belong 
;to a specified group. 
;
;BOOL EnumServicesStatus(
;  SC_HANDLE hSCManager,             // handle to SCM database
;  DWORD dwServiceType,              // service type
;  DWORD dwServiceState,             // service state
;  LPENUM_SERVICE_STATUS lpServices, // status buffer
;  DWORD cbBufSize,                  // size of status buffer
;  LPDWORD pcbBytesNeeded,           // buffer size needed
;  LPDWORD lpServicesReturned,       // number of entries returned
;  LPDWORD lpResumeHandle            // next entry
;);
;Predefined constants
SERVICE_WIN32_OWN_PROCESS = 16
SERVICE_WIN32_SHARE_PROCESS = 32
SERVICE_WIN32 =  SERVICE_WIN32_OWN_PROCESS | SERVICE_WIN32_SHARE_PROCESS
SERVICE_ACTIVE = 1
SERVICE_INACTIVE =  2
SERVICE_STATE_ALL = SERVICE_ACTIVE | SERVICE_INACTIVE

bufsize = 100000
lpServices = BinaryAlloc(bufsize)
BinaryEodSet( lpServices,bufsize)

pcbBytesNeeded=BinaryAlloc(4)
BinaryPoke4(pcbBytesNeeded,0,0)

lpServicesReturned=BinaryAlloc(4)
BinaryPoke4(lpServicesReturned,0,0)

lpResumeHandle=BinaryAlloc(4)
BinaryPoke4(lpResumeHandle,0,0)


ret=DllCall(dllname,long:"EnumServicesStatusA",long:SC_HANDLE,long:SERVICE_WIN32,long:SERVICE_STATE_ALL,lpbinary:lpServices,long:bufsize,lpbinary:pcbBytesNeeded,lpbinary:lpServicesReturned,lpbinary:lpResumeHandle)

If ret == 0
  Message("Error","Unable to enumerate services")
  Exit
EndIf

BytesNeeded = BinaryPeek4(pcbBytesNeeded, 0)
ServicesReturned = BinaryPeek4(lpServicesReturned, 0)
ResumeHandle= BinaryPeek4(lpResumeHandle, 0)

svclist = ""
For i = 0 To ServicesReturned-1
      Switch listtype
      Case 0
            ;Get list of Service Names
            lpname =  BinaryPeek4(lpServices, (i*36))
            Break
      Case 1
            ;Get list of Service Display Names
            lpname =  BinaryPeek4(lpServices, (i*36)+4)
            Break
      Case response    ; default case
             ;Get a list of service names
             lpname =  BinaryPeek4(lpServices, (i*36))
             Break
      EndSwitch
      memaddr=IntControl (42, lpServices, 0, 0, 0)
      diff = lpname-memaddr
      svcname = BinaryPeekStr(lpServices, diff,30)
      svclist=StrCat(svclist,@TAB,svcname)
Next
BinaryFree(lpServices)
BinaryFree(pcbBytesNeeded)
BinaryFree(lpServicesReturned)
BinaryFree(lpResumeHandle)


svclist= StrTrim(svclist)

;BOOL CloseServiceHandle(
;  SC_HANDLE hSCObject   // handle to service or SCM object
;);
DllCall( dllname, long:"CloseServiceHandle",long:SC_HANDLE)

Return svclist
#EndFunction

svclist = MyServiceList(0)
AskItemlist("List of Service Names",svclist,@TAB,@SORTED,@SINGLE)

svclist = MyServiceList(1)
AskItemlist("List of Service Display Names",svclist,@TAB,@SORTED,@SINGLE)





Article ID:   W14993
File Created: 2003:05:28:10:24:04
Last Updated: 2003:05:28:10:24:04