Enumerate Windows system services, fetch service name through process ID

Source: Internet
Author: User



Enumerate Windows system Services

The functions used and the MSDN instructions 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;                Buffer length passed in DWORD dwbufneed = 0;        Required buffer length DWORD Dwnumberofservice = 0;   Number of services returned enum_service_status_process *pserviceinfo = NULL; Service Information//establish 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;} Gets the required buffer size enumservicesstatusex (HSCM, Sc_enum_process_info, Service_win32, Service_state_all, NULL, Dwbufsize, &dwbufneed, &dwnumberofservice, NULL, NULL),//Multiple settings 1 length of service information dwbufsize = dwbufneed + sizeof (enum_service_ status_process);p buf = (char *) malloc (dwbufsize); if (NULL = = PBuf) {printf ("malloc error.\n"); return-1;} memset (pBuf, 0, dwbufsize);//Get 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 Open Service Handle bRet =:: Closeservicehandle (HSCM), if (BRet = = FALSE) {printf ("Closeservicehandle error.\n");} printf ("Service num:%d\n", dwnumberofservice);p serviceinfo = (lpenum_service_status_process) pbuf;//Print the service information obtained for ( unsigned int i = 0; i < Dwnumberofservice; i++) {printf ("----------%d----------\ n", i);p rintf ("DisplayName \t\t:%s \ n", Pserviceinfo[i].lpdisplayname);p rintf ( "ServiceName \t\t:%s \ n", Pserviceinfo[i].lpservicename);p rintf ("servicetype \t\t:%d \ n", Pserviceinfo[i]. Servicestatusprocess.dwservicetype);p rintf ("currentstate \t\t:%d \ n", Pserviceinfo[i]. servicestatusprocess.dwcurrentstate);p rintf ("controlsaccepted \ t:%d \ n", Pserviceinfo[i]. servicestatusprocess.dwcontrolsaccepted);p rintf ("Win32exitcode \t\t: %d \ n ", Pserviceinfo[i]. Servicestatusprocess.dwwin32exitcode);p rintf ("ServiceSpecificExitCode:%d \ n", Pserviceinfo[i]. Servicestatusprocess.dwservicespecificexitcode);p rintf ("CheckPoint \t\t:%d \ n", Pserviceinfo[i]. Servicestatusprocess.dwcheckpoint);p rintf ("WaitHint \t\t:%d \ n", Pserviceinfo[i]. Servicestatusprocess.dwwaithint);p rintf ("Process Id \t\t:%d \ n", Pserviceinfo[i]. SERVICESTATUSPROCESS.DWPROCESSID);p rintf ("serviceflags \t\t:%d \ n", Pserviceinfo[i]. Servicestatusprocess.dwserviceflags);} Free (pBuf); System ("PAUSE"); return 0;}


Now that you have access to all of the service information,
Then, according to the process ID query whether the process is a service, get the service name and a series of operations can be completed according to their own needs
Get process ID GetCurrentProcessId ()



Enumerate Windows system services, fetch service name through process ID

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.