Leaked source code of windows server 2003
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.

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