-
Service Error 1072
Hi All,
I've written a service and I'm having trouble uninstalling it. Rather than just uninstalling the OS marks the service for deletion and gives this error if I try and uninstall again:
ERROR_SERVICE_MARKED_FOR_DELETE
The initial removal returns no error.
I know a reboot is a temprary solution.
What could I be doing wrong?
Code:
int MyDeleteService()
{
SC_HANDLE hService,schSCManager;
config *my_config;
if(InitService(my_config))
{
printf("Error initialising - check config file!\n");
return 1;
}
schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
if (schSCManager == NULL)
return GetLastError();
hService=OpenService(schSCManager,my_config->my_server.serviceName,SERVICE_ALL_ACCESS);
if (hService == NULL)
return GetLastError();
if(DeleteService(hService)==0)
return GetLastError();
if(CloseServiceHandle(hService)==0)
return GetLastError();
CleanupService();
return 0;
}
Thanks, rotis23
-
I think you need to provide a proper "stop" service as well, otherwise it remains in use until the OS reboots.
If the stop properly detaches your service from the OS, then there is no reason why it should not be possible to delete it when you de-install it.
-
When you say stop are you refering to a signal handler?
I do this:
Code:
void ControlHandler(DWORD request)
{
switch(request)
{
case SERVICE_CONTROL_STOP:
CleanupService();
ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus (hStatus, &ServiceStatus);
return;
case SERVICE_CONTROL_SHUTDOWN:
CleanupService();
ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus (hStatus, &ServiceStatus);
return;
default:
break;
}
// Report current status
SetServiceStatus (hStatus, &ServiceStatus);
}
I think you're right though - I've read that it will remain in the 'Service Table' until all handles are freed. As far as I'm aware I'm freeing all handles.
Thanks for your reply.
-
Sorry - the service was still sleeping in the main loop. I've changed the sleeping mechanism.
rotis23
-
Spoke too soon - The service is stopping correctly now - closing and cleaning up.
But I still can't delete the service.
Upon what conditions will the service still be 'attached'?
If I install the service and remove it - it is deleted OK.
If I run the service then it does not delete OK afterwards.
What could I be leaving open in ServiceMain? There are no SC_HANDLE structure to close!
rotis23
-
Hi rotis23,
I've created my own service last week and it's working fine. I also had the same problem but found out I had the services window still open.
Here is my version of the un-install:
Code:
void UninstallService()
{
SC_HANDLE serviceControlManager = OpenSCManager(0, 0, SC_MANAGER_CONNECT);
if(serviceControlManager)
{
SC_HANDLE service = OpenService( serviceControlManager,
serviceName, SERVICE_QUERY_STATUS | DELETE);
if(service)
{
SERVICE_STATUS serviceStatus;
if(QueryServiceStatus( service, &serviceStatus))
{
if(serviceStatus.dwCurrentState == SERVICE_STOPPED)
DeleteService(service);
}
CloseServiceHandle(service);
}
CloseServiceHandle(serviceControlManager);
}
}