Skip to content

Instantly share code, notes, and snippets.

@Sohamkadam333
Created December 12, 2023 18:56
Show Gist options
  • Save Sohamkadam333/d493f34c54e81527fb83bf8889c14c55 to your computer and use it in GitHub Desktop.
Save Sohamkadam333/d493f34c54e81527fb83bf8889c14c55 to your computer and use it in GitHub Desktop.
Opens an existing service.
// OpenService function is part of the Windows API, and it is used to open a handle to an existing service in the Service Control Manager (SCM) database. The SCM is responsible for managing services on a Windows system.
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
const wchar_t *serviceName = L"AdobeARMservice";
SC_HANDLE scmHandle = OpenSCManager(NULL,NULL,SC_MANAGER_CONNECT);
if(scmHandle == NULL)
{
cerr<<"Error Opening Service Control Manager, Error No: "<<GetLastError()<<endl;
system("PAUSE");
return 1;
}
cout<<"Service Control Manager Opened Successfully "<<endl;
SC_HANDLE serviceHandle = OpenService(scmHandle,serviceName,SERVICE_QUERY_STATUS);
if(serviceHandle == NULL)
{
cerr<<"Error Opening Service, Error No: "<<GetLastError()<<endl;
system("PAUSE");
return 1;
}
cout<<"Service Opened Successfully "<<endl;
CloseServiceHandle(serviceHandle);
cout<<"Service Closed Successfully "<<endl;
CloseServiceHandle(scmHandle);
cout<<"Service Control Manager Closed Successfully "<<endl;
system("PAUSE");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment