I'm trying to stop a sertvice, but the OpenService function fails, saying that the service doesn't exist...
The GetErr function is just a function which returns system error info as text.
Code:
int stopSvc(LPTSTR achSvc){
SERVICE_STATUS_PROCESS sp;
SERVICE_STATUS ss;
SC_HANDLE hScM, hSvc;
DWORD dwBytesNeeded;
if((hScM=OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS))){
if((hSvc=OpenService(hScM, achSvc, SERVICE_ALL_ACCESS))){
if(!QueryServiceStatusEx(hSvc, SC_STATUS_PROCESS_INFO, (LPBYTE)&sp, sizeof(SERVICE_STATUS_PROCESS), &dwBytesNeeded)){
GetErr("QueryServiceStatusEx");
return(0);
}else if(sp.dwCurrentState == SERVICE_STOPPED)
return(1);
if(ControlService(hSvc, SERVICE_CONTROL_STOP, &ss)>0){
CloseServiceHandle(hSvc);
CloseServiceHandle(hScM);
return(1);
}else
GetErr("ControlService");
}else
GetErr("OpenService");
CloseServiceHandle(hSvc);
}else
GetErr("OpenSCManager");
CloseServiceHandle(hScM);
return(0);
}
The service is running on the local machine, and I'm logged on as admin. Running XP SP2/VC.Net.
Any clues?