diff --git a/Makefile b/Makefile index 5517fc1..70e0592 100755 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ TARGET := pppc.exe -OBJS := pppc.o +OBJS := pppc.o session_notification.o CXXFLAGS := -O2 -Wall all: $(TARGET) diff --git a/session_notification.cc b/session_notification.cc new file mode 100644 index 0000000..1229899 --- /dev/null +++ b/session_notification.cc @@ -0,0 +1,70 @@ + +#include +#include +#include +#include "session_notification.h" +using namespace std; + +static BOOL WINAPI (*WTSRegisterSessionNotification)(HWND, DWORD) = NULL; +static BOOL WINAPI (*WTSUnRegisterSessionNotification)(HWND) = NULL; +static HMODULE wtsapi = NULL; + +void session_notification_enable(HWND hWnd) +{ + if (wtsapi == NULL) + { + wtsapi = LoadLibrary("wtsapi32.dll"); + if (wtsapi == NULL) + { + cerr << "Could not load wtsapi32.dll!" << endl; + exit(2); + } + } + if (WTSRegisterSessionNotification == NULL) + { + WTSRegisterSessionNotification = (BOOL WINAPI (*)(HWND, DWORD)) + GetProcAddress(wtsapi, "WTSRegisterSessionNotification"); + if (WTSRegisterSessionNotification == NULL) + { + cerr << "Could not obtain WTSRegisterSessionNotification address!" + << endl; + exit(3); + } + } + + if (WTSRegisterSessionNotification(hWnd, NOTIFY_FOR_ALL_SESSIONS) + != TRUE) + { + cerr << "WTSRegisterSessionNotification() error: " + << GetLastError() << endl; + } +} + +void session_notification_disable(HWND hWnd) +{ + if (wtsapi == NULL) + { + wtsapi = LoadLibrary("wtsapi32.dll"); + if (wtsapi == NULL) + { + cerr << "Could not load wtsapi32.dll!" << endl; + exit(2); + } + } + if (WTSUnRegisterSessionNotification == NULL) + { + WTSUnRegisterSessionNotification = (BOOL WINAPI (*)(HWND)) + GetProcAddress(wtsapi, "WTSUnRegisterSessionNotification"); + if (WTSUnRegisterSessionNotification == NULL) + { + cerr << "Could not obtain WTSUnRegisterSessionNotification address!" + << endl; + exit(3); + } + } + if (WTSUnRegisterSessionNotification(hWnd) != TRUE) + { + cerr << "WTSUnRegisterSessionNotification() error: " + << GetLastError() << endl; + } +} diff --git a/session_notification.h b/session_notification.h new file mode 100644 index 0000000..4c181af --- /dev/null +++ b/session_notification.h @@ -0,0 +1,20 @@ + +#include + +#define NOTIFY_FOR_THIS_SESSION 0 +#define NOTIFY_FOR_ALL_SESSIONS 1 + +#define WM_WTSSESSION_CHANGE 0x02B1 + +#define WTS_CONSOLE_CONNECT 0x1 +#define WTS_CONSOLE_DISCONNECT 0x2 +#define WTS_REMOTE_CONNECT 0x3 +#define WTS_REMOTE_DISCONNECT 0x4 +#define WTS_SESSION_LOGON 0x5 +#define WTS_SESSION_LOGOFF 0x6 +#define WTS_SESSION_LOCK 0x7 +#define WTS_SESSION_UNLOCK 0x8 +#define WTS_SESSION_REMOTE_CONTROL 0x9 + +void session_notification_enable(HWND hWnd); +void session_notification_disable(HWND hWnd);