Enumerate Windows Services, obtain service names by process ID, and enumerate system services
Zookeeper
// Enumerate Windows system services
// The functions used and MSDN descriptions are as follows:
// 1. OpenSCManager description
// Http://msdn.microsoft.com/en-us/library/windows/desktop/ms684323 (v = vs.85). aspx
// 2. EnumServicesStatusEx description
// Http://msdn.microsoft.com/en-us/library/windows/desktop/ms682640 (v = vs.85). aspx
// 3. CloseServiceHandle description
// Http://msdn.microsoft.com/en-us/library/windows/desktop/ms682028 (v = vs.85). aspx
// Test code:
# Include <stdio. h> # include <stdlib. h> # include <tchar. h> # include <windows. h> int main (int argc, char * argv []) {LONG lRet = 0; BOOL bRet = FALSE; SC _HANDLE hSCM = NULL; // Service database handle char * pBuf = NULL; // buffer pointer DWORD dwBufSize = 0; // input buffer length DWORD dwBufNeed = 0; // The required buffer length DWORD dwNumberOfService = 0; // The number of returned services ENUM_SERVICE_STATUS_PROCESS * pServiceInfo = NULL; // The Service Information // establishes a connection to the Service Control Manager, and open the specified database hSCM = OpenSCManager (NULL, NULL, SC _MANAGER_ENUMERATE_SERVICE | SC _MANAGER_CONNECT); if (NULL = hSCM) {printf ("OpenSCManager error. \ n "); return-1 ;}// obtain the required buffer size EnumServicesStatusEx (hSCM, callback, SERVICE_WIN32, SERVICE_STATE_ALL, NULL, dwBufSize, & dwBufNeed, & dwNumberOfService, NULL, NULL); // set the length of one service to dwBufSize = dwBufNeed + sizeof (ENUM_SERVICE_STATUS_PROCESS); pBuf = (char *) malloc (dwBufSize ); if (NULL = pBuf) {printf ("malloc error. \ n "); return-1;} memset (pBuf, 0, dwBufSize); // obtain service information bRet = EnumServicesStatusEx (hSCM, SC _ENUM_PROCESS_INFO, SERVICE_WIN32, SERVICE_STATE_ALL, (LPBYTE) pBuf, dwBufSize, & dwBufNeed, & dwNumberOfService, NULL, NULL); if (bRet = FALSE) {printf ("EnumServicesStatusEx error. \ n ");: CloseServiceHandle (hSCM); free (pBuf); return-1;} // close the opened Service handle bRet =: CloseServiceHandle (hSCM ); if (bRet = FALSE) {printf ("CloseServiceHandle error. \ n ");} printf (" Service Num: % d \ n ", dwNumberOfService); pServiceInfo = (LPENUM_SERVICE_STATUS_PROCESS) pBuf; // print the obtained service information for (unsigned int I = 0; I <dwNumberOfService; I ++) {printf ("---------- % d ---------- \ n", I ); printf ("DisplayName \ t: % s \ n", pServiceInfo [I]. lpDisplayName); printf ("ServiceName \ t: % s \ n", pServiceInfo [I]. lpServiceName); printf ("ServiceType \ t: % d \ n", pServiceInfo [I]. serviceStatusProcess. dwServiceType); printf ("CurrentState \ t: % d \ n", pServiceInfo [I]. serviceStatusProcess. dwCurrentState); printf ("ControlsAccepted \ t: % d \ n", pServiceInfo [I]. serviceStatusProcess. dwControlsAccepted); printf ("Win32ExitCode \ t: % d \ n", pServiceInfo [I]. serviceStatusProcess. dwWin32ExitCode); printf ("ServiceSpecificExitCode: % d \ n", pServiceInfo [I]. serviceStatusProcess. dwServiceSpecificExitCode); printf ("CheckPoint \ t: % d \ n", pServiceInfo [I]. serviceStatusProcess. dwCheckPoint); printf ("WaitHint \ t: % d \ n", pServiceInfo [I]. serviceStatusProcess. dwWaitHint); printf ("Process Id \ t: % d \ n", pServiceInfo [I]. serviceStatusProcess. dwProcessId); printf ("ServiceFlags \ t: % d \ n", pServiceInfo [I]. serviceStatusProcess. dwServiceFlags);} free (pBuf); system ("PAUSE"); return 0 ;}
// Since all service information can be obtained,
// Query whether the process is a service based on the process ID, and obtain the service name and other operations as required.
// Obtain the process ID GetCurrentProcessId ()
How to obtain the service ID of a windows system
Right-click my computer and choose Properties-the computer name is displayed on it!
In Windows, how does one enumerate all system services in the command line?
Net start
----------------------------
I only saw you say "whether there are other commands". I didn't expect you to say SC.
Very easy!
REM ============================================== ===
REM startup status is RUNNING
@ Echo off
SC query | find "DISPLAY_NAME"> 1.txt
For/f "tokens = 2, * delims =" % I in (1.txt) do echo % I % j
Del 1.txt
Pause
REM ============================================== ===
If you want to list all statuses, replace them with the following
SC query state = all | find "DISPLAY_NAME"