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.

478 lines
13 KiB

  1. // ConMsg.cpp : Defines the entry point for the application.
  2. //
  3. #include "stdafx.h"
  4. #include "resource.h"
  5. #include "stdio.h"
  6. #include "tchar.h"
  7. #include "wtsapi32.h"
  8. #include "winsta.h"
  9. #define MAX_LOADSTRING 100
  10. // Global Variables:
  11. HINSTANCE hInst; // current instance
  12. TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
  13. TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
  14. // Foward declarations of functions included in this code module:
  15. ATOM MyRegisterClass(HINSTANCE hInstance);
  16. BOOL InitInstance(HINSTANCE, int);
  17. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  18. LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
  19. int APIENTRY WinMain(HINSTANCE hInstance,
  20. HINSTANCE /* hPrevInstance*/,
  21. LPSTR /* lpCmdLine */,
  22. int nCmdShow)
  23. {
  24. // TODO: Place code here.
  25. MSG msg;
  26. HACCEL hAccelTable;
  27. // Initialize global strings
  28. LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  29. LoadString(hInstance, IDC_POWERMSG, szWindowClass, MAX_LOADSTRING);
  30. MyRegisterClass(hInstance);
  31. // Perform application initialization:
  32. if (!InitInstance (hInstance, nCmdShow))
  33. {
  34. return FALSE;
  35. }
  36. hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_POWERMSG);
  37. // Main message loop:
  38. while (GetMessage(&msg, NULL, 0, 0))
  39. {
  40. if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  41. {
  42. TranslateMessage(&msg);
  43. DispatchMessage(&msg);
  44. }
  45. }
  46. return msg.wParam;
  47. }
  48. //
  49. // FUNCTION: MyRegisterClass()
  50. //
  51. // PURPOSE: Registers the window class.
  52. //
  53. // COMMENTS:
  54. //
  55. // This function and its usage is only necessary if you want this code
  56. // to be compatible with Win32 systems prior to the 'RegisterClassEx'
  57. // function that was added to Windows 95. It is important to call this function
  58. // so that the application will get 'well formed' small icons associated
  59. // with it.
  60. //
  61. ATOM MyRegisterClass(HINSTANCE hInstance)
  62. {
  63. WNDCLASSEX wcex;
  64. wcex.cbSize = sizeof(WNDCLASSEX);
  65. wcex.style = CS_HREDRAW | CS_VREDRAW;
  66. wcex.lpfnWndProc = (WNDPROC)WndProc;
  67. wcex.cbClsExtra = 0;
  68. wcex.cbWndExtra = 0;
  69. wcex.hInstance = hInstance;
  70. wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_POWERMSG);
  71. wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  72. wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  73. wcex.lpszMenuName = (LPCSTR)IDC_POWERMSG;
  74. wcex.lpszClassName = szWindowClass;
  75. wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
  76. return RegisterClassEx(&wcex);
  77. }
  78. //
  79. // FUNCTION: InitInstance(HANDLE, int)
  80. //
  81. // PURPOSE: Saves instance handle and creates main window
  82. //
  83. // COMMENTS:
  84. //
  85. // In this function, we save the instance handle in a global variable and
  86. // create and display the main program window.
  87. //
  88. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  89. {
  90. HWND hWnd;
  91. hInst = hInstance; // Store instance handle in our global variable
  92. hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
  93. CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
  94. if (!hWnd)
  95. {
  96. return FALSE;
  97. }
  98. ShowWindow(hWnd, nCmdShow);
  99. UpdateWindow(hWnd);
  100. // if (!SetTimer(hWnd, 23, 1500, NULL))
  101. // MessageBox(NULL, _T("Failed"), _T("Failed"), MB_OK);
  102. return TRUE;
  103. }
  104. //
  105. // FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
  106. //
  107. // PURPOSE: Processes messages for the main window.
  108. //
  109. // WM_COMMAND - process the application menu
  110. // WM_PAINT - Paint the main window
  111. // WM_DESTROY - post a quit message and return
  112. //
  113. //
  114. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  115. {
  116. int wmId, wmEvent;
  117. static HWND hChildWnd;
  118. LRESULT lResult;
  119. TCHAR szString[512];
  120. int iCount;
  121. static int iState = 0;
  122. DWORD dwSessionId;
  123. // int iVal;
  124. // HWND hWnd2;
  125. switch (message)
  126. {
  127. case WM_CREATE:
  128. lResult = DefWindowProc(hWnd, message, wParam, lParam);
  129. RECT rt;
  130. GetClientRect(hWnd, &rt);
  131. hChildWnd = CreateWindow("LISTBOX", szTitle, WS_CHILD | WS_VISIBLE,
  132. 0, 0, rt.right - rt.left, rt.bottom - rt.top, hWnd, NULL, hInst, NULL);
  133. srand( 25 );
  134. if (!SetTimer(hWnd, 23, 1500, NULL))
  135. MessageBox(NULL, _T("Failed"), _T("Failed"), MB_OK);
  136. return lResult;
  137. break;
  138. /*
  139. case WM_TIMER:
  140. SendMessage(hChildWnd, LB_ADDSTRING, 0, LPARAM(_T("Got Timer")));
  141. // hWnd2 = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
  142. // CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInst, NULL);
  143. // ShowWindow(hWnd2, SW_SHOWDEFAULT);
  144. // UpdateWindow(hWnd2);
  145. iVal = rand() % 3;
  146. switch (iVal)
  147. {
  148. case 0:
  149. PostMessage(hWnd, WM_COMMAND, ID_FILE_REGISTERALLSESSIONS, 0);
  150. break;
  151. case 1:
  152. PostMessage(hWnd, WM_COMMAND, ID_FILE_REGISTERFORTHISSESSION, 0);
  153. break;
  154. case 2:
  155. PostMessage(hWnd, WM_COMMAND, ID_FILE_UNREGISTER, 0);
  156. break;
  157. }
  158. break;
  159. */
  160. case WM_POWERBROADCAST:
  161. {
  162. switch (wParam)
  163. {
  164. case PBT_APMBATTERYLOW:
  165. SendMessage(hChildWnd, LB_ADDSTRING, 0, LPARAM(_T("PBT_APMBATTERYLOW")));
  166. break;
  167. case PBT_APMOEMEVENT:
  168. SendMessage(hChildWnd, LB_ADDSTRING, 0, LPARAM(_T("PBT_APMOEMEVENT")));
  169. break;
  170. case PBT_APMPOWERSTATUSCHANGE:
  171. SendMessage(hChildWnd, LB_ADDSTRING, 0, LPARAM(_T("PBT_APMPOWERSTATUSCHANGE")));
  172. break;
  173. case PBT_APMQUERYSUSPEND:
  174. SendMessage(hChildWnd, LB_ADDSTRING, 0, LPARAM(_T("PBT_APMQUERYSUSPEND")));
  175. break;
  176. case PBT_APMQUERYSUSPENDFAILED:
  177. SendMessage(hChildWnd, LB_ADDSTRING, 0, LPARAM(_T("PBT_APMQUERYSUSPENDFAILED")));
  178. break;
  179. case PBT_APMRESUMEAUTOMATIC:
  180. SendMessage(hChildWnd, LB_ADDSTRING, 0, LPARAM(_T("PBT_APMRESUMEAUTOMATIC")));
  181. break;
  182. case PBT_APMRESUMECRITICAL:
  183. SendMessage(hChildWnd, LB_ADDSTRING, 0, LPARAM(_T("PBT_APMRESUMECRITICAL")));
  184. break;
  185. case PBT_APMRESUMESUSPEND:
  186. SendMessage(hChildWnd, LB_ADDSTRING, 0, LPARAM(_T("PBT_APMRESUMESUSPEND")));
  187. break;
  188. case PBT_APMSUSPEND:
  189. SendMessage(hChildWnd, LB_ADDSTRING, 0, LPARAM(_T("PBT_APMSUSPEND")));
  190. break;
  191. }
  192. }
  193. break;
  194. case WM_COMMAND:
  195. wmId = LOWORD(wParam);
  196. wmEvent = HIWORD(wParam);
  197. // Parse the menu selections:
  198. switch (wmId)
  199. {
  200. case ID_FILE_REGISTERALLSESSIONS:
  201. OutputDebugString(TEXT("* ConMsg:calling WTSRegisterSessionNotification\n"));
  202. if (WTSRegisterSessionNotification(hWnd, NOTIFY_FOR_ALL_SESSIONS))
  203. {
  204. iState = 2;
  205. }
  206. else
  207. {
  208. SendMessage(hChildWnd, LB_ADDSTRING, 0, LPARAM(_T("ERROR:Registering for all sessions **failed**.")));
  209. }
  210. OutputDebugString(TEXT("* ConMsg:done with WTSRegisterSessionNotification\n"));
  211. break;
  212. case ID_FILE_REGISTERFORTHISSESSION:
  213. OutputDebugString(TEXT("* ConMsg:calling WTSRegisterSessionNotification\n"));
  214. if (WTSRegisterSessionNotification(hWnd, NOTIFY_FOR_THIS_SESSION))
  215. {
  216. iState = 1;
  217. }
  218. else
  219. {
  220. SendMessage(hChildWnd, LB_ADDSTRING, 0, LPARAM(_T("ERROR:Registered for this session **failed**.")));
  221. }
  222. OutputDebugString(TEXT("* ConMsg:done with WTSRegisterSessionNotification\n"));
  223. break;
  224. case ID_FILE_UNREGISTER:
  225. OutputDebugString(TEXT("* ConMsg:calling WTSUnRegisterSessionNotification\n"));
  226. if (WTSUnRegisterSessionNotification(hWnd))
  227. {
  228. iState = 0;
  229. }
  230. else
  231. {
  232. SendMessage(hChildWnd, LB_ADDSTRING, 0, LPARAM(_T("UNRegistered for this sessions **failed**.")));
  233. }
  234. OutputDebugString(TEXT("* ConMsg:done with WTSUnRegisterConsoleNotification\n"));
  235. break;
  236. case ID_FILE_CLEANLOG:
  237. iCount = SendMessage(hChildWnd, LB_GETCOUNT , 0, 0);
  238. while (iCount != LB_ERR && iCount > 0)
  239. {
  240. SendMessage(hChildWnd, LB_DELETESTRING , 0, 0);
  241. iCount = SendMessage(hChildWnd, LB_GETCOUNT , 0, 0);
  242. }
  243. break;
  244. case IDM_EXIT:
  245. DestroyWindow(hWnd);
  246. iState = 3;
  247. break;
  248. case IDM_GET_SESSIONSTATE:
  249. {
  250. BOOL bLockedState;
  251. DWORD ReturnLength;
  252. if (!WinStationQueryInformation( SERVERNAME_CURRENT,
  253. LOGONID_CURRENT,
  254. WinStationLockedState,
  255. (PVOID)&bLockedState,
  256. sizeof(bLockedState),
  257. &ReturnLength))
  258. {
  259. _stprintf(szString, _T("WinStationQueryInformationfailed, LastError = %d"), GetLastError());
  260. SendMessage(hChildWnd, LB_ADDSTRING, 0, LPARAM(szString));
  261. }
  262. else
  263. {
  264. _stprintf(szString, _T("WinStationQueryInformation Succeeded"));
  265. SendMessage(hChildWnd, LB_ADDSTRING, 0, LPARAM(szString));
  266. _stprintf(szString, _T(" LockedState = %s"), bLockedState ? _T("On") : _T("Off"));
  267. SendMessage(hChildWnd, LB_ADDSTRING, 0, LPARAM(szString));
  268. }
  269. }
  270. break;
  271. case IDM_SET_WELCOME_ON:
  272. {
  273. BOOL bWelcomeOn = 1;
  274. if (!WinStationSetInformation( SERVERNAME_CURRENT,
  275. LOGONID_CURRENT,
  276. WinStationLockedState,
  277. &bWelcomeOn, sizeof(bWelcomeOn)))
  278. {
  279. _stprintf(szString, _T("WinStationSetInformation failed, LastError = %d"), GetLastError());
  280. SendMessage(hChildWnd, LB_ADDSTRING, 0, LPARAM(szString));
  281. }
  282. else
  283. {
  284. _stprintf(szString, _T("WinStationSetInformation succeeded"));
  285. SendMessage(hChildWnd, LB_ADDSTRING, 0, LPARAM(szString));
  286. }
  287. }
  288. break;
  289. case IDM_SET_WELCOME_OFF:
  290. {
  291. BOOL bWelcomeOff = 0;
  292. if (!WinStationSetInformation( SERVERNAME_CURRENT,
  293. LOGONID_CURRENT,
  294. WinStationLockedState,
  295. &bWelcomeOff, sizeof(bWelcomeOff)))
  296. {
  297. _stprintf(szString, _T("WinStationSetInformation failed, LastError = %d"), GetLastError());
  298. SendMessage(hChildWnd, LB_ADDSTRING, 0, LPARAM(szString));
  299. }
  300. else
  301. {
  302. _stprintf(szString, _T("WinStationSetInformation succeeded"));
  303. SendMessage(hChildWnd, LB_ADDSTRING, 0, LPARAM(szString));
  304. }
  305. }
  306. break;
  307. default:
  308. return DefWindowProc(hWnd, message, wParam, lParam);
  309. }
  310. switch (iState)
  311. {
  312. case 0:
  313. _tcscpy(szString, _T("UNRegistered for this sessions."));
  314. break;
  315. case 1:
  316. _tcscpy(szString, _T("Registered for this sessions."));
  317. break;
  318. case 2:
  319. _tcscpy(szString, _T("Registered for all sessions."));
  320. break;
  321. case 3:
  322. default:
  323. iState = 3;
  324. }
  325. if (iState != 3)
  326. {
  327. SendMessage(hChildWnd, LB_ADDSTRING, 0, LPARAM(szString));
  328. }
  329. break;
  330. case WM_DESTROY:
  331. PostQuitMessage(0);
  332. break;
  333. case WM_WTSSESSION_CHANGE:
  334. DWORD GetSessionId(LPARAM lParam);
  335. dwSessionId = GetSessionId(lParam);
  336. switch (wParam)
  337. {
  338. case WTS_CONSOLE_CONNECT:
  339. _stprintf(szString, _T("WTS_CONSOLE_CONNECT, Session Affected was %d"), dwSessionId);
  340. break;
  341. case WTS_CONSOLE_DISCONNECT:
  342. _stprintf(szString, _T("WTS_CONSOLE_DISCONNECT, Session Affected was %d"), dwSessionId);
  343. break;
  344. case WTS_REMOTE_CONNECT:
  345. _stprintf(szString, _T("WTS_REMOTE_CONNECT, Session Affected was %d"), dwSessionId);
  346. break;
  347. case WTS_REMOTE_DISCONNECT:
  348. _stprintf(szString, _T("WTS_REMOTE_DISCONNECT, Session Affected was %d"), dwSessionId);
  349. break;
  350. case WTS_SESSION_LOGON:
  351. _stprintf(szString, _T("WTS_SESSION_LOGON, Session Affected was %d"), dwSessionId);
  352. break;
  353. case WTS_SESSION_LOGOFF:
  354. _stprintf(szString, _T("WTS_SESSION_LOGOFF, Session Affected was %d"), dwSessionId);
  355. break;
  356. case WTS_SESSION_LOCK:
  357. _stprintf(szString, _T("WTS_SESSION_LOCK, Session Affected was %d"), dwSessionId);
  358. break;
  359. case WTS_SESSION_UNLOCK:
  360. _stprintf(szString, _T("WTS_SESSION_UNLOCK, Session Affected was %d"), dwSessionId);
  361. break;
  362. default:
  363. _stprintf(szString, _T("Unknown Notification Code!!!!, Session Affected was %d"), dwSessionId);
  364. break;
  365. }
  366. SendMessage(hChildWnd, LB_ADDSTRING, 0, LPARAM(szString));
  367. break;
  368. default:
  369. return DefWindowProc(hWnd, message, wParam, lParam);
  370. }
  371. return 0;
  372. }
  373. // Mesage handler for about box.
  374. LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM /* lParam */)
  375. {
  376. switch (message)
  377. {
  378. case WM_INITDIALOG:
  379. return TRUE;
  380. case WM_COMMAND:
  381. if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  382. {
  383. EndDialog(hDlg, LOWORD(wParam));
  384. return TRUE;
  385. }
  386. break;
  387. }
  388. return FALSE;
  389. }
  390. DWORD GetSessionId(LPARAM lParam)
  391. {
  392. return lParam;
  393. }