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.

247 lines
9.4 KiB

  1. #include <string.h>
  2. #include "tsvs.h"
  3. HANDLE g_hWinstaThread = NULL;
  4. DWORD g_idWinstaThread;
  5. void GetWinStationInfo(void);
  6. /*/
  7. typedef enum _WTS_CONNECTSTATE_CLASS {
  8. WTSActive,
  9. WTSConnected,
  10. WTSConnectQuery,
  11. WTSShadow,
  12. WTSDisconnected,
  13. WTSIdle,
  14. WTSListen,
  15. WTSReset,
  16. WTSDown,
  17. WTSInit,
  18. } WTS_CONNECTSTATE_CLASS;
  19. x
  20. #define ACTIVE 0 // "Active"
  21. #define CONNECTED 1 // "Connected"
  22. #define CONNECTQUERY 2 // "Connect Query"
  23. #define SHADOW 3 // "Shadow"
  24. #define DISCONNECTED 4 // "Disconnected"
  25. #define IDLE 5 // "Idle"
  26. #define LISTEN 6 // "Listen"
  27. #define RESET 7 // "Reset"
  28. #define DOWN 8 // "Down"
  29. #define INIT 9 // "Initializing"
  30. /*/
  31. TCHAR * Status[] =
  32. {
  33. "Active", "Connected", "Connect Query", "Shadow", "Disconnected", \
  34. "Idle", "Listen", "Reset", "Down", "Initializing"
  35. };
  36. //////////////////////////////////////////////////////////////////////////////
  37. void GetWinStationInfo(void)
  38. {
  39. UINT i;
  40. WINSTATIONCLIENT ClientData;
  41. ULONG *pNumber;
  42. TCHAR szNumber[10];
  43. int nMcIndex;
  44. int *nConnectState;
  45. DWORD pEventFlags;
  46. static BOOL bInitialized = FALSE;
  47. nMcIndex = 0;
  48. if (bInitialized) {
  49. // wait for someone to log on or off.....
  50. WTSWaitSystemEvent(
  51. WTS_CURRENT_SERVER_HANDLE,
  52. WTS_EVENT_ALL,
  53. //WTS_EVENT_LOGON | WTS_EVENT_LOGOFF,
  54. &pEventFlags
  55. );
  56. //.......................................
  57. }
  58. bInitialized = TRUE;
  59. // count the sessions after a logon
  60. if (WTSEnumerateSessions(
  61. WTS_CURRENT_SERVER_HANDLE,
  62. 0,
  63. 1,
  64. &ppSessionInfo,
  65. &pCount))
  66. {
  67. // loop through the sessions and save their WTSWinStationNames
  68. for (i = 0; i < pCount; i++)
  69. {
  70. if (WTSQuerySessionInformation(
  71. WTS_CURRENT_SERVER_HANDLE,
  72. ppSessionInfo[i].SessionId,
  73. WTSWinStationName,
  74. &ppBuffer,
  75. &pBytesReturned))
  76. {
  77. if (GetMenuState(g_hMenu, IDM_SHOW_ALL,
  78. MF_BYCOMMAND) == MF_UNCHECKED)
  79. {
  80. if (_tcslen(ppBuffer) > 7) // don't take console or enpty ID's
  81. {
  82. _tcscpy(szMcID[nMcIndex], ppBuffer);
  83. WTSFreeMemory(ppBuffer);
  84. // get domain name
  85. if (WTSQuerySessionInformation(
  86. WTS_CURRENT_SERVER_HANDLE,
  87. ppSessionInfo[i].SessionId,
  88. WTSDomainName,
  89. &ppBuffer,
  90. &pBytesReturned))
  91. {
  92. _tcscpy(szMcNames[nMcIndex], ppBuffer);
  93. if (_tcslen(ppBuffer) > 0)
  94. _tcscat(szMcNames[nMcIndex], "\\");
  95. WTSFreeMemory(ppBuffer); }
  96. // get user name
  97. if (WTSQuerySessionInformation(
  98. WTS_CURRENT_SERVER_HANDLE,
  99. ppSessionInfo[i].SessionId,
  100. WTSUserName,
  101. &ppBuffer,
  102. &pBytesReturned))
  103. {
  104. _tcscat(szMcNames[nMcIndex], ppBuffer);
  105. WTSFreeMemory(ppBuffer);
  106. }
  107. // get IP address
  108. if (WinStationQueryInformation(
  109. WTS_CURRENT_SERVER_HANDLE,
  110. ppSessionInfo[i].SessionId,
  111. WinStationClient,
  112. &ClientData,
  113. sizeof(WINSTATIONCLIENT),
  114. &pBytesReturned ))
  115. {
  116. _tcscpy(szMcAddress[nMcIndex],
  117. ClientData.ClientAddress);
  118. }
  119. // get build number
  120. /*/
  121. if (WTSQuerySessionInformation(
  122. WTS_CURRENT_SERVER_HANDLE,
  123. ppSessionInfo[i].SessionId,
  124. WTSClientBuildNumber,
  125. (LPTSTR *)(&pNumber),
  126. &pBytesReturned))
  127. {
  128. _ltot(*pNumber, szNumber, 10);
  129. _tcscpy(szBuild[nMcIndex], szNumber);
  130. WTSFreeMemory(pNumber);
  131. }
  132. /*/
  133. // get connection state
  134. if (WTSQuerySessionInformation(
  135. WTS_CURRENT_SERVER_HANDLE,
  136. ppSessionInfo[i].SessionId,
  137. WTSConnectState,
  138. (LPTSTR *)(&nConnectState),
  139. &pBytesReturned))
  140. {
  141. _tcscpy(szBuild[nMcIndex], Status[*nConnectState]);
  142. WTSFreeMemory(nConnectState);
  143. } else {
  144. _tcscpy(szBuild[nMcIndex], _T("Unknown"));
  145. }
  146. nMcIndex++;
  147. }
  148. } else { // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  149. _tcscpy(szMcID[nMcIndex], ppBuffer);
  150. WTSFreeMemory(ppBuffer);
  151. // get domain name
  152. if (WTSQuerySessionInformation(
  153. WTS_CURRENT_SERVER_HANDLE,
  154. ppSessionInfo[i].SessionId,
  155. WTSDomainName,
  156. &ppBuffer,
  157. &pBytesReturned))
  158. {
  159. _tcscpy(szMcNames[nMcIndex], ppBuffer);
  160. if (_tcslen(ppBuffer) > 0)
  161. _tcscat(szMcNames[nMcIndex], "\\");
  162. WTSFreeMemory(ppBuffer);
  163. }
  164. // get user name
  165. if (WTSQuerySessionInformation(
  166. WTS_CURRENT_SERVER_HANDLE,
  167. ppSessionInfo[i].SessionId,
  168. WTSUserName,
  169. &ppBuffer,
  170. &pBytesReturned))
  171. {
  172. _tcscat(szMcNames[nMcIndex], ppBuffer);
  173. WTSFreeMemory(ppBuffer);
  174. }
  175. // get IP address
  176. if (WinStationQueryInformation(
  177. WTS_CURRENT_SERVER_HANDLE,
  178. ppSessionInfo[i].SessionId,
  179. WinStationClient,
  180. &ClientData,
  181. sizeof(WINSTATIONCLIENT),
  182. &pBytesReturned ))
  183. {
  184. _tcscpy(szMcAddress[nMcIndex],
  185. ClientData.ClientAddress);
  186. }
  187. // get connection state
  188. if (WTSQuerySessionInformation(
  189. WTS_CURRENT_SERVER_HANDLE,
  190. ppSessionInfo[i].SessionId,
  191. WTSConnectState,
  192. (LPTSTR *)(&nConnectState),
  193. &pBytesReturned))
  194. {
  195. _tcscpy(szBuild[nMcIndex], Status[*nConnectState]);
  196. WTSFreeMemory(nConnectState);
  197. }
  198. nMcIndex++;
  199. }
  200. }
  201. }
  202. }
  203. FillList(nMcIndex);
  204. }
  205. //////////////////////////////////////////////////////////////////////////////
  206. DWORD WinstaThreadMessageLoop(LPVOID)
  207. {
  208. MSG msg;
  209. while(GetMessage(&msg, NULL, 0, 0))
  210. {
  211. switch(msg.message)
  212. {
  213. case PM_WINSTA:
  214. {
  215. GetWinStationInfo();
  216. Sleep(1000); // check for logon/off every second
  217. if (g_idWinstaThread)
  218. {
  219. PostThreadMessage(g_idWinstaThread, PM_WINSTA, 0, 0);
  220. }
  221. break;
  222. }
  223. }
  224. }
  225. return 0;
  226. }
  227. //////////////////////////////////////////////////////////////////////////////