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.

198 lines
4.3 KiB

  1. /*
  2. TSen.h
  3. (c) 2002 Microsoft Corp
  4. */
  5. #include <stdio.h>
  6. #include <malloc.h>
  7. #include <assert.h>
  8. #include <tchar.h>
  9. #include <windows.h>
  10. #include <wtsapi32.h>
  11. #include <Shellapi.h>
  12. #include <process.h>
  13. #include <lm.h>
  14. #include "tsen.h"
  15. void __cdecl main (int argc, TCHAR *argv[])
  16. {
  17. //Get the server name
  18. TCHAR* pszServerName = ParseArgs(argc,argv);
  19. //Print the TS session information
  20. if (printTSSession(pszServerName ))
  21. {
  22. exit(1);
  23. }
  24. exit(0);
  25. }
  26. /*
  27. Function: Constants
  28. Return value: TCHAR Session status values
  29. */
  30. TCHAR* Constants()
  31. {
  32. TCHAR* sztConstants = (TCHAR* ) calloc(SZTSIZE, sizeof(TCHAR));
  33. if(sztConstants!=NULL)
  34. {
  35. ZeroMemory(sztConstants, 1 + _tcslen(sztConstants));
  36. _stprintf(sztConstants, Codes
  37. , (DWORD) WTSActive
  38. , (DWORD) WTSConnected
  39. , (DWORD) WTSConnectQuery
  40. , (DWORD) WTSShadow
  41. , (DWORD) WTSDisconnected
  42. , (DWORD) WTSIdle
  43. , (DWORD) WTSListen
  44. , (DWORD) WTSReset
  45. , (DWORD) WTSDown
  46. , (DWORD) WTSInit);
  47. }
  48. else
  49. {
  50. exit(1);
  51. }
  52. return sztConstants;
  53. }
  54. /*
  55. Function: ParseArgs(int argc, TCHAR *argv[])
  56. Return value: Server name
  57. */
  58. TCHAR* ParseArgs(int argc, TCHAR *argv[])
  59. {
  60. TCHAR* sKeyWord = NULL;
  61. if(argc == 1 || _tcscmp(argv[1],"/?")==0)
  62. {
  63. TCHAR* sUsageInfo = Constants();
  64. _tprintf(_T("%s\n\n%s") , Usage,sUsageInfo);
  65. free(sUsageInfo);
  66. }
  67. else
  68. {
  69. sKeyWord = argv[1];
  70. }
  71. return sKeyWord;
  72. }
  73. /*
  74. printTSSession(TCHAR* pszServerName)
  75. Prints the pszSeverNAme TS session information
  76. */
  77. int printTSSession(TCHAR* pszServerName)
  78. {
  79. DWORD dwTotalCount;
  80. DWORD SessionId;
  81. LPTSTR ppBuffer;
  82. DWORD pBytesReturned;
  83. DWORD dwCount = 0;
  84. TCHAR* sztStatus = NULL;
  85. TCHAR* sztStatLine = NULL;
  86. CONST DWORD Reserved = 0 ;
  87. CONST DWORD Version = 1 ;
  88. PWTS_SESSION_INFO ppSessionInfo;
  89. NET_API_STATUS nStatus;
  90. HANDLE hServer = WTS_CURRENT_SERVER_HANDLE;
  91. if (pszServerName == NULL)
  92. {
  93. exit(1);
  94. }
  95. hServer = WTSOpenServer( pszServerName);
  96. if ( hServer == NULL)
  97. {
  98. _tprintf(_T("WTSOpenServer \"%ws\" error: %u\n"), pszServerName, GetLastError( ));
  99. exit(1);
  100. }
  101. sztStatus = calloc(SZTSIZE, sizeof(TCHAR));
  102. if(sztStatus == NULL)
  103. {
  104. return 1;
  105. }
  106. sztStatLine = calloc(SZTSIZE, sizeof(TCHAR));
  107. if(sztStatLine == NULL)
  108. {
  109. free(sztStatus);
  110. return 1;
  111. }
  112. dwTotalCount = 0;
  113. //Get all the sessions in the server (hServer)
  114. nStatus = WTSEnumerateSessions(hServer,Reserved,Version,&ppSessionInfo,&dwTotalCount);
  115. if (0 == nStatus || 0 == dwTotalCount)
  116. {
  117. _tprintf(_T("WTSEnumerateSessions \"%s\" error: %u\n"),pszServerName,GetLastError( ));
  118. return 1;
  119. }
  120. //Loop trough the session and prints the information bout them
  121. for (dwCount = 0; (dwCount < dwTotalCount); dwCount++)
  122. {
  123. //We only need to display this when the session is active
  124. if (WTSActive == ppSessionInfo[dwCount].State)
  125. {
  126. SessionId = ppSessionInfo[dwCount].SessionId;
  127. WTSQuerySessionInformation(hServer,SessionId,WTSUserName,&ppBuffer,&pBytesReturned);
  128. _stprintf(sztStatLine, _T("Server=%s\nWindow station=%s\nThis session Id=%u\nUser=%s\n"),pszServerName,_tcsupr(ppSessionInfo[dwCount].pWinStationName),SessionId,_tcsupr(ppBuffer));
  129. _tcscpy(sztStatus,sztStatLine );
  130. WTSFreeMemory( ppBuffer);
  131. WTSQuerySessionInformation(hServer,SessionId,WTSClientName,&ppBuffer,&pBytesReturned);
  132. _stprintf(sztStatLine, _T("Client machine=%s\n"),_tcsupr(ppBuffer));
  133. _tcscat(sztStatus, sztStatLine );
  134. WTSFreeMemory( ppBuffer);
  135. WTSQuerySessionInformation(hServer,SessionId,WTSClientAddress,&ppBuffer,&pBytesReturned);
  136. _stprintf(sztStatLine , _T("Active console session=%u\n"),(DWORD) WTSGetActiveConsoleSessionId ());
  137. _tcscat(sztStatus, sztStatLine );
  138. WTSFreeMemory( ppBuffer);
  139. _tprintf(_T("%s"), sztStatus);
  140. } // if (WTSActive == ppSessionInfo[dwCount].State)
  141. } //for (dwCount = 0; (dwCount < dwTotalCount); dwCount++)
  142. WTSFreeMemory( ppSessionInfo);
  143. free(sztStatLine);
  144. free(sztStatus);
  145. if (hServer != WTS_CURRENT_SERVER_HANDLE)
  146. {
  147. (void) WTSCloseServer( hServer);
  148. }
  149. return 0;
  150. } //int printTSSession(TCHAR* pszServerName)