serviço Windows desligar
Pergunta
Eu uso VS6 e ATL com CServiceModule para implementar um serviço de janelas personalizadas. No caso de um serviço de erro fatal deve se desligar. Desde CServiceModule está disponível através _Module variável em todos os arquivos Pensei em algo como isso causa CServiceModule :: Run para parar de bombear mensagens e se desligar
PostThreadMessage(_Module.dwThreadID, WM_QUIT, 0, 0);
Este é correta ou você tem idéia melhor?
Solução
Para auto desligamento você envia comando para Service Manager. Tente este exemplo:
BOOL StopServiceCmd ( const char * szServiceName )
{
SC_HANDLE schService;
SC_HANDLE schSCManager;
SERVICE_STATUS ssStatus; // current status of the service
BOOL bRet;
int iCont=0;
schSCManager = OpenSCManager(
NULL, // machine (NULL == local)
NULL, // database (NULL == default)
SC_MANAGER_ALL_ACCESS // access required
);
if ( schSCManager )
{
schService = OpenService(schSCManager, szServiceName, SERVICE_ALL_ACCESS);
if (schService)
{
// try to stop the service
if ( ControlService( schService, SERVICE_CONTROL_STOP, &ssStatus ) )
{
Sleep( 1000 );
while( QueryServiceStatus( schService, &ssStatus ) )
{
iCont++;
if ( ssStatus.dwCurrentState == SERVICE_STOP_PENDING )
{
Sleep( 1000 );
if ( iCont > 4 ) break;
}
else
break;
}
if ( ssStatus.dwCurrentState == SERVICE_STOPPED )
bRet = TRUE;
else
bRet = FALSE;
}
CloseServiceHandle(schService);
}
else
bRet = FALSE;
CloseServiceHandle(schSCManager);
}
else
bRet = FALSE;
return bRet;
}
Outras dicas
Eu acredito que se você fizer isso, então o gerente de serviço vai pensar que o serviço deixou de funcionar e se o usuário já define-se a auto-restart, ele vai.
Em .NET, você usa o ServiceController para sinalizar o seu serviço para desligar. Espero que é similar em Win32 já que a maioria deste material em .NET é apenas wrappers. Desculpe, eu não tenho o código C ++ útil para encerrar o serviço, mas aqui está o código .NET. Esperamos que possa ajudar a Google a informação que você precisa, ou encontrar a documentação no MSDN.
Isto é de algum código conjunto de testes, assim, o estilo de verificação de erros;.) Você terá que colocar esse código em um fio de modo que a mensagem de desligamento fica manipulados
private void stopPLService( bool close )
{
if ( m_serviceController == null )
{
m_serviceController = new ServiceController( "PLService" );
}
WriteLine( "StopPLService" );
if ( m_serviceController != null )
{
try
{
m_serviceController.Stop();
}
catch
{
// Probably just means that it wasn't running or installed, ignore
}
// Wait up to 30 seconds for the service to stop
try
{
m_serviceController.WaitForStatus( ServiceControllerStatus.Stopped, new TimeSpan( 0, 0, 30 ) );
}
catch ( System.ServiceProcess.TimeoutException )
{
Assert.Fail( "Timeout waiting for PLService to stop" );
}
catch
{
// Not installed, we only care in the start
}
if ( close )
{
m_serviceController.Close();
m_serviceController = null;
}
}
}
Você provavelmente vai querer usar o ControlService ou ControlServiceEx métodos para encerrar o serviço. Você deve ser capaz de obter o identificador exigido do CServiceModule.