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.

1687 lines
58 KiB

  1. /******************************************************************
  2. Copyright (C) 2001 Microsoft Corp.
  3. sdwmi.cpp -- WMI provider class implementation
  4. Generated by Microsoft WMI Code Generation Engine
  5. TO DO: - See individual function headers
  6. - When linking, make sure you link to framedyd.lib &
  7. msvcrtd.lib (debug) or framedyn.lib & msvcrt.lib (retail).
  8. Description:
  9. ******************************************************************/
  10. #include "stdafx.h"
  11. #include <fwcommon.h>
  12. #include "sdwmi.h"
  13. #include "smartptr.h"
  14. #include <windows.h>
  15. LPWSTR pszUuid = L"6af13c8b-0844-4c83-9064-1892ba825527"; // From JETRPC.IDL
  16. #define SD_QUERY_ENDPOINT_NAME L"TSSessionDirectoryQueryApi"
  17. /****************************************************************************/
  18. // MIDL_user_allocate
  19. // MIDL_user_free
  20. //
  21. // RPC-required allocation functions.
  22. /****************************************************************************/
  23. void __RPC_FAR * __RPC_USER MIDL_user_allocate(size_t Size)
  24. {
  25. return LocalAlloc(LMEM_FIXED, Size);
  26. }
  27. void __RPC_USER MIDL_user_free(void __RPC_FAR *p)
  28. {
  29. LocalFree(p);
  30. }
  31. // Get the handle of the SD RPC server
  32. RPC_BINDING_HANDLE OpenSDWMIServer()
  33. {
  34. HRESULT hr;
  35. WCHAR *pBindingString = NULL;
  36. RPC_BINDING_HANDLE hRPCBinding = NULL;
  37. hr = RpcStringBindingCompose(/*(WCHAR *)g_RPCUUID,*/
  38. 0,
  39. L"ncalrpc", NULL,
  40. SD_QUERY_ENDPOINT_NAME, // Endpoint
  41. NULL, &pBindingString);
  42. if (hr == RPC_S_OK) {
  43. // Generate the RPC binding from the canonical RPC binding string.
  44. hr = RpcBindingFromStringBinding(pBindingString, &hRPCBinding);
  45. if (hr != RPC_S_OK) {
  46. ERR((TB,"SDWMI OpenSDWMIServer: Error %d in RpcBindingFromStringBinding\n", hr));
  47. goto HandleError;
  48. }
  49. }
  50. else {
  51. ERR((TB,"SDWMI OpenSDWMIServer: Error %d in RpcStringBindingCompose\n", hr));
  52. goto HandleError;
  53. }
  54. hr = RpcEpResolveBinding(hRPCBinding, TSSDQUERYRPC_ClientIfHandle);
  55. if (hr != RPC_S_OK) {
  56. ERR((TB, "SDWMI OpenSDWMIServer: Error %d in RpcEpResolveBinding", hr));
  57. goto HandleError;
  58. }
  59. HandleError:
  60. if (pBindingString != NULL) {
  61. RpcStringFree(&pBindingString);
  62. }
  63. return hRPCBinding;
  64. }
  65. // wrapper to make RPC call: TSSDRpcQueryAllClusterInfo
  66. BOOL SDWMIQueryAllClusterInfo(DWORD *pNumberOfClusters,
  67. TSSD_ClusterInfo **ppClusterInfo)
  68. {
  69. BOOL rc = FALSE;
  70. RPC_BINDING_HANDLE rpcHandle;
  71. DWORD result;
  72. unsigned long RpcException;
  73. rpcHandle = OpenSDWMIServer();
  74. if (rpcHandle == NULL) {
  75. goto HandleError;
  76. }
  77. RpcTryExcept {
  78. // Make the call.
  79. result = TSSDRpcQueryAllClusterInfo(rpcHandle, pNumberOfClusters, ppClusterInfo);
  80. }
  81. RpcExcept(TRUE) {
  82. RpcException = RpcExceptionCode();
  83. ERR((TB,"TSSDRpcQueryAllClusterInfo: RPC Exception %d\n", RpcException));
  84. result = E_FAIL;
  85. }
  86. RpcEndExcept
  87. if (SUCCEEDED(result)) {
  88. rc = TRUE;
  89. }
  90. else {
  91. ERR((TB,"TSSDRpcQueryAllClusterInfo: RPC call failed, result=0x%X", result));
  92. }
  93. HandleError:
  94. return rc;
  95. }
  96. // wrapper to make RPC call: TSSDRpcQueryClusterInfo
  97. BOOL SDWMIQueryClusterInfo(WCHAR *ClusterName,
  98. DWORD *pNumberOfClusters,
  99. TSSD_ClusterInfo **ppClusterInfo)
  100. {
  101. BOOL rc = FALSE;
  102. DWORD result;
  103. RPC_BINDING_HANDLE rpcHandle;
  104. unsigned long RpcException;
  105. rpcHandle = OpenSDWMIServer();
  106. if (rpcHandle == NULL) {
  107. goto HandleError;
  108. }
  109. RpcTryExcept {
  110. // Make the call.
  111. result = TSSDRpcQueryClusterInfo(rpcHandle, ClusterName,
  112. pNumberOfClusters, ppClusterInfo);
  113. }
  114. RpcExcept(TRUE) {
  115. RpcException = RpcExceptionCode();
  116. ERR((TB,"TSSDRpcQueryClusterInfo: RPC Exception %d\n", RpcException));
  117. result = E_FAIL;
  118. }
  119. RpcEndExcept
  120. if (SUCCEEDED(result)) {
  121. rc = TRUE;
  122. }
  123. else {
  124. ERR((TB,"TSSDRpcQueryClusterInfo: RPC call failed, result=0x%X", result));
  125. }
  126. HandleError:
  127. return rc;
  128. }
  129. // wrapper to make RPC call: TSSDRpcQueryServerByName
  130. BOOL SDWMIQueryServerByName(WCHAR *ServerName,
  131. DWORD *pNumberOfServers,
  132. TSSD_ServerInfo **ppServerInfo)
  133. {
  134. BOOL rc = FALSE;
  135. RPC_BINDING_HANDLE rpcHandle;
  136. DWORD result;
  137. unsigned long RpcException;
  138. rpcHandle = OpenSDWMIServer();
  139. if (rpcHandle == NULL) {
  140. goto HandleError;
  141. }
  142. RpcTryExcept {
  143. // Make the call.
  144. result = TSSDRpcQueryServerByName(rpcHandle, ServerName, pNumberOfServers, ppServerInfo);
  145. }
  146. RpcExcept(TRUE) {
  147. RpcException = RpcExceptionCode();
  148. ERR((TB,"TSSDRpcQueryServerByName: RPC Exception %d\n", RpcException));
  149. result = E_FAIL;
  150. }
  151. RpcEndExcept
  152. if (SUCCEEDED(result)) {
  153. rc = TRUE;
  154. }
  155. else {
  156. ERR((TB,"TSSDRpcQueryServerByName: RPC call failed, result=0x%X", result));
  157. }
  158. HandleError:
  159. return rc;
  160. }
  161. // wrapper to make RPC call: TSSDRpcQueryServersInCluster
  162. BOOL SDWMIQueryServersInCluster(WCHAR *ClusterName,
  163. DWORD *pNumberOfServers,
  164. TSSD_ServerInfo **ppServerInfo)
  165. {
  166. BOOL rc = FALSE;
  167. RPC_BINDING_HANDLE rpcHandle;
  168. DWORD result;
  169. unsigned long RpcException;
  170. rpcHandle = OpenSDWMIServer();
  171. if (rpcHandle == NULL) {
  172. goto HandleError;
  173. }
  174. RpcTryExcept {
  175. // Make the call.
  176. result = TSSDRpcQueryServersInCluster(rpcHandle, ClusterName, pNumberOfServers, ppServerInfo);
  177. }
  178. RpcExcept(TRUE) {
  179. RpcException = RpcExceptionCode();
  180. ERR((TB,"TSSDRpcQueryServersInCluster: RPC Exception %d\n", RpcException));
  181. result = E_FAIL;
  182. }
  183. RpcEndExcept
  184. if (SUCCEEDED(result)) {
  185. rc = TRUE;
  186. }
  187. else {
  188. ERR((TB,"TSSDRpcQueryServersInCluster: RPC call failed, result=0x%X", result));
  189. }
  190. HandleError:
  191. return rc;
  192. }
  193. // wrapper to make RPC call: TSSDRpcQueryAllServers
  194. BOOL SDWMIQueryAllServers(DWORD *pNumberOfServers,
  195. TSSD_ServerInfo **ppServerInfo)
  196. {
  197. BOOL rc = FALSE;
  198. RPC_BINDING_HANDLE rpcHandle;
  199. DWORD result;
  200. unsigned long RpcException;
  201. rpcHandle = OpenSDWMIServer();
  202. if (rpcHandle == NULL) {
  203. goto HandleError;
  204. }
  205. RpcTryExcept {
  206. // Make the call.
  207. result = TSSDRpcQueryAllServers(rpcHandle, pNumberOfServers, ppServerInfo);
  208. }
  209. RpcExcept(TRUE) {
  210. RpcException = RpcExceptionCode();
  211. ERR((TB,"TSSDRpcQueryAllServers: RPC Exception %d\n", RpcException));
  212. result = E_FAIL;
  213. }
  214. RpcEndExcept
  215. if (SUCCEEDED(result)) {
  216. rc = TRUE;
  217. }
  218. else {
  219. ERR((TB,"TSSDRpcQueryAllServers: RPC call failed, result=0x%X", result));
  220. }
  221. HandleError:
  222. return rc;
  223. }
  224. // wrapper to make RPC call: TSSDRpcQuerySessionInfoByUserName
  225. BOOL SDWMIQuerySessionInfoByUserName(WCHAR *UserName,
  226. WCHAR *DomainName,
  227. DWORD *pNumberOfSessions,
  228. TSSD_SessionInfo **ppSessionInfo)
  229. {
  230. BOOL rc = FALSE;
  231. RPC_BINDING_HANDLE rpcHandle;
  232. DWORD result;
  233. unsigned long RpcException;
  234. rpcHandle = OpenSDWMIServer();
  235. if (rpcHandle == NULL) {
  236. goto HandleError;
  237. }
  238. RpcTryExcept {
  239. // Make the call.
  240. result = TSSDRpcQuerySessionInfoByUserName(rpcHandle, UserName, DomainName,
  241. pNumberOfSessions, ppSessionInfo);
  242. }
  243. RpcExcept(TRUE) {
  244. RpcException = RpcExceptionCode();
  245. ERR((TB,"TSSDRpcQuerySessionInfoByUserName: RPC Exception %d\n", RpcException));
  246. result = E_FAIL;
  247. }
  248. RpcEndExcept
  249. if (SUCCEEDED(result)) {
  250. rc = TRUE;
  251. }
  252. else {
  253. ERR((TB,"TSSDRpcQuerySessionInfoByUserName: RPC call failed, result=0x%X", result));
  254. }
  255. HandleError:
  256. return rc;
  257. }
  258. // wrapper to make RPC call: TSSDRpcQuerySessionInfoByServer
  259. BOOL SDWMIQuerySessionInfoByServer(WCHAR *ServerName,
  260. DWORD *pNumberOfSessions,
  261. TSSD_SessionInfo **ppSessionInfo)
  262. {
  263. BOOL rc = FALSE;
  264. RPC_BINDING_HANDLE rpcHandle;
  265. DWORD result;
  266. unsigned long RpcException;
  267. rpcHandle = OpenSDWMIServer();
  268. if (rpcHandle == NULL) {
  269. goto HandleError;
  270. }
  271. RpcTryExcept {
  272. // Make the call.
  273. result = TSSDRpcQuerySessionInfoByServer(rpcHandle, ServerName,
  274. pNumberOfSessions, ppSessionInfo);
  275. }
  276. RpcExcept(TRUE) {
  277. RpcException = RpcExceptionCode();
  278. ERR((TB,"TSSDRpcQuerySessionInfoByServer: RPC Exception %d\n", RpcException));
  279. result = E_FAIL;
  280. }
  281. RpcEndExcept
  282. if (SUCCEEDED(result)) {
  283. rc = TRUE;
  284. }
  285. else {
  286. ERR((TB,"TSSDRpcQuerySessionInfoByServer: RPC call failed, result=0x%X", result));
  287. }
  288. HandleError:
  289. return rc;
  290. }
  291. // TO DO: Replace "NameSpace" with the appropriate namespace for your
  292. // provider instance. For instance: "root\\default or "root\\cimv2".
  293. //===================================================================
  294. /***********************************************************************
  295. CWin32_SessionDirectoryCluster
  296. ************************************************************************/
  297. /*****************************************************************************
  298. *
  299. * FUNCTION : CWin32_SessionDirectoryCluster::CWin32_SessionDirectoryCluster
  300. * This class reads properties such as NumberOfServers, etc.
  301. *
  302. *****************************************************************************/
  303. CWin32_SessionDirectoryCluster::CWin32_SessionDirectoryCluster (LPCWSTR lpwszName, LPCWSTR lpwszNameSpace ) : Provider( lpwszName, lpwszNameSpace )
  304. {
  305. TRC2((TB, "CWin32_SessionDirectoryCluster_ctor"));
  306. _tcscpy(m_szClusterName, _T("ClusterName"));
  307. _tcscpy(m_szNumberOfServers, _T("NumberOfServers"));
  308. _tcscpy(m_szSingleSessionMode, _T("SingleSessionMode"));
  309. m_pClusterInfo = NULL;
  310. }
  311. //=-------------
  312. /*****************************************************************************
  313. *
  314. * FUNCTION : CWin32_SessionDirectoryCluster::~CWin32_SessionDirectoryCluster
  315. *****************************************************************************/
  316. CWin32_SessionDirectoryCluster::~CWin32_SessionDirectoryCluster ()
  317. {
  318. }
  319. //=-------------
  320. /*****************************************************************************
  321. *
  322. * FUNCTION : CWin32_SessionDirectoryCluster::EnumerateInstances
  323. *
  324. * DESCRIPTION : Returns all the instances of this class.
  325. *
  326. * INPUTS : A pointer to the MethodContext for communication with WinMgmt.
  327. * A long that contains the flags described in
  328. * IWbemServices::CreateInstanceEnumAsync. Note that the following
  329. * flags are handled by (and filtered out by) WinMgmt:
  330. * WBEM_FLAG_DEEP, WBEM_FLAG_SHALLOW, WBEM_FLAG_RETURN_IMMEDIATELY,
  331. * WBEM_FLAG_FORWARD_ONLY, WBEM_FLAG_BIDIRECTIONAL
  332. *
  333. * RETURNS : WBEM_S_NO_ERROR if successful
  334. *
  335. * COMMENTS : All instances on the machine are returned here and
  336. * all properties that this class knows how to populate must
  337. * be filled in. If there are no instances, return
  338. * WBEM_S_NO_ERROR.
  339. *****************************************************************************/
  340. HRESULT CWin32_SessionDirectoryCluster::EnumerateInstances (MethodContext* pMethodContext, long lFlags )
  341. {
  342. HRESULT hr = WBEM_E_NOT_FOUND;
  343. DWORD dwRequiredProperties = 0;
  344. //CHStringArray asNames;
  345. CHStringArray aszNames;
  346. DWORD i, NumberOfClusters = 0;
  347. WCHAR ClusterName[TSSD_NameLength];
  348. BOOL rc;
  349. ERR((TB, "EnumerateInstances in SDCluster"));
  350. rc = SDWMIQueryAllClusterInfo(&NumberOfClusters, &m_pClusterInfo);
  351. if (!rc || (NumberOfClusters == 0)) {
  352. goto HandleError;
  353. }
  354. //
  355. for(i=0; i<NumberOfClusters; i++)
  356. {
  357. CInstance* pInstance = CreateNewInstance(pMethodContext);
  358. if( pInstance == NULL)
  359. {
  360. ERR((TB, "CWin32_SessionDirectoryCluster@ExecQuery: CreateNewInstance failed"));
  361. hr = WBEM_E_OUT_OF_MEMORY;
  362. goto HandleError;
  363. }
  364. hr = LoadPropertyValues(pInstance, i, BIT_ALL_PROPERTIES);
  365. if( SUCCEEDED( hr ) )
  366. {
  367. hr = pInstance->Commit();
  368. }
  369. pInstance->Release();
  370. }
  371. if (m_pClusterInfo != NULL) {
  372. MIDL_user_free(m_pClusterInfo);
  373. m_pClusterInfo = NULL;
  374. }
  375. HandleError:
  376. return hr ;
  377. }
  378. //=---------
  379. /*****************************************************************************
  380. *
  381. * FUNCTION : CWin32_SessionDirectoryCluster::GetObject
  382. *
  383. * DESCRIPTION : Find a single instance based on the key property, the TerminalName.
  384. *
  385. * INPUTS : A pointer to a CInstance object containing the key properties.
  386. * A long that contains the flags described in
  387. * IWbemServices::GetObjectAsync.
  388. *
  389. * RETURNS : WBEM_S_NO_ERROR if the instance can be found
  390. * WBEM_E_NOT_FOUND if the instance described by the key properties
  391. * could not be found
  392. * WBEM_E_FAILED if the instance could be found but another error
  393. * occurred.
  394. *
  395. *****************************************************************************/
  396. HRESULT CWin32_SessionDirectoryCluster::GetObject ( CInstance* pInstance, long lFlags, CFrameworkQuery &Query )
  397. {
  398. HRESULT hr = WBEM_E_NOT_FOUND;
  399. CHString StrClusterName;
  400. DWORD dwRequiredProperties = 0;
  401. DWORD NumberOfClusters = 0;
  402. WCHAR ClusterName[TSSD_NameLength];
  403. BOOL rc;
  404. ERR((TB, "GetObject in SDCluster"));
  405. if( Query.IsPropertyRequired(m_szClusterName))
  406. dwRequiredProperties |= BIT_CLUSTERNAME;
  407. if (Query.IsPropertyRequired(m_szNumberOfServers))
  408. dwRequiredProperties |= BIT_NUMBEROFSERVERS;
  409. if (Query.IsPropertyRequired(m_szSingleSessionMode))
  410. dwRequiredProperties |= BIT_SINGLESESSIONMODE;
  411. // Get the key property
  412. pInstance->GetCHString(m_szClusterName, StrClusterName);
  413. wcsncpy(ClusterName, StrClusterName, TSSD_NameLength);
  414. ClusterName[TSSD_NameLength - 1] = L'\0';
  415. ERR((TB,"TSSDRpcQueryClusterInfo: Query cluster name: %S", ClusterName));
  416. rc = SDWMIQueryClusterInfo(ClusterName, &NumberOfClusters, &m_pClusterInfo);
  417. ERR((TB,"ExecQuery: Get numCluster is %d", NumberOfClusters));
  418. if (!rc || (NumberOfClusters == 0)) {
  419. goto HandleError;
  420. }
  421. hr = LoadPropertyValues(pInstance, 0, dwRequiredProperties);
  422. if (m_pClusterInfo != NULL) {
  423. MIDL_user_free(m_pClusterInfo);
  424. m_pClusterInfo = NULL;
  425. }
  426. HandleError:
  427. return hr ;
  428. }
  429. //=---------
  430. /*****************************************************************************
  431. *
  432. * FUNCTION : CWin32_SessionDirectoryCluster::ExecQuery
  433. *
  434. * DESCRIPTION : The method context is passed for use in the creation of
  435. * instances that satisfy the query, and a CFrameworkQuery
  436. * which describes the query. Create and populate all
  437. * instances which satisfy the query. You may return more
  438. * instances or more properties than are requested and WinMgmt
  439. * will post filter out any that do not apply.
  440. *
  441. * INPUTS : A pointer to the MethodContext for communication with WinMgmt.
  442. * A query object describing the query to satisfy.
  443. * A long that contains the flags described in
  444. * IWbemServices::CreateInstanceEnumAsync. Note that the following
  445. * flags are handled by (and filtered out by) WinMgmt:
  446. * WBEM_FLAG_FORWARD_ONLY
  447. * WBEM_FLAG_BIDIRECTIONAL
  448. * WBEM_FLAG_ENSURE_LOCATABLE
  449. *
  450. * RETURNS : WBEM_E_PROVIDER_NOT_CAPABLE if queries not supported for
  451. * this class or if the query is too complex for this class
  452. * to interpret. The framework will call the EnumerateInstances
  453. * function instead and let Winmgmt post filter.
  454. * WBEM_E_FAILED if the query failed
  455. * WBEM_S_NO_ERROR if query was successful
  456. *
  457. * COMMENTS : TO DO: Most providers will not need to implement this method. If you don't, WinMgmt
  458. * will call your enumerate function to get all the instances and perform the
  459. * filtering for you. Unless you expect SIGNIFICANT savings from implementing
  460. * queries, you should remove this method. You should also remove this method
  461. * if you are implementing a 'method only' provider.
  462. *
  463. *****************************************************************************/
  464. HRESULT CWin32_SessionDirectoryCluster::ExecQuery (MethodContext *pMethodContext, CFrameworkQuery& Query, long lFlags)
  465. {
  466. HRESULT hr = WBEM_E_NOT_FOUND, result;
  467. DWORD dwRequiredProperties = 0;
  468. //CHStringArray asNames;
  469. CHStringArray aszNames;
  470. DWORD i, NumberOfClusters = 0;
  471. WCHAR ClusterName[TSSD_NameLength];
  472. BOOL rc;
  473. //Query.GetValuesForProp(m_szClusterName, asNames);
  474. //BOOL bGetAllInstances = asNames.GetSize() == 0;
  475. // Method 1
  476. if (Query.IsPropertyRequired(m_szClusterName))
  477. dwRequiredProperties |= BIT_CLUSTERNAME;
  478. if (Query.IsPropertyRequired(m_szNumberOfServers))
  479. dwRequiredProperties |= BIT_NUMBEROFSERVERS;
  480. if (Query.IsPropertyRequired(m_szSingleSessionMode))
  481. dwRequiredProperties |= BIT_SINGLESESSIONMODE;
  482. result = Query.GetValuesForProp(m_szClusterName, aszNames);
  483. if ((result != WBEM_S_NO_ERROR) ||
  484. (aszNames.GetSize() == 0)) {
  485. // No query found
  486. goto HandleError;
  487. }
  488. wcsncpy(ClusterName, aszNames.GetAt(0), TSSD_NameLength);
  489. ClusterName[TSSD_NameLength - 1] = L'\0';
  490. ERR((TB,"TSSDRpcQueryClusterInfo: Query cluster name: %S", ClusterName));
  491. rc = SDWMIQueryClusterInfo(ClusterName, &NumberOfClusters, &m_pClusterInfo);
  492. ERR((TB,"ExecQuery: Get numCluster is %d", NumberOfClusters));
  493. if (!rc || (NumberOfClusters == 0)) {
  494. goto HandleError;
  495. }
  496. //
  497. for(i=0; i<NumberOfClusters; i++)
  498. {
  499. CInstance* pInstance = CreateNewInstance(pMethodContext);
  500. if( pInstance == NULL)
  501. {
  502. ERR((TB, "CWin32_SessionDirectoryCluster@ExecQuery: CreateNewInstance failed"));
  503. hr = WBEM_E_OUT_OF_MEMORY;
  504. goto HandleError;
  505. }
  506. hr = LoadPropertyValues(pInstance, i, dwRequiredProperties);
  507. if( SUCCEEDED( hr ) )
  508. {
  509. hr = pInstance->Commit();
  510. }
  511. pInstance->Release();
  512. }
  513. if (m_pClusterInfo != NULL) {
  514. MIDL_user_free(m_pClusterInfo);
  515. m_pClusterInfo = NULL;
  516. }
  517. HandleError:
  518. return hr;
  519. }
  520. BOOL CWin32_SessionDirectoryCluster::IsInList(const CHStringArray &asArray, LPCWSTR pszString)
  521. {
  522. DWORD dwSize = asArray.GetSize();
  523. for (DWORD x=0; x < dwSize; x++)
  524. {
  525. if( asArray[x].CompareNoCase(pszString) == 0 )
  526. {
  527. return TRUE;
  528. }
  529. }
  530. return FALSE;
  531. }
  532. //=---------
  533. /*************************************************************************************
  534. *
  535. * FUNCTION : CWin32_SessionDirectoryCluster::PutInstance
  536. *
  537. * DESCRIPTION : PutInstance is in provider classes that can
  538. * write instance information back to the registry.
  539. *
  540. * INPUTS : A pointer to a CInstance object containing the key
  541. * property - ClusterName
  542. *
  543. * A long that contains the flags described in
  544. * IWbemServices::PutInstanceAsync.
  545. *
  546. * RETURNS : WBEM_E_PROVIDER_NOT_CAPABLE if PutInstance is not available
  547. * WBEM_E_FAILED if there is an error delivering the instance
  548. * WBEM_E_INVALID_PARAMETER if any of the instance properties
  549. * are incorrect.
  550. * WBEM_S_NO_ERROR if instance is properly delivered
  551. *
  552. * COMMENTS :
  553. *
  554. ***************************************************************************************/
  555. HRESULT CWin32_SessionDirectoryCluster::PutInstance ( const CInstance &Instance, long lFlags)
  556. {
  557. return (WBEM_E_PROVIDER_NOT_CAPABLE);
  558. }
  559. //=---------
  560. /*****************************************************************************
  561. *
  562. * FUNCTION : CWin32_SessionDirectoryCluster::DeleteInstance
  563. *
  564. * DESCRIPTION : DeleteInstance, like PutInstance, actually writes information
  565. * to the software or hardware. For most hardware devices,
  566. * DeleteInstance should not be implemented, but for software
  567. * configuration, DeleteInstance implementation is plausible.
  568. *
  569. * INPUTS : A pointer to a CInstance object containing the key properties.
  570. * A long that contains the flags described in
  571. * IWbemServices::DeleteInstanceAsync.
  572. *
  573. * RETURNS : WBEM_E_PROVIDER_NOT_CAPABLE if DeleteInstance is not available.
  574. * WBEM_E_FAILED if there is an error deleting the instance.
  575. * WBEM_E_INVALID_PARAMETER if any of the instance properties
  576. * are incorrect.
  577. * WBEM_S_NO_ERROR if instance is properly deleted.
  578. *
  579. * COMMENTS : TO DO: If you don't intend to support deleting instances or are
  580. * creating a 'method only' provider, remove this method.
  581. *
  582. *****************************************************************************/
  583. HRESULT CWin32_SessionDirectoryCluster::DeleteInstance ( const CInstance &Instance, long lFlags )
  584. {
  585. return (WBEM_E_PROVIDER_NOT_CAPABLE);
  586. }
  587. //=---------
  588. /*****************************************************************************
  589. *
  590. * FUNCTION : CWin32_SessionDirectoryCluster::ExecMethod
  591. *
  592. * DESCRIPTION : Override this function to provide support for methods.
  593. * A method is an entry point for the user of your provider
  594. * to request your class perform some function above and
  595. * beyond a change of state. (A change of state should be
  596. * handled by PutInstance() )
  597. *
  598. * INPUTS : A pointer to a CInstance containing the instance the method was executed against.
  599. * A string containing the method name
  600. * A pointer to the CInstance which contains the IN parameters.
  601. * A pointer to the CInstance to contain the OUT parameters.
  602. * A set of specialized method flags
  603. *
  604. * RETURNS : WBEM_E_PROVIDER_NOT_CAPABLE if not implemented for this class
  605. * WBEM_S_NO_ERROR if method executes successfully
  606. * WBEM_E_FAILED if error occurs executing method
  607. *
  608. * COMMENTS : Provides method to configure the License type base on
  609. * the Terminal server mode, UseTempFolders, DeleteTempFolders
  610. * and Help that are group policy based.
  611. *
  612. *****************************************************************************/
  613. /*HRESULT CWin32_SessionDirectoryCluster::ExecMethod ( const CInstance& Inst,
  614. const BSTR bstrMethodName,
  615. CInstance *pInParams,
  616. CInstance *pOutParams,
  617. long lFlags)
  618. {
  619. DWORD dwData = 0;
  620. BOOL fRet = FALSE;
  621. bool bRet;
  622. HRESULT hr = WBEM_S_NO_ERROR;
  623. //If the method takes one or more parameters, require to check for validity of pInParams
  624. if(pInParams == NULL)
  625. {
  626. return WBEM_E_INVALID_METHOD_PARAMETERS;
  627. }
  628. if( _wcsicmp(bstrMethodName, m_szSetNumberOfSessions) == 0 )
  629. {
  630. bRet = pInParams->GetDWORD(m_szNumberOfSessions, dwData);
  631. if ( !bRet )
  632. {
  633. return WBEM_E_INVALID_PARAMETER;
  634. }
  635. // RPC function that sets the Number of Sessions
  636. TRC2( (TB, "CWin32_SessionDirectoryClusterSetting@ExecMethod returned 0x%x \n" , hr) );
  637. if( ERROR_SUCCESS == hr && pOutParams != NULL )
  638. {
  639. pOutParams->SetDWORD(L"ReturnValue", WBEM_S_NO_ERROR);
  640. }
  641. else
  642. {
  643. hr = WBEM_E_INVALID_OPERATION;
  644. }
  645. }
  646. return hr;
  647. }
  648. */
  649. //=---------
  650. HRESULT CWin32_SessionDirectoryCluster::LoadPropertyValues( CInstance *pInstance, DWORD dwIndex, DWORD dwRequiredProperties)
  651. {
  652. dwIndex;
  653. DWORD dwData = 0;
  654. DWORD dwSize = 0;
  655. BOOL bData = 0;
  656. BOOL bActivate = 0;
  657. DWORD dwStatus = 0;
  658. HRESULT hr = WBEM_E_INVALID_PARAMETER;
  659. // Your RPC call that gets all the data for the instance of interest. You could override the LoadPropertyValues method
  660. // to pass the data structure you got in GetObject, EnumerateInstances or ExecQuery
  661. if( dwRequiredProperties & BIT_CLUSTERNAME )
  662. {
  663. pInstance->SetCHString(m_szClusterName, m_pClusterInfo[dwIndex].ClusterName);
  664. ERR((TB,"UserName is %S", m_pClusterInfo[dwIndex].ClusterName));
  665. }
  666. if( dwRequiredProperties & BIT_NUMBEROFSERVERS )
  667. {
  668. pInstance->SetDWORD(m_szNumberOfServers, m_pClusterInfo[dwIndex].NumberOfServers);
  669. ERR((TB,"NumberofServers is %d", m_pClusterInfo[dwIndex].NumberOfServers));
  670. }
  671. if( dwRequiredProperties & BIT_SINGLESESSIONMODE )
  672. {
  673. pInstance->SetDWORD(m_szSingleSessionMode, m_pClusterInfo[dwIndex].SingleSessionMode ? 1 : 0);
  674. }
  675. return S_OK;
  676. }
  677. /***********************************************************************
  678. CWin32_SessionDirectoryServer
  679. ************************************************************************/
  680. /*****************************************************************************
  681. *
  682. * FUNCTION : CWin32_SessionDirectoryServer::CWin32_SessionDirectoryServer
  683. *
  684. *
  685. *****************************************************************************/
  686. CWin32_SessionDirectoryServer::CWin32_SessionDirectoryServer (LPCWSTR lpwszName, LPCWSTR lpwszNameSpace ) : Provider( lpwszName, lpwszNameSpace )
  687. {
  688. TRC2((TB, "CWin32_SessionDirectoryServer_ctor"));
  689. _tcscpy(m_szServerName, _T("ServerName"));
  690. _tcscpy(m_szServerIPAddress, _T("ServerIPAddress"));
  691. _tcscpy(m_szClusterName, _T("ClusterName"));
  692. _tcscpy(m_szNumberOfSessions, _T("NumberOfSessions"));
  693. _tcscpy(m_szSingleSessionMode, _T("SingleSessionMode"));
  694. m_pServerInfo = NULL;
  695. }
  696. /*****************************************************************************
  697. *
  698. * FUNCTION : CWin32_SessionDirectoryServer::~CWin32_SessionDirectoryServer
  699. *****************************************************************************/
  700. CWin32_SessionDirectoryServer::~CWin32_SessionDirectoryServer ()
  701. {
  702. }
  703. /*****************************************************************************
  704. *
  705. * FUNCTION : CWin32_SessionDirectoryServer::EnumerateInstances
  706. *
  707. * DESCRIPTION : Returns all the instances of this class.
  708. *
  709. * INPUTS : A pointer to the MethodContext for communication with WinMgmt.
  710. * A long that contains the flags described in
  711. * IWbemServices::CreateInstanceEnumAsync. Note that the following
  712. * flags are handled by (and filtered out by) WinMgmt:
  713. * WBEM_FLAG_DEEP, WBEM_FLAG_SHALLOW, WBEM_FLAG_RETURN_IMMEDIATELY,
  714. * WBEM_FLAG_FORWARD_ONLY, WBEM_FLAG_BIDIRECTIONAL
  715. *
  716. * RETURNS : WBEM_S_NO_ERROR if successful
  717. *
  718. * COMMENTS : All instances on the machine are returned here and
  719. * all properties that this class knows how to populate must
  720. * be filled in. If there are no instances, return
  721. * WBEM_S_NO_ERROR.
  722. *****************************************************************************/
  723. HRESULT CWin32_SessionDirectoryServer::EnumerateInstances (MethodContext* pMethodContext, long lFlags )
  724. {
  725. HRESULT hr = WBEM_E_NOT_FOUND;
  726. RPC_BINDING_HANDLE rpcHandle = NULL;
  727. DWORD i, NumberOfServers = 0;
  728. BOOL rc;
  729. ERR((TB, "EnumerateInstances in SDServer"));
  730. rc = SDWMIQueryAllServers(&NumberOfServers, &m_pServerInfo);
  731. if (!rc || (NumberOfServers == 0)) {
  732. goto HandleError;
  733. }
  734. for(i=0; i<NumberOfServers; i++)
  735. {
  736. CInstance* pInstance = CreateNewInstance(pMethodContext);
  737. if( pInstance == NULL)
  738. {
  739. ERR((TB, "CWin32_SessionDirectoryServer@ExecQuery: CreateNewInstance failed"));
  740. hr = WBEM_E_OUT_OF_MEMORY;
  741. goto HandleError;
  742. }
  743. hr = LoadPropertyValues(pInstance, i, BIT_ALL_PROPERTIES);
  744. if( SUCCEEDED( hr ) )
  745. {
  746. hr = pInstance->Commit();
  747. }
  748. pInstance->Release();
  749. }
  750. if (m_pServerInfo != NULL) {
  751. MIDL_user_free(m_pServerInfo);
  752. m_pServerInfo = NULL;
  753. }
  754. HandleError:
  755. return hr ;
  756. }
  757. /*****************************************************************************
  758. *
  759. * FUNCTION : CWin32_SessionDirectoryServer::GetObject
  760. *
  761. * DESCRIPTION : Find a single instance based on the key property, the TerminalName.
  762. *
  763. * INPUTS : A pointer to a CInstance object containing the key properties.
  764. * A long that contains the flags described in
  765. * IWbemServices::GetObjectAsync.
  766. *
  767. * RETURNS : WBEM_S_NO_ERROR if the instance can be found
  768. * WBEM_E_NOT_FOUND if the instance described by the key properties
  769. * could not be found
  770. * WBEM_E_FAILED if the instance could be found but another error
  771. * occurred.
  772. *
  773. *****************************************************************************/
  774. HRESULT CWin32_SessionDirectoryServer::GetObject ( CInstance* pInstance, long lFlags, CFrameworkQuery &Query )
  775. {
  776. HRESULT hr = WBEM_E_NOT_FOUND, result;
  777. DWORD dwRequiredProperties = 0;
  778. CHString StrServerName;
  779. DWORD i, NumberOfServers = 0;
  780. WCHAR ServerName[TSSD_NameLength];
  781. BOOL rc;
  782. if (Query.IsPropertyRequired(m_szServerName))
  783. dwRequiredProperties |= BIT_SERVERNAME;
  784. if (Query.IsPropertyRequired(m_szServerIPAddress))
  785. dwRequiredProperties |= BIT_SERVERIPADDRESS;
  786. if (Query.IsPropertyRequired(m_szClusterName))
  787. dwRequiredProperties |= BIT_CLUSTERNAME;
  788. if (Query.IsPropertyRequired(m_szNumberOfSessions))
  789. dwRequiredProperties |= BIT_NUMBEROFSESSIONS;
  790. if (Query.IsPropertyRequired(m_szSingleSessionMode))
  791. dwRequiredProperties |= BIT_SINGLESESSIONMODE;
  792. // Get the key property
  793. pInstance->GetCHString(m_szServerName, StrServerName);
  794. wcsncpy(ServerName, StrServerName, TSSD_NameLength);
  795. ServerName[TSSD_NameLength - 1] = L'\0';
  796. // Query by Server Name
  797. ERR((TB,"SDQueryServerInfo: Query server name: %S", ServerName));
  798. rc = SDWMIQueryServerByName(ServerName, &NumberOfServers, &m_pServerInfo);
  799. ERR((TB,"ExecQuery: Get numServer is %d", NumberOfServers));
  800. if (!rc || (NumberOfServers == 0)) {
  801. // Not found
  802. goto HandleError;
  803. }
  804. hr = LoadPropertyValues(pInstance, 0, dwRequiredProperties);
  805. if (m_pServerInfo != NULL) {
  806. MIDL_user_free(m_pServerInfo);
  807. m_pServerInfo = NULL;
  808. }
  809. HandleError:
  810. return hr;
  811. }
  812. /*****************************************************************************
  813. *
  814. * FUNCTION : CWin32_SessionDirectoryServer::ExecQuery
  815. *
  816. * DESCRIPTION : The method context is passed for use in the creation of
  817. * instances that satisfy the query, and a CFrameworkQuery
  818. * which describes the query. Create and populate all
  819. * instances which satisfy the query. You may return more
  820. * instances or more properties than are requested and WinMgmt
  821. * will post filter out any that do not apply.
  822. *
  823. * INPUTS : A pointer to the MethodContext for communication with WinMgmt.
  824. * A query object describing the query to satisfy.
  825. * A long that contains the flags described in
  826. * IWbemServices::CreateInstanceEnumAsync. Note that the following
  827. * flags are handled by (and filtered out by) WinMgmt:
  828. * WBEM_FLAG_FORWARD_ONLY
  829. * WBEM_FLAG_BIDIRECTIONAL
  830. * WBEM_FLAG_ENSURE_LOCATABLE
  831. *
  832. * RETURNS : WBEM_E_PROVIDER_NOT_CAPABLE if queries not supported for
  833. * this class or if the query is too complex for this class
  834. * to interpret. The framework will call the EnumerateInstances
  835. * function instead and let Winmgmt post filter.
  836. * WBEM_E_FAILED if the query failed
  837. * WBEM_S_NO_ERROR if query was successful
  838. *
  839. * COMMENTS : TO DO: Most providers will not need to implement this method. If you don't, WinMgmt
  840. * will call your enumerate function to get all the instances and perform the
  841. * filtering for you. Unless you expect SIGNIFICANT savings from implementing
  842. * queries, you should remove this method. You should also remove this method
  843. * if you are implementing a 'method only' provider.
  844. *
  845. *****************************************************************************/
  846. HRESULT CWin32_SessionDirectoryServer::ExecQuery (MethodContext *pMethodContext, CFrameworkQuery& Query, long lFlags)
  847. {
  848. HRESULT hr = WBEM_E_NOT_FOUND, result;
  849. DWORD dwRequiredProperties = 0;
  850. //CHStringArray asNames;
  851. CHStringArray aszNames;
  852. DWORD i, NumberOfServers = 0;
  853. WCHAR ServerName[TSSD_NameLength], ClusterName[TSSD_NameLength];
  854. BOOL rc;
  855. if (Query.IsPropertyRequired(m_szServerName))
  856. dwRequiredProperties |= BIT_SERVERNAME;
  857. if (Query.IsPropertyRequired(m_szServerIPAddress))
  858. dwRequiredProperties |= BIT_SERVERIPADDRESS;
  859. if (Query.IsPropertyRequired(m_szClusterName))
  860. dwRequiredProperties |= BIT_CLUSTERNAME;
  861. if (Query.IsPropertyRequired(m_szNumberOfSessions))
  862. dwRequiredProperties |= BIT_NUMBEROFSESSIONS;
  863. if (Query.IsPropertyRequired(m_szSingleSessionMode))
  864. dwRequiredProperties |= BIT_SINGLESESSIONMODE;
  865. // Query by Server Name
  866. result = Query.GetValuesForProp(m_szServerName, aszNames);
  867. if ((result == WBEM_S_NO_ERROR) &&
  868. (aszNames.GetSize() != 0)) {
  869. wcsncpy(ServerName, aszNames.GetAt(0), TSSD_NameLength);
  870. ServerName[TSSD_NameLength - 1] = L'\0';
  871. ERR((TB,"SDQueryServerInfo: Query server name: %S", ServerName));
  872. rc = SDWMIQueryServerByName(ServerName, &NumberOfServers, &m_pServerInfo);
  873. ERR((TB,"ExecQuery: Get numServer is %d", NumberOfServers));
  874. if (!rc || (NumberOfServers == 0)) {
  875. // Not found
  876. goto HandleError;
  877. }
  878. }
  879. else {
  880. result = Query.GetValuesForProp(m_szClusterName, aszNames);
  881. if ((result == WBEM_S_NO_ERROR) &&
  882. (aszNames.GetSize() != 0)) {
  883. wcsncpy(ClusterName, aszNames.GetAt(0), TSSD_NameLength);
  884. ClusterName[TSSD_NameLength - 1] = L'\0';
  885. ERR((TB,"SDQueryServerInfo: Query Cluster name: %S", ClusterName));
  886. rc = SDWMIQueryServersInCluster(ClusterName, &NumberOfServers, &m_pServerInfo);
  887. ERR((TB,"ExecQuery: Get numServer is %d", NumberOfServers));
  888. if (!rc || (NumberOfServers == 0)) {
  889. // Not found
  890. goto HandleError;
  891. }
  892. }
  893. }
  894. //
  895. for(i=0; i<NumberOfServers; i++)
  896. {
  897. CInstance* pInstance = CreateNewInstance(pMethodContext);
  898. if( pInstance == NULL)
  899. {
  900. ERR((TB, "CWin32_SessionDirectoryServer@ExecQuery: CreateNewInstance failed"));
  901. hr = WBEM_E_OUT_OF_MEMORY;
  902. goto HandleError;
  903. }
  904. hr = LoadPropertyValues(pInstance, i, dwRequiredProperties);
  905. if( SUCCEEDED( hr ) )
  906. {
  907. hr = pInstance->Commit();
  908. }
  909. pInstance->Release();
  910. }
  911. if (m_pServerInfo != NULL) {
  912. MIDL_user_free(m_pServerInfo);
  913. m_pServerInfo = NULL;
  914. }
  915. HandleError:
  916. return hr;
  917. }
  918. BOOL CWin32_SessionDirectoryServer::IsInList(const CHStringArray &asArray, LPCWSTR pszString)
  919. {
  920. DWORD dwSize = asArray.GetSize();
  921. for (DWORD x=0; x < dwSize; x++)
  922. {
  923. if( asArray[x].CompareNoCase(pszString) == 0 )
  924. {
  925. return TRUE;
  926. }
  927. }
  928. return FALSE;
  929. }
  930. /*************************************************************************************
  931. *
  932. * FUNCTION : CWin32_SessionDirectoryServer::PutInstance
  933. *
  934. * DESCRIPTION : PutInstance is in provider classes that can
  935. * write instance information back to the registry.
  936. *
  937. * INPUTS : A pointer to a CInstance object containing the key
  938. * property - ClusterName
  939. *
  940. * A long that contains the flags described in
  941. * IWbemServices::PutInstanceAsync.
  942. *
  943. * RETURNS : WBEM_E_PROVIDER_NOT_CAPABLE if PutInstance is not available
  944. * WBEM_E_FAILED if there is an error delivering the instance
  945. * WBEM_E_INVALID_PARAMETER if any of the instance properties
  946. * are incorrect.
  947. * WBEM_S_NO_ERROR if instance is properly delivered
  948. *
  949. * COMMENTS :
  950. *
  951. ***************************************************************************************/
  952. HRESULT CWin32_SessionDirectoryServer::PutInstance ( const CInstance &Instance, long lFlags)
  953. {
  954. return (WBEM_E_PROVIDER_NOT_CAPABLE);
  955. }
  956. /*****************************************************************************
  957. *
  958. * FUNCTION : CWin32_SessionDirectoryServer::DeleteInstance
  959. *
  960. * DESCRIPTION : DeleteInstance, like PutInstance, actually writes information
  961. * to the software or hardware. For most hardware devices,
  962. * DeleteInstance should not be implemented, but for software
  963. * configuration, DeleteInstance implementation is plausible.
  964. *
  965. * INPUTS : A pointer to a CInstance object containing the key properties.
  966. * A long that contains the flags described in
  967. * IWbemServices::DeleteInstanceAsync.
  968. *
  969. * RETURNS : WBEM_E_PROVIDER_NOT_CAPABLE if DeleteInstance is not available.
  970. * WBEM_E_FAILED if there is an error deleting the instance.
  971. * WBEM_E_INVALID_PARAMETER if any of the instance properties
  972. * are incorrect.
  973. * WBEM_S_NO_ERROR if instance is properly deleted.
  974. *
  975. * COMMENTS : TO DO: If you don't intend to support deleting instances or are
  976. * creating a 'method only' provider, remove this method.
  977. *
  978. *****************************************************************************/
  979. HRESULT CWin32_SessionDirectoryServer::DeleteInstance ( const CInstance &Instance, long lFlags )
  980. {
  981. return (WBEM_E_PROVIDER_NOT_CAPABLE);
  982. }
  983. HRESULT CWin32_SessionDirectoryServer::LoadPropertyValues( CInstance *pInstance, DWORD dwIndex, DWORD dwRequiredProperties)
  984. {
  985. DWORD dwData = 0;
  986. DWORD dwSize = 0;
  987. BOOL bData = 0;
  988. BOOL bActivate = 0;
  989. DWORD dwStatus = 0;
  990. HRESULT hr = WBEM_E_INVALID_PARAMETER;
  991. if( dwRequiredProperties & BIT_SERVERNAME )
  992. {
  993. pInstance->SetCHString(m_szServerName, m_pServerInfo[dwIndex].ServerName);
  994. ERR((TB,"Server is %S", m_pServerInfo[dwIndex].ServerName));
  995. }
  996. if( dwRequiredProperties & BIT_SERVERIPADDRESS )
  997. {
  998. pInstance->SetCHString(m_szServerIPAddress, m_pServerInfo[dwIndex].ServerIPAddress);
  999. ERR((TB,"ServerIP is %S", m_pServerInfo[dwIndex].ServerIPAddress));
  1000. }
  1001. if( dwRequiredProperties & BIT_CLUSTERNAME )
  1002. {
  1003. pInstance->SetCHString(m_szClusterName, m_pServerInfo[dwIndex].ClusterName);
  1004. ERR((TB,"ClusterName is %S", m_pServerInfo[dwIndex].ClusterName));
  1005. }
  1006. if( dwRequiredProperties & BIT_NUMBEROFSESSIONS )
  1007. {
  1008. pInstance->SetDWORD(m_szNumberOfSessions, m_pServerInfo[dwIndex].NumberOfSessions);
  1009. ERR((TB,"NumberofSessions is %d", m_pServerInfo[dwIndex].NumberOfSessions));
  1010. }
  1011. if( dwRequiredProperties & BIT_SINGLESESSIONMODE )
  1012. {
  1013. pInstance->SetDWORD(m_szSingleSessionMode, m_pServerInfo[dwIndex].SingleSessionMode ? 1 : 0);
  1014. }
  1015. return S_OK;
  1016. }
  1017. /***********************************************************************
  1018. CWin32_SessionDirectorySession
  1019. ************************************************************************/
  1020. /*****************************************************************************
  1021. *
  1022. * FUNCTION : CWin32_SessionDirectorySession::CWin32_SessionDirectorySession
  1023. *
  1024. *
  1025. *****************************************************************************/
  1026. CWin32_SessionDirectorySession::CWin32_SessionDirectorySession (LPCWSTR lpwszName, LPCWSTR lpwszNameSpace ) : Provider( lpwszName, lpwszNameSpace )
  1027. {
  1028. TRC2((TB, "CWin32_SessionDirectorySession_ctor"));
  1029. _tcscpy(m_szServerName, _T("ServerName"));
  1030. _tcscpy(m_szSessionID, _T("SessionID"));
  1031. _tcscpy(m_szUserName, _T("UserName"));
  1032. _tcscpy(m_szDomainName, _T("DomainName"));
  1033. _tcscpy(m_szServerIPAddress, _T("ServerIPAddress"));
  1034. _tcscpy(m_szTSProtocol, _T("TSProtocol"));
  1035. _tcscpy(m_szApplicationType, _T("ApplicationType"));
  1036. _tcscpy(m_szResolutionWidth, _T("ResolutionWidth"));
  1037. _tcscpy(m_szResolutionHeight, _T("ResolutionHeight"));
  1038. _tcscpy(m_szColorDepth, _T("ColorDepth"));
  1039. _tcscpy(m_szCreateTime, _T("CreateTime"));
  1040. _tcscpy(m_szDisconnectTime, _T("DisconnectTime"));
  1041. _tcscpy(m_szSessionState, _T("SessionState"));
  1042. m_pSessionInfo = NULL;
  1043. }
  1044. /*****************************************************************************
  1045. *
  1046. * FUNCTION : CWin32_SessionDirectorySession::~CWin32_SessionDirectorySession
  1047. *****************************************************************************/
  1048. CWin32_SessionDirectorySession::~CWin32_SessionDirectorySession ()
  1049. {
  1050. }
  1051. /*****************************************************************************
  1052. *
  1053. * FUNCTION : CWin32_SessionDirectorySession::EnumerateInstances
  1054. *
  1055. * DESCRIPTION : Returns all the instances of this class.
  1056. *
  1057. * INPUTS : A pointer to the MethodContext for communication with WinMgmt.
  1058. * A long that contains the flags described in
  1059. * IWbemServices::CreateInstanceEnumAsync. Note that the following
  1060. * flags are handled by (and filtered out by) WinMgmt:
  1061. * WBEM_FLAG_DEEP, WBEM_FLAG_SHALLOW, WBEM_FLAG_RETURN_IMMEDIATELY,
  1062. * WBEM_FLAG_FORWARD_ONLY, WBEM_FLAG_BIDIRECTIONAL
  1063. *
  1064. * RETURNS : WBEM_S_NO_ERROR if successful
  1065. *
  1066. * COMMENTS : All instances on the machine are returned here and
  1067. * all properties that this class knows how to populate must
  1068. * be filled in. If there are no instances, return
  1069. * WBEM_S_NO_ERROR.
  1070. *****************************************************************************/
  1071. HRESULT CWin32_SessionDirectorySession::EnumerateInstances (MethodContext* pMethodContext, long lFlags )
  1072. {
  1073. return (WBEM_E_PROVIDER_NOT_CAPABLE);
  1074. }
  1075. /*****************************************************************************
  1076. *
  1077. * FUNCTION : CWin32_SessionDirectorySession::GetObject
  1078. *
  1079. * DESCRIPTION : Find a single instance based on the key property, the TerminalName.
  1080. *
  1081. * INPUTS : A pointer to a CInstance object containing the key properties.
  1082. * A long that contains the flags described in
  1083. * IWbemServices::GetObjectAsync.
  1084. *
  1085. * RETURNS : WBEM_S_NO_ERROR if the instance can be found
  1086. * WBEM_E_NOT_FOUND if the instance described by the key properties
  1087. * could not be found
  1088. * WBEM_E_FAILED if the instance could be found but another error
  1089. * occurred.
  1090. *
  1091. *****************************************************************************/
  1092. HRESULT CWin32_SessionDirectorySession::GetObject ( CInstance* pInstance, long lFlags, CFrameworkQuery &Query )
  1093. {
  1094. return (WBEM_E_PROVIDER_NOT_CAPABLE);
  1095. }
  1096. /*****************************************************************************
  1097. *
  1098. * FUNCTION : CWin32_SessionDirectorySession::ExecQuery
  1099. *
  1100. * DESCRIPTION : The method context is passed for use in the creation of
  1101. * instances that satisfy the query, and a CFrameworkQuery
  1102. * which describes the query. Create and populate all
  1103. * instances which satisfy the query. You may return more
  1104. * instances or more properties than are requested and WinMgmt
  1105. * will post filter out any that do not apply.
  1106. *
  1107. * INPUTS : A pointer to the MethodContext for communication with WinMgmt.
  1108. * A query object describing the query to satisfy.
  1109. * A long that contains the flags described in
  1110. * IWbemServices::CreateInstanceEnumAsync. Note that the following
  1111. * flags are handled by (and filtered out by) WinMgmt:
  1112. * WBEM_FLAG_FORWARD_ONLY
  1113. * WBEM_FLAG_BIDIRECTIONAL
  1114. * WBEM_FLAG_ENSURE_LOCATABLE
  1115. *
  1116. * RETURNS : WBEM_E_PROVIDER_NOT_CAPABLE if queries not supported for
  1117. * this class or if the query is too complex for this class
  1118. * to interpret. The framework will call the EnumerateInstances
  1119. * function instead and let Winmgmt post filter.
  1120. * WBEM_E_FAILED if the query failed
  1121. * WBEM_S_NO_ERROR if query was successful
  1122. *
  1123. * COMMENTS : TO DO: Most providers will not need to implement this method. If you don't, WinMgmt
  1124. * will call your enumerate function to get all the instances and perform the
  1125. * filtering for you. Unless you expect SIGNIFICANT savings from implementing
  1126. * queries, you should remove this method. You should also remove this method
  1127. * if you are implementing a 'method only' provider.
  1128. *
  1129. *****************************************************************************/
  1130. HRESULT CWin32_SessionDirectorySession::ExecQuery (MethodContext *pMethodContext, CFrameworkQuery& Query, long lFlags)
  1131. {
  1132. HRESULT hr = WBEM_E_NOT_FOUND, result;
  1133. DWORD dwRequiredProperties = 0;
  1134. //CHStringArray asNames;
  1135. CHStringArray aszNames;
  1136. DWORD i, NumberOfSessions = 0;
  1137. WCHAR UserName[TSSD_NameLength], DomainName[TSSD_NameLength], ServerName[TSSD_NameLength];
  1138. BOOL rc;
  1139. BOOL bQueryByServerName = FALSE;
  1140. if (Query.IsPropertyRequired(m_szServerName))
  1141. dwRequiredProperties |= BIT_SERVERNAME;
  1142. if (Query.IsPropertyRequired(m_szSessionID))
  1143. dwRequiredProperties |= BIT_SESSIONID;
  1144. if (Query.IsPropertyRequired(m_szUserName))
  1145. dwRequiredProperties |= BIT_USERNAME;
  1146. if (Query.IsPropertyRequired(m_szDomainName))
  1147. dwRequiredProperties |= BIT_DOMAINNAME;
  1148. if (Query.IsPropertyRequired(m_szServerIPAddress))
  1149. dwRequiredProperties |= BIT_SERVERIPADDRESS;
  1150. if (Query.IsPropertyRequired(m_szTSProtocol))
  1151. dwRequiredProperties |= BIT_TSPROTOCOL;
  1152. if (Query.IsPropertyRequired(m_szApplicationType))
  1153. dwRequiredProperties |= BIT_APPLICATIONTYPE;
  1154. if (Query.IsPropertyRequired(m_szResolutionWidth))
  1155. dwRequiredProperties |= BIT_RESOLUTIONWIDTH;
  1156. if (Query.IsPropertyRequired(m_szResolutionHeight))
  1157. dwRequiredProperties |= BIT_RESOLUTIONHEIGHT;
  1158. if (Query.IsPropertyRequired(m_szColorDepth))
  1159. dwRequiredProperties |= BIT_COLORDEPTH;
  1160. if (Query.IsPropertyRequired(m_szCreateTime))
  1161. dwRequiredProperties |= BIT_CREATETIME;
  1162. if (Query.IsPropertyRequired(m_szDisconnectTime))
  1163. dwRequiredProperties |= BIT_DISCONNECTTIME;
  1164. if (Query.IsPropertyRequired(m_szSessionState))
  1165. dwRequiredProperties |= BIT_SESSIONSTATE;
  1166. // Get the server name
  1167. result = Query.GetValuesForProp(m_szServerName, aszNames);
  1168. if ((result == WBEM_S_NO_ERROR) &&
  1169. (aszNames.GetSize() != 0)) {
  1170. bQueryByServerName = TRUE;
  1171. wcsncpy(ServerName, aszNames.GetAt(0), TSSD_NameLength);
  1172. ServerName[TSSD_NameLength - 1] = L'\0';
  1173. ERR((TB, "Query sessions for servername %S", ServerName));
  1174. rc = SDWMIQuerySessionInfoByServer(ServerName, &NumberOfSessions, &m_pSessionInfo);
  1175. ERR((TB,"ExecQuery: Get numSession is %d", NumberOfSessions));
  1176. if (!rc || (NumberOfSessions == 0)) {
  1177. goto HandleError;
  1178. }
  1179. }
  1180. else {
  1181. // Get the user name
  1182. result = Query.GetValuesForProp(m_szUserName, aszNames);
  1183. if ((result != WBEM_S_NO_ERROR) ||
  1184. (aszNames.GetSize() == 0)) {
  1185. // No query found
  1186. goto HandleError;
  1187. }
  1188. wcsncpy(UserName, aszNames.GetAt(0), TSSD_NameLength);
  1189. UserName[TSSD_NameLength - 1] = L'\0';
  1190. ERR((TB, "Query UserName is %S", UserName));
  1191. // Get the domain name
  1192. result = Query.GetValuesForProp(m_szDomainName, aszNames);
  1193. if ((result != WBEM_S_NO_ERROR) ||
  1194. (aszNames.GetSize() == 0)) {
  1195. // No query found
  1196. goto HandleError;
  1197. }
  1198. wcsncpy(DomainName, aszNames.GetAt(0), TSSD_NameLength);
  1199. DomainName[TSSD_NameLength - 1] = L'\0';
  1200. ERR((TB, "Query DomainName is %S", DomainName));
  1201. rc = SDWMIQuerySessionInfoByUserName(UserName, DomainName, &NumberOfSessions, &m_pSessionInfo);
  1202. ERR((TB,"ExecQuery: Get numSession is %d", NumberOfSessions));
  1203. if (!rc || (NumberOfSessions == 0)) {
  1204. goto HandleError;
  1205. }
  1206. }
  1207. //
  1208. for(i=0; i<NumberOfSessions; i++)
  1209. {
  1210. CInstance* pInstance = CreateNewInstance(pMethodContext);
  1211. if( pInstance == NULL)
  1212. {
  1213. ERR((TB, "CWin32_SessionDirectorySession@ExecQuery: CreateNewInstance failed"));
  1214. hr = WBEM_E_OUT_OF_MEMORY;
  1215. goto HandleError;
  1216. }
  1217. hr = LoadPropertyValues(pInstance, i, dwRequiredProperties);
  1218. if( SUCCEEDED( hr ) )
  1219. {
  1220. hr = pInstance->Commit();
  1221. }
  1222. pInstance->Release();
  1223. }
  1224. if (m_pSessionInfo != NULL) {
  1225. MIDL_user_free(m_pSessionInfo);
  1226. m_pSessionInfo = NULL;
  1227. }
  1228. HandleError:
  1229. return hr;
  1230. }
  1231. BOOL CWin32_SessionDirectorySession::IsInList(const CHStringArray &asArray, LPCWSTR pszString)
  1232. {
  1233. DWORD dwSize = asArray.GetSize();
  1234. for (DWORD x=0; x < dwSize; x++)
  1235. {
  1236. if( asArray[x].CompareNoCase(pszString) == 0 )
  1237. {
  1238. return TRUE;
  1239. }
  1240. }
  1241. return FALSE;
  1242. }
  1243. /*************************************************************************************
  1244. *
  1245. * FUNCTION : CWin32_SessionDirectorySession::PutInstance
  1246. *
  1247. * DESCRIPTION : PutInstance is in provider classes that can
  1248. * write instance information back to the registry.
  1249. *
  1250. * INPUTS : A pointer to a CInstance object containing the key
  1251. * property - ClusterName
  1252. *
  1253. * A long that contains the flags described in
  1254. * IWbemServices::PutInstanceAsync.
  1255. *
  1256. * RETURNS : WBEM_E_PROVIDER_NOT_CAPABLE if PutInstance is not available
  1257. * WBEM_E_FAILED if there is an error delivering the instance
  1258. * WBEM_E_INVALID_PARAMETER if any of the instance properties
  1259. * are incorrect.
  1260. * WBEM_S_NO_ERROR if instance is properly delivered
  1261. *
  1262. * COMMENTS :
  1263. *
  1264. ***************************************************************************************/
  1265. HRESULT CWin32_SessionDirectorySession::PutInstance ( const CInstance &Instance, long lFlags)
  1266. {
  1267. return (WBEM_E_PROVIDER_NOT_CAPABLE);
  1268. }
  1269. /*****************************************************************************
  1270. *
  1271. * FUNCTION : CWin32_SessionDirectorySession::DeleteInstance
  1272. *
  1273. * DESCRIPTION : DeleteInstance, like PutInstance, actually writes information
  1274. * to the software or hardware. For most hardware devices,
  1275. * DeleteInstance should not be implemented, but for software
  1276. * configuration, DeleteInstance implementation is plausible.
  1277. *
  1278. * INPUTS : A pointer to a CInstance object containing the key properties.
  1279. * A long that contains the flags described in
  1280. * IWbemServices::DeleteInstanceAsync.
  1281. *
  1282. * RETURNS : WBEM_E_PROVIDER_NOT_CAPABLE if DeleteInstance is not available.
  1283. * WBEM_E_FAILED if there is an error deleting the instance.
  1284. * WBEM_E_INVALID_PARAMETER if any of the instance properties
  1285. * are incorrect.
  1286. * WBEM_S_NO_ERROR if instance is properly deleted.
  1287. *
  1288. * COMMENTS : TO DO: If you don't intend to support deleting instances or are
  1289. * creating a 'method only' provider, remove this method.
  1290. *
  1291. *****************************************************************************/
  1292. HRESULT CWin32_SessionDirectorySession::DeleteInstance ( const CInstance &Instance, long lFlags )
  1293. {
  1294. return (WBEM_E_PROVIDER_NOT_CAPABLE);
  1295. }
  1296. HRESULT CWin32_SessionDirectorySession::LoadPropertyValues( CInstance *pInstance, DWORD dwIndex, DWORD dwRequiredProperties)
  1297. {
  1298. BOOL rc;
  1299. HRESULT hr = WBEM_E_INVALID_PARAMETER;
  1300. if( dwRequiredProperties & BIT_SERVERNAME )
  1301. {
  1302. pInstance->SetCHString(m_szServerName, m_pSessionInfo[dwIndex].ServerName);
  1303. ERR((TB,"Server is %S", m_pSessionInfo[dwIndex].ServerName));
  1304. }
  1305. if( dwRequiredProperties & BIT_SESSIONID )
  1306. {
  1307. pInstance->SetDWORD(m_szSessionID, m_pSessionInfo[dwIndex].SessionID);
  1308. ERR((TB,"Session ID is %d", m_pSessionInfo[dwIndex].SessionID));
  1309. }
  1310. if( dwRequiredProperties & BIT_USERNAME )
  1311. {
  1312. pInstance->SetCHString(m_szUserName, m_pSessionInfo[dwIndex].UserName);
  1313. ERR((TB,"UserName is %S", m_pSessionInfo[dwIndex].UserName));
  1314. }
  1315. if( dwRequiredProperties & BIT_DOMAINNAME )
  1316. {
  1317. pInstance->SetCHString(m_szDomainName, m_pSessionInfo[dwIndex].DomainName);
  1318. ERR((TB,"DomainName is %S", m_pSessionInfo[dwIndex].DomainName));
  1319. }
  1320. if( dwRequiredProperties & BIT_SERVERIPADDRESS )
  1321. {
  1322. pInstance->SetCHString(m_szServerIPAddress, m_pSessionInfo[dwIndex].ServerIPAddress);
  1323. ERR((TB,"ServerIP is %S", m_pSessionInfo[dwIndex].ServerIPAddress));
  1324. }
  1325. if( dwRequiredProperties & BIT_TSPROTOCOL )
  1326. {
  1327. pInstance->SetDWORD(m_szTSProtocol, m_pSessionInfo[dwIndex].TSProtocol);
  1328. ERR((TB,"TSProtocol is %d", m_pSessionInfo[dwIndex].TSProtocol));
  1329. }
  1330. if( dwRequiredProperties & BIT_APPLICATIONTYPE )
  1331. {
  1332. pInstance->SetCHString(m_szApplicationType, m_pSessionInfo[dwIndex].ApplicationType);
  1333. ERR((TB,"AppType is %S", m_pSessionInfo[dwIndex].ApplicationType));
  1334. }
  1335. if( dwRequiredProperties & BIT_RESOLUTIONWIDTH )
  1336. {
  1337. pInstance->SetDWORD(m_szResolutionWidth, m_pSessionInfo[dwIndex].ResolutionWidth);
  1338. ERR((TB,"ResolutionWidth is %d", m_pSessionInfo[dwIndex].ResolutionWidth));
  1339. }
  1340. if( dwRequiredProperties & BIT_RESOLUTIONHEIGHT )
  1341. {
  1342. pInstance->SetDWORD(m_szResolutionHeight, m_pSessionInfo[dwIndex].ResolutionHeight);
  1343. ERR((TB,"ResolutionHeight is %d", m_pSessionInfo[dwIndex].ResolutionHeight));
  1344. }
  1345. if( dwRequiredProperties & BIT_COLORDEPTH )
  1346. {
  1347. pInstance->SetDWORD(m_szColorDepth, m_pSessionInfo[dwIndex].ColorDepth);
  1348. ERR((TB,"ColorDepth is %d", m_pSessionInfo[dwIndex].ColorDepth));
  1349. }
  1350. if( dwRequiredProperties & BIT_CREATETIME )
  1351. {
  1352. rc = pInstance->SetDateTime(m_szCreateTime, WBEMTime(m_pSessionInfo[dwIndex].CreateTime));
  1353. }
  1354. if( dwRequiredProperties & BIT_DISCONNECTTIME )
  1355. {
  1356. // Only set Disconnect time when the session is disconnected
  1357. if (m_pSessionInfo[dwIndex].SessionState != 0) {
  1358. pInstance->SetDateTime(m_szDisconnectTime, WBEMTime(m_pSessionInfo[dwIndex].DisconnectTime));
  1359. }
  1360. }
  1361. if( dwRequiredProperties & BIT_SESSIONSTATE )
  1362. {
  1363. pInstance->SetDWORD(m_szSessionState, m_pSessionInfo[dwIndex].SessionState ? 1 : 0);
  1364. ERR((TB,"SessionState is %d", m_pSessionInfo[dwIndex].SessionState));
  1365. }
  1366. return S_OK;
  1367. }