Skip to content

Commit

Permalink
Windows specific: added registerServiceDeviceNotification() and handl…
Browse files Browse the repository at this point in the history
…eDeviceEvent() to handle SERVICE_CONTROL_DEVICEEVENT events
  • Loading branch information
obiltschnig committed Mar 13, 2015
1 parent e6b4c12 commit e1694c3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
12 changes: 11 additions & 1 deletion Util/include/Poco/Util/ServerApplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,16 @@ class Util_API ServerApplication: public Application
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;
Expand All @@ -188,7 +198,7 @@ class Util_API ServerApplication: public Application
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
Expand Down
30 changes: 26 additions & 4 deletions Util/src/ServerApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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;
}


Expand All @@ -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");
Expand Down

0 comments on commit e1694c3

Please sign in to comment.