Archive: SimpleSC::RemoveService returns 1052 instead of 0.


SimpleSC::RemoveService does not work.
Hello,

I am changing my script to use SimpleSC instead of nsSCM.
All seems work except RemoveService.

SimpleSC::RemoveService always returns 1052 (ERROR_INVALID_SERVICE_CONTROL) and it is not removed.

nsSCM::Remove returns "success" and it is removed.

Any ideas?

PS. I am installing by 'SimpleSC::InstallService "MyDrv" "My Driver" "1" "1" "$SYSDIR\Drivers\mydrv.sys" "" "" ""'

Thanks,
Valery.


This is sort of a stab in the dark. First the obvious, make sure you are calling RemoveService with the name of your service. In your case you should be calling 'SimpleSC::RemoveService "MyDrv"'.

Assuming you are doing this and it is not working, then my guess is that the service is not properly stopped. According to MSDN, that error means "The requested control code is not valid, or it is unacceptable to the service." The code should be valid so it likely means the service is not accepting the control (most likely because it is not stopped). Make sure you have code that is checking for the state of the service before trying to remove it. If it says it is running or stopping then you can't call RemoveService and need to either try and resolve this in the installer or notify the user. For example, in one of my installers I check for the existence of a service, get its status, try and stop it, if stop fails I try and kill its process and then stop it again, finally if it was successfully stopped I remove the service. Here is example code with your service name put in (it is a little ugly and there are undoubtedly cleaner ways to do this but this was the first installer I wrote):

!macro SERVICEUNINST un
Function ${un}uninstallService
SimpleSC::ExistsService "myDrv"
Pop $0 ;check the error code
${IF} $0 == 0 ;the service exists
SimpleSC::GetServiceStatus "myDrv"
Pop $0 ;error code
Pop $1 ;service status
${IF} $0 == 0 ;0 indicates success
${ANDIF} $1 == 4 ;4 = service is running
SimpleSC::StopService "myDrv"
Pop $0
${IF} $0 != 0 ;service could not be stopped, try killing the process
Processes::FindProcess "My Driver"
${IF} $R0 != 1 ;process not found
MessageBox MB_ICONSTOP "Error stopping the service. Please stop the service manually and then restart the uninstall process."
Abort
${ELSE}
Processes::KillProcess "My Driver"
${IF} $R0 != 1 ;process not killed
MessageBox MB_ICONSTOP "Error stopping the service. Please stop the service manually and then restart the uninstall process."
Abort
${ELSE} ;killed the process, now try stopping the service again
SimpleSC::StopService "myDrv"
Pop $0
${IF} $0 != 0 ;service could not be stopped
MessageBox MB_ICONSTOP "Error stopping the service. Please stop the service manually and then restart the uninstall process."
Abort
${ENDIF}
${ENDIF}
${ENDIF}
${ENDIF} ;if stop service
${ENDIF} ;if service running
SimpleSC::GetServiceStatus "myDrv"
Pop $0 ;error code
Pop $1 ;service status
${IF} $0 == 0 ;0 indicates success
${ANDIF} $1 == 1 ;1 = service is stopped
SimpleSC::RemoveService "MyDrv" ;delete the service
${ELSE}
MessageBox MB_OK "Service not removed from the system. Please remove it manually using 'sc delete MyDrv' from a command line."
${ENDIF} ;if service exists
FunctionEnd
!macroend