Windows specific: added registerServiceDeviceNotification() and handleDeviceEvent() to handle SERVICE_CONTROL_DEVICEEVENT events

This commit is contained in:
Günter Obiltschnig 2015-03-13 13:51:51 +01:00
parent e6b4c12ad2
commit e1694c3e68
2 changed files with 37 additions and 5 deletions

View File

@ -168,6 +168,16 @@ protected:
void defineOptions(OptionSet& options);
#endif
#if defined(POCO_OS_FAMILY_WINDOWS)
static HDEVNOTIFY registerServiceDeviceNotification(LPVOID filter, DWORD flags);
/// Registers the ServerApplication to receive SERVICE_CONTROL_DEVICEEVENT
/// events.
virtual DWORD handleDeviceEvent(DWORD event_type, LPVOID event_data);
/// Handles the SERVICE_CONTROL_DEVICEEVENT event. The default
/// implementation does nothing and returns ERROR_CALL_NOT_IMPLEMENTED.
#endif // if defined(POCO_OS_FAMILY_WINDOWS)
private:
#if defined(POCO_VXWORKS)
static Poco::Event _terminate;
@ -188,7 +198,7 @@ private:
SRV_UNREGISTER
};
static BOOL __stdcall ConsoleCtrlHandler(DWORD ctrlType);
static void __stdcall ServiceControlHandler(DWORD control);
static DWORD __stdcall ServiceControlHandler(DWORD control, DWORD event_type, LPVOID event_data, LPVOID context);
#if defined(POCO_WIN32_UTF8) && !defined(POCO_NO_WSTRING)
static void __stdcall ServiceMain(DWORD argc, LPWSTR* argv);
#else

View File

@ -132,8 +132,23 @@ BOOL ServerApplication::ConsoleCtrlHandler(DWORD ctrlType)
}
void ServerApplication::ServiceControlHandler(DWORD control)
HDEVNOTIFY ServerApplication::registerServiceDeviceNotification(LPVOID filter, DWORD flags)
{
return RegisterDeviceNotification(_serviceStatusHandle, filter, flags);
}
DWORD ServerApplication::handleDeviceEvent(DWORD /*event_type*/, LPVOID /*event_data*/)
{
return ERROR_CALL_NOT_IMPLEMENTED;
}
DWORD ServerApplication::ServiceControlHandler(DWORD control, DWORD event_type, LPVOID event_data, LPVOID context)
{
DWORD result = NO_ERROR;
auto instance = reinterpret_cast<ServerApplication*>(context);
switch (control)
{
case SERVICE_CONTROL_STOP:
@ -142,9 +157,16 @@ void ServerApplication::ServiceControlHandler(DWORD control)
_serviceStatus.dwCurrentState = SERVICE_STOP_PENDING;
break;
case SERVICE_CONTROL_INTERROGATE:
break;
break;
case SERVICE_CONTROL_DEVICEEVENT:
if (instance)
{
result = instance->handleDeviceEvent(event_type, event_data);
}
break;
}
SetServiceStatus(_serviceStatusHandle, &_serviceStatus);
return result;
}
@ -159,9 +181,9 @@ void ServerApplication::ServiceMain(DWORD argc, LPTSTR* argv)
app.config().setBool("application.runAsService", true);
#if defined(POCO_WIN32_UTF8) && !defined(POCO_NO_WSTRING)
_serviceStatusHandle = RegisterServiceCtrlHandlerW(L"", ServiceControlHandler);
_serviceStatusHandle = RegisterServiceCtrlHandlerExW(L"", ServiceControlHandler, &app);
#else
_serviceStatusHandle = RegisterServiceCtrlHandlerA("", ServiceControlHandler);
_serviceStatusHandle = RegisterServiceCtrlHandlerExA("", ServiceControlHandler, &app);
#endif
if (!_serviceStatusHandle)
throw SystemException("cannot register service control handler");