Search notes:

Enumerate Windows Services in C/WinAPI

The following WinAPI example demonstrates EnumServiceStatusEx: it enumerates all Windows services on the local computer:
//
//   cl .\EnumServiceStatusEx.c advapi32.lib
//

#include <windows.h>
#include <stdio.h>

int main() {

   SC_HANDLE scMgr = OpenSCManager(
      NULL,
      SERVICES_ACTIVE_DATABASE,
      SC_MANAGER_ALL_ACCESS
   );

   if (scMgr) {

      DWORD myPID = GetCurrentProcessId();

      DWORD additionalNeeded;
      DWORD cnt = 0;
      DWORD resume       = 0;

      ENUM_SERVICE_STATUS_PROCESS  services[1024];

      if (
         EnumServicesStatusEx(
            scMgr,
            SC_ENUM_PROCESS_INFO,        // Influences 5th parameter!
            SERVICE_WIN32_OWN_PROCESS,   // Service type (SERVICE_WIN32_OWN_PROCESS = services that run in their own process)
            SERVICE_STATE_ALL,           // Service state (ALL = active and inactive ones)
            services,
            sizeof(services),
           &additionalNeeded,
           &cnt,
           &resume,
            NULL                         // Group name
        ))
       {

          for (DWORD i = 0; i < cnt; i++) {

            printf("%-50s %5d\n", services[i].lpServiceName, services[i].ServiceStatusProcess.dwProcessId);

          }
       }
       CloseServiceHandle(scMgr);
   }
   else {
      printf("Could not open service manager.\n");
   }
}
Github repository Windows-development, path: /WinAPI/EnumServiceStatusEx.c

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/Windows/...', 1758201509, '216.73.216.150', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/Windows/services/enumerate/c(87): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78