Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
4.0 KiB

  1. #include "wtsapi32.h" // for terminal services
  2. typedef LRESULT CALLBACK FN_TSNotifyWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  3. typedef BOOL (WINAPI *LPWTSREGISTERSESSIONNOTIFICATION)(HWND hWnd, DWORD dwFlags);
  4. typedef BOOL (WINAPI *LPWTSUNREGISTERSESSIONNOTIFICATION)(HWND hWnd);
  5. HMODULE g_hLibrary = 0;
  6. LPWTSREGISTERSESSIONNOTIFICATION g_lpfnWTSRegisterSessionNotification = 0;
  7. LPWTSUNREGISTERSESSIONNOTIFICATION g_lpfnWTSUnRegisterSessionNotification = 0;
  8. BOOL GetWTSLib()
  9. {
  10. g_hLibrary = LoadLibrary(TEXT("wtsapi32.dll"));
  11. if (g_hLibrary)
  12. {
  13. g_lpfnWTSRegisterSessionNotification
  14. = (LPWTSREGISTERSESSIONNOTIFICATION)GetProcAddress(
  15. g_hLibrary
  16. , "WTSRegisterSessionNotification");
  17. g_lpfnWTSUnRegisterSessionNotification
  18. = (LPWTSUNREGISTERSESSIONNOTIFICATION)GetProcAddress(
  19. g_hLibrary
  20. , "WTSUnRegisterSessionNotification");
  21. }
  22. return (g_lpfnWTSRegisterSessionNotification
  23. && g_lpfnWTSUnRegisterSessionNotification)?TRUE:FALSE;
  24. }
  25. void FreeWTSLib()
  26. {
  27. if (g_hLibrary)
  28. {
  29. FreeLibrary(g_hLibrary);
  30. g_hLibrary = 0;
  31. g_lpfnWTSRegisterSessionNotification = 0;
  32. g_lpfnWTSUnRegisterSessionNotification = 0;
  33. }
  34. }
  35. // CreateWTSNotifyWindow - create a message-only windows to handle
  36. // terminal server notification messages
  37. //
  38. HWND CreateWTSNotifyWindow(HINSTANCE hInstance, FN_TSNotifyWndProc lpfnTSNotifyWndProc)
  39. {
  40. HWND hWnd = 0;
  41. if (GetWTSLib())
  42. {
  43. LPTSTR pszWindowClass = TEXT("TS Notify Window");
  44. WNDCLASS wc;
  45. wc.style = 0;
  46. wc.lpfnWndProc = lpfnTSNotifyWndProc;
  47. wc.cbClsExtra = 0;
  48. wc.cbWndExtra = 0;
  49. wc.hInstance = hInstance;
  50. wc.hIcon = NULL;
  51. wc.hCursor = NULL;
  52. wc.hbrBackground = NULL;
  53. wc.lpszMenuName = NULL;
  54. wc.lpszClassName = pszWindowClass;
  55. // RegisterClass can legally fail sometimes. If the class fails
  56. // to register, we'll fail when we try to create the window.
  57. RegisterClass(&wc);
  58. // Create window to receive terminal service notification messages
  59. hWnd = CreateWindow(
  60. pszWindowClass
  61. , NULL,0,0,0,0,0
  62. , HWND_MESSAGE
  63. , NULL, hInstance, NULL);
  64. if( hWnd )
  65. {
  66. if (!g_lpfnWTSRegisterSessionNotification(hWnd, NOTIFY_FOR_THIS_SESSION))
  67. {
  68. DBPRINTF(TEXT("CreateWTSNotifyWindow: WTSRegisterSessionNotification FAILED %d\r\n"), GetLastError());
  69. DestroyWindow(hWnd);
  70. hWnd = 0;
  71. }
  72. }
  73. }
  74. return hWnd;
  75. }
  76. // DestroyWTSNotifyWindow - clean up terminal server notification window
  77. //
  78. void DestroyWTSNotifyWindow(HWND hWnd)
  79. {
  80. if(hWnd && g_lpfnWTSUnRegisterSessionNotification)
  81. {
  82. g_lpfnWTSUnRegisterSessionNotification(hWnd);
  83. DBPRINTF(TEXT("DestroyWTSNotifyWindow: WTSUnRegisterSessionNotification returned %d\r\n"), GetLastError());
  84. }
  85. if(hWnd)
  86. {
  87. DestroyWindow(hWnd);
  88. }
  89. FreeWTSLib();
  90. }
  91. /*
  92. // TSNotifyWndProc - callback that receives window message notifications from terminal services
  93. //
  94. // This is a sample notification callback function
  95. //
  96. LRESULT CALLBACK TSNotifyWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  97. {
  98. if (uMsg != WM_WTSSESSION_CHANGE)
  99. return 0;
  100. switch (wParam)
  101. {
  102. case WTS_CONSOLE_CONNECT: // local session is connected
  103. break;
  104. case WTS_CONSOLE_DISCONNECT:// local session is disconnected
  105. break;
  106. case WTS_REMOTE_CONNECT: // remote session is connected
  107. break;
  108. case WTS_REMOTE_DISCONNECT: // remote session is disconnected
  109. break;
  110. case WTS_SESSION_LOGON: // session is being logged on
  111. break;
  112. case WTS_SESSION_LOGOFF: // session is being logged off
  113. break;
  114. default:
  115. break;
  116. }
  117. return DefWindowProc( hwnd, uMsg, wParam, lParam );
  118. }
  119. */