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.

235 lines
5.3 KiB

  1. //*************************************************************
  2. // File name: RSOPREG.CPP
  3. //
  4. // Description: A small command line utility that shows how
  5. // to query for all the registry policy objects
  6. // in a WMI namespace
  7. //
  8. //
  9. // Microsoft Confidential
  10. // Copyright (c) Microsoft Corporation 2000
  11. // All rights reserved
  12. //
  13. //*************************************************************
  14. #include <windows.h>
  15. #include <ole2.h>
  16. #include <wbemcli.h>
  17. #include <tchar.h>
  18. #include <stdio.h>
  19. //*************************************************************
  20. //
  21. // EnumObjects()
  22. //
  23. // Purpose: Enumerates the given namespace for all registry
  24. // policy objects
  25. //
  26. // Parameters: pIWbemServices - Interface pointer to the namespace
  27. //
  28. // Return: void
  29. //
  30. //*************************************************************
  31. void EnumObjects (IWbemServices * pIWbemServices)
  32. {
  33. BSTR pLanguage = NULL, pQuery = NULL, pValueName = NULL, pRegistryKey = NULL;
  34. IEnumWbemClassObject * pEnum;
  35. IWbemClassObject *pObjects[2];
  36. HRESULT hr;
  37. ULONG ulRet;
  38. VARIANT varRegistryKey, varValueName;
  39. ULONG ulCount = 0;
  40. //
  41. // Print heading
  42. //
  43. _tprintf (TEXT("\n\nRegistry objects in the RSOP\\User namespace:\n\n"));
  44. //
  45. // Allocate BSTRs for the query language and for the query itself
  46. //
  47. pLanguage = SysAllocString (TEXT("WQL"));
  48. pQuery = SysAllocString (TEXT("SELECT * FROM RSOP_RegistryPolicySetting"));
  49. //
  50. // Allocate BSTRs for the property names we want to retreive
  51. //
  52. pRegistryKey = SysAllocString (TEXT("registryKey"));
  53. pValueName = SysAllocString (TEXT("valueName"));
  54. //
  55. // Check if the allocations succeeded
  56. //
  57. if (pLanguage && pQuery && pRegistryKey && pValueName)
  58. {
  59. //
  60. // Execute the query
  61. //
  62. hr = pIWbemServices->ExecQuery (pLanguage, pQuery, WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
  63. NULL, &pEnum);
  64. if (SUCCEEDED(hr))
  65. {
  66. //
  67. // Loop through the results retreiving the registry key and value names
  68. //
  69. while (pEnum->Next(WBEM_INFINITE, 1, pObjects, &ulRet) == S_OK)
  70. {
  71. hr = pObjects[0]->Get (pRegistryKey, 0, &varRegistryKey, NULL, NULL);
  72. if (SUCCEEDED(hr))
  73. {
  74. hr = pObjects[0]->Get (pValueName, 0, &varValueName, NULL, NULL);
  75. if (SUCCEEDED(hr))
  76. {
  77. //
  78. // Print the key / value names
  79. //
  80. _tprintf (TEXT(" %s\\%s\n"), varRegistryKey.bstrVal, varValueName.bstrVal);
  81. VariantClear (&varValueName);
  82. }
  83. VariantClear (&varRegistryKey);
  84. }
  85. ulCount++;
  86. }
  87. if (ulCount == 0)
  88. {
  89. _tprintf (TEXT("\tNo registry objects found\n"));
  90. }
  91. pEnum->Release();
  92. }
  93. }
  94. if (pLanguage)
  95. {
  96. SysFreeString (pLanguage);
  97. }
  98. if (pQuery)
  99. {
  100. SysFreeString (pQuery);
  101. }
  102. if (pRegistryKey)
  103. {
  104. SysFreeString (pRegistryKey);
  105. }
  106. if (pValueName)
  107. {
  108. SysFreeString (pValueName);
  109. }
  110. }
  111. //*************************************************************
  112. //
  113. // main()
  114. //
  115. // Purpose: Entry point of this application
  116. //
  117. // Parameters: argc & argv
  118. //
  119. // Return: 0
  120. //
  121. //*************************************************************
  122. int __cdecl main( int argc, char *argv[])
  123. {
  124. IWbemLocator *pIWbemLocator = NULL;
  125. IWbemServices *pIWbemServices = NULL;
  126. BSTR pNamespace = NULL;
  127. HRESULT hr;
  128. //
  129. // Initialize COM
  130. //
  131. CoInitialize(NULL);
  132. //
  133. // Create the locator interface
  134. //
  135. hr = CoCreateInstance(CLSID_WbemLocator, NULL, CLSCTX_INPROC_SERVER,
  136. IID_IWbemLocator, (LPVOID *) &pIWbemLocator);
  137. if (hr != S_OK)
  138. {
  139. _tprintf(TEXT("CoCreateInstance failed with 0x%x\n"), hr);
  140. goto Exit;
  141. }
  142. //
  143. // Using the locator, connect to the RSOP user namespace
  144. //
  145. pNamespace = SysAllocString(TEXT("root\\rsop\\user"));
  146. if (pNamespace)
  147. {
  148. hr = pIWbemLocator->ConnectServer(pNamespace,
  149. NULL, //using current account for simplicity
  150. NULL, //using current password for simplicity
  151. 0L, // locale
  152. 0L, // securityFlags
  153. NULL, // authority (domain for NTLM)
  154. NULL, // context
  155. &pIWbemServices);
  156. if (hr != S_OK)
  157. {
  158. _tprintf(TEXT("ConnectServer failed with 0x%x\n"), hr);
  159. goto Exit;
  160. }
  161. EnumObjects (pIWbemServices);
  162. }
  163. Exit:
  164. if (pNamespace)
  165. {
  166. SysFreeString(pNamespace);
  167. }
  168. if (pIWbemServices)
  169. {
  170. pIWbemServices->Release();
  171. }
  172. if (pIWbemLocator)
  173. {
  174. pIWbemLocator->Release();
  175. }
  176. CoUninitialize ();
  177. return 0;
  178. }