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.

316 lines
11 KiB

  1. /****************************************************************************/
  2. // tssdcli.cpp
  3. //
  4. // Terminal Server Session Directory test client code.
  5. //
  6. // Copyright (C) 2000 Microsoft Corporation
  7. /****************************************************************************/
  8. #define INITGUID
  9. #include <windows.h>
  10. #include <objbase.h>
  11. #include <stdio.h>
  12. #include <tssd.h>
  13. // Test session data.
  14. const WCHAR *ServerName = L"TSSDTestSvrName";
  15. const WCHAR *TestClusterName = L"TSSDTestClusterName";
  16. const TSSD_CreateSessionInfo TestSessions[] =
  17. {
  18. { L"FooUser1", L"FooDomain", 1, TSProtocol_RDP, L"", 1024, 768, 8, 0,
  19. 0xFFFF },
  20. };
  21. const FILETIME TestSessionDiscTime[] =
  22. {
  23. { 0, 0xCCCC },
  24. };
  25. /****************************************************************************/
  26. // Command line main
  27. /****************************************************************************/
  28. int __cdecl main(int argc, char *argv[])
  29. {
  30. int rc = 1;
  31. DWORD NumSessions;
  32. HRESULT hr;
  33. FILETIME FileTime;
  34. LONG RegRetVal;
  35. HKEY hKey;
  36. DWORD Len;
  37. DWORD Type;
  38. ITSSessionDirectory *pTSSD;
  39. CLSID TSSDCLSID;
  40. WCHAR CLSIDStr[39];
  41. WCHAR StoreServerName[64];
  42. WCHAR ClusterName[64];
  43. WCHAR OpaqueSettings[256];
  44. TSSD_CreateSessionInfo CreateInfo;
  45. TSSD_DisconnectedSessionInfo DiscInfo[10];
  46. TSSD_ReconnectSessionInfo ReconnInfo;
  47. printf("TS Session Directory test client\n");
  48. // Initialize COM
  49. hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
  50. if (SUCCEEDED(hr)) {
  51. printf(" Initialized COM\n");
  52. }
  53. else {
  54. printf("*** Failed to init COM\n");
  55. return 1;
  56. }
  57. // Get the CLSID of the session directory object to instantiate.
  58. RegRetVal = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
  59. L"System\\CurrentControlSet\\Control\\Terminal Server",
  60. 0, KEY_READ, &hKey);
  61. if (RegRetVal == ERROR_SUCCESS) {
  62. CLSIDStr[0] = L'\0';
  63. Len = sizeof(CLSIDStr);
  64. RegQueryValueExW(hKey, L"SessionDirectoryCLSID",
  65. NULL, &Type, (BYTE *)CLSIDStr, &Len);
  66. if (wcslen(CLSIDStr) == 0 ||
  67. !SUCCEEDED(CLSIDFromString(CLSIDStr, &TSSDCLSID))) {
  68. printf("*** SessDir CLSID invalid, idstr=%S\n", CLSIDStr);
  69. RegCloseKey(hKey);
  70. goto PostInitCOM;
  71. }
  72. RegCloseKey(hKey);
  73. }
  74. else {
  75. printf("*** Failed to open TS key\n");
  76. goto PostInitCOM;
  77. }
  78. // Get the reg settings for the sessdir object. Absence of these settings
  79. // is not an error.
  80. StoreServerName[0] = L'\0';
  81. ClusterName[0] = L'\0';
  82. OpaqueSettings[0] = L'\0';
  83. RegRetVal = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
  84. L"System\\CurrentControlSet\\Control\\Terminal Server\\ClusterSettings",
  85. 0, KEY_READ, &hKey);
  86. if (RegRetVal == ERROR_SUCCESS) {
  87. Len = sizeof(StoreServerName);
  88. RegRetVal = RegQueryValueExW(hKey, L"SessionDirectoryLocation",
  89. NULL, &Type, (BYTE *)StoreServerName, &Len);
  90. Len = sizeof(ClusterName);
  91. RegRetVal = RegQueryValueExW(hKey, L"SessionDirectoryClusterName",
  92. NULL, &Type, (BYTE *)ClusterName, &Len);
  93. // Not an error for the string to be absent or empty.
  94. Len = sizeof(OpaqueSettings);
  95. RegRetVal = RegQueryValueExW(hKey, L"SessionDirectoryAdditionalParams",
  96. NULL, &Type, (BYTE *)OpaqueSettings, &Len);
  97. RegCloseKey(hKey);
  98. }
  99. printf(" Retrieved reg settings:\n");
  100. printf(" SessionDirectoryLocation: %S\n", StoreServerName);
  101. printf(" SessionDirectoryClusterName: %S\n", ClusterName);
  102. printf(" TestClusterName: %S\n", TestClusterName);
  103. printf(" SessionDirectoryAdditionalParams: %S\n", OpaqueSettings);
  104. // Open the TSSD object.
  105. hr = CoCreateInstance(TSSDCLSID, NULL, CLSCTX_INPROC_SERVER,
  106. IID_ITSSessionDirectory, (void **)&pTSSD);
  107. if (SUCCEEDED (hr)) {
  108. printf(" Created a TSSD object\n");
  109. }
  110. else {
  111. printf("*** Failed to create a TSSD, hr=0x%X\n", hr);
  112. goto PostInitCOM;
  113. }
  114. // Initialize with a test local server address.
  115. hr = pTSSD->Initialize((WCHAR *)ServerName, StoreServerName,
  116. (WCHAR *)TestClusterName, OpaqueSettings);
  117. if (SUCCEEDED(hr)) {
  118. printf(" Initialized TSSD, servername=%S\n", ServerName);
  119. }
  120. else {
  121. printf("*** Failed init TSSD, hr=0x%X\n", hr);
  122. goto PostCreateTSSD;
  123. }
  124. // Create one test session.
  125. CreateInfo = TestSessions[0];
  126. hr = pTSSD->NotifyCreateLocalSession(&CreateInfo);
  127. if (SUCCEEDED(hr)) {
  128. printf(" Created test SessionID %u, uname=%S, domain=%S\n",
  129. TestSessions[0].SessionID, TestSessions[0].UserName,
  130. TestSessions[0].Domain);
  131. }
  132. else {
  133. printf("*** Failed create session, hr=0x%X\n", hr);
  134. goto PostCreateTSSD;
  135. }
  136. hr = pTSSD->GetUserDisconnectedSessions((WCHAR *)TestSessions[0].UserName,
  137. (WCHAR *)TestSessions[0].Domain, &NumSessions, DiscInfo);
  138. if (SUCCEEDED(hr)) {
  139. if (NumSessions == 0) {
  140. printf(" Disc session query returned 0 sessions as expected\n");
  141. }
  142. else if (NumSessions > TSSD_MaxDisconnectedSessions) {
  143. printf("*** Disc session query returned %u sessions, over limit "
  144. "of %u\n", NumSessions, TSSD_MaxDisconnectedSessions);
  145. goto PostCreateTSSD;
  146. }
  147. else {
  148. printf("*** Disc session query returned %u sessions, should be 0\n",
  149. NumSessions);
  150. goto PostCreateTSSD;
  151. }
  152. }
  153. else {
  154. printf("*** Failed GetDisc, hr=0x%X\n", hr);
  155. goto PostCreateTSSD;
  156. }
  157. FileTime = TestSessionDiscTime[0];
  158. hr = pTSSD->NotifyDisconnectLocalSession(1, FileTime);
  159. if (SUCCEEDED(hr)) {
  160. printf(" Changed test SessionID %u to disconnected\n",
  161. TestSessions[0].SessionID);
  162. }
  163. else {
  164. printf("*** Failed set test session to disc, hr=0x%X\n", hr);
  165. goto PostCreateTSSD;
  166. }
  167. hr = pTSSD->GetUserDisconnectedSessions((WCHAR *)TestSessions[0].UserName,
  168. (WCHAR *)TestSessions[0].Domain, &NumSessions, DiscInfo);
  169. if (SUCCEEDED(hr)) {
  170. if (NumSessions == 1) {
  171. printf(" Disc session query returned 1 session as expected\n");
  172. // Validate the session info returned.
  173. if (_wcsicmp(DiscInfo[0].ServerAddress, ServerName)) {
  174. printf("*** Ret ServerAddr %S does not match expected %S\n",
  175. DiscInfo[0].ServerAddress, ServerName);
  176. }
  177. if (DiscInfo[0].SessionID != TestSessions[0].SessionID) {
  178. printf("*** Ret SessionID %u does not match expected %u\n",
  179. DiscInfo[0].SessionID,
  180. TestSessions[0].SessionID);
  181. }
  182. if (DiscInfo[0].TSProtocol != TestSessions[0].TSProtocol) {
  183. printf("*** Ret TSProtocol %u does not match expected %u\n",
  184. DiscInfo[0].TSProtocol,
  185. TestSessions[0].TSProtocol);
  186. }
  187. if (_wcsicmp(DiscInfo[0].ApplicationType,
  188. TestSessions[0].ApplicationType)) {
  189. printf("*** Ret AppType %S does not match expected %S\n",
  190. DiscInfo[0].ApplicationType,
  191. TestSessions[0].ApplicationType);
  192. }
  193. if (DiscInfo[0].ResolutionWidth != TestSessions[0].ResolutionWidth) {
  194. printf("*** Ret Width %u does not match expected %u\n",
  195. DiscInfo[0].ResolutionWidth,
  196. TestSessions[0].ResolutionWidth);
  197. }
  198. if (DiscInfo[0].ResolutionHeight != TestSessions[0].ResolutionHeight) {
  199. printf("*** Ret Height %u does not match expected %u\n",
  200. DiscInfo[0].ResolutionHeight,
  201. TestSessions[0].ResolutionHeight);
  202. }
  203. if (DiscInfo[0].ColorDepth != TestSessions[0].ColorDepth) {
  204. printf("*** Ret ColorDepth %u does not match expected %u\n",
  205. DiscInfo[0].ColorDepth,
  206. TestSessions[0].ColorDepth);
  207. }
  208. if (memcmp(&DiscInfo[0].CreateTime, &TestSessions[0].CreateTime,
  209. sizeof(FILETIME))) {
  210. printf("*** Ret CreateTime %u:%u does not match expected %u:%u\n",
  211. DiscInfo[0].CreateTime.dwHighDateTime,
  212. DiscInfo[0].CreateTime.dwLowDateTime,
  213. TestSessions[0].CreateTime.dwHighDateTime,
  214. TestSessions[0].CreateTime.dwLowDateTime);
  215. }
  216. if (memcmp(&DiscInfo[0].DisconnectionTime, &TestSessionDiscTime[0],
  217. sizeof(FILETIME))) {
  218. printf("*** Ret DiscTime %X:%X does not match expected %X:%X\n",
  219. DiscInfo[0].DisconnectionTime.dwHighDateTime,
  220. DiscInfo[0].DisconnectionTime.dwLowDateTime,
  221. TestSessionDiscTime[0].dwHighDateTime,
  222. TestSessionDiscTime[0].dwLowDateTime);
  223. }
  224. }
  225. else if (NumSessions > TSSD_MaxDisconnectedSessions) {
  226. printf("*** Disc session query returned %u sessions, over limit "
  227. "of %u\n", NumSessions, TSSD_MaxDisconnectedSessions);
  228. goto PostCreateTSSD;
  229. }
  230. else {
  231. printf("*** Disc session query returned %u sessions, should be 1\n",
  232. NumSessions);
  233. goto PostCreateTSSD;
  234. }
  235. }
  236. else {
  237. printf("*** Failed GetDisc, hr=0x%X\n", hr);
  238. goto PostCreateTSSD;
  239. }
  240. ReconnInfo.SessionID = TestSessions[0].SessionID;
  241. ReconnInfo.TSProtocol = TestSessions[0].TSProtocol;
  242. ReconnInfo.ResolutionWidth = TestSessions[0].ResolutionWidth;
  243. ReconnInfo.ResolutionHeight = TestSessions[0].ResolutionHeight;
  244. ReconnInfo.ColorDepth = TestSessions[0].ColorDepth;
  245. hr = pTSSD->NotifyReconnectLocalSession(&ReconnInfo);
  246. if (SUCCEEDED(hr)) {
  247. printf(" Changed test SessionID %u to connected\n",
  248. TestSessions[0].SessionID);
  249. }
  250. else {
  251. printf("*** Failed set test sessionID %u to conn, hr=0x%X\n",
  252. TestSessions[0].SessionID, hr);
  253. goto PostCreateTSSD;
  254. }
  255. // Verify again that there are no disconnected sessions.
  256. hr = pTSSD->GetUserDisconnectedSessions((WCHAR *)TestSessions[0].UserName,
  257. (WCHAR *)TestSessions[0].Domain, &NumSessions, DiscInfo);
  258. if (SUCCEEDED(hr)) {
  259. if (NumSessions == 0) {
  260. printf(" Disc session query returned 0 sessions as expected\n");
  261. }
  262. else if (NumSessions > TSSD_MaxDisconnectedSessions) {
  263. printf("*** Disc session query returned %u sessions, over limit "
  264. "of %u\n", NumSessions, TSSD_MaxDisconnectedSessions);
  265. goto PostCreateTSSD;
  266. }
  267. else {
  268. printf("*** Disc session query returned %u sessions, should be 0\n",
  269. NumSessions);
  270. goto PostCreateTSSD;
  271. }
  272. }
  273. else {
  274. printf("*** Failed GetDisc, hr=0x%X\n", hr);
  275. goto PostCreateTSSD;
  276. }
  277. // Success.
  278. rc = 0;
  279. PostCreateTSSD:
  280. pTSSD->Release();
  281. PostInitCOM:
  282. CoUninitialize();
  283. return rc;
  284. }