As we all know to register a service from cmd .Now let’s check how to achieve this from a cpp program to register the service and start.Here we used the same regsvr32 for registering it.The following sample first check if the service(here we assumed the service name is “bytecrunchers“)is available in the system if not then register it and start.Let’ check the code snippets……
SC_HANDLE sch,schService;
SERVICE_STATUS srvcstatus;
sch=OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
schService=OpenService(sch,_T(“bytecrunchers“),SERVICE_ALL_ACCESS);
if(schService==NULL)
{
ShellExecute(NULL,NULL,_T(“regsvr32.exe”),path,NULL,SW_SHOW );
schService=OpenService(sch,_T(“bytecrunchers“),SERVICE_ALL_ACCESS);
StartService(schService,NULL,NULL);
CloseServiceHandle(sch);
CloseServiceHandle(schService);
}
