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.

270 lines
5.4 KiB

  1. //---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996
  5. //
  6. // File: proputil.cxx
  7. //
  8. // Contents: Property Listing Utilities
  9. //
  10. // History: 08-05-96 t-danal created
  11. //
  12. //----------------------------------------------------------------------------
  13. //
  14. // ********* System Includes
  15. //
  16. #define UNICODE
  17. #define _UNICODE
  18. #define INC_OLE2
  19. // #define _OLEAUT32_
  20. // #define SECURITY_WIN32
  21. #include <windows.h>
  22. //
  23. // ********* CRunTime Includes
  24. //
  25. #include <stdlib.h>
  26. #include <limits.h>
  27. #include <io.h>
  28. #include <stdio.h>
  29. //
  30. // ********* Public ADs includes
  31. //
  32. #include <activeds.h>
  33. #include "macro.hxx"
  34. #include "proputil.hxx"
  35. HRESULT
  36. PrintVariant(
  37. VARIANT varPropData
  38. )
  39. {
  40. HRESULT hr;
  41. BSTR bstrValue;
  42. switch (varPropData.vt) {
  43. case VT_I4:
  44. printf("\t%d", varPropData.lVal);
  45. break;
  46. case VT_BSTR:
  47. printf("\t%S", varPropData.bstrVal);
  48. break;
  49. case VT_BOOL:
  50. printf("\t%d", V_BOOL(&varPropData));
  51. break;
  52. case (VT_ARRAY | VT_VARIANT):
  53. PrintVariantArray(varPropData);
  54. break;
  55. case VT_DATE:
  56. hr = VarBstrFromDate(
  57. varPropData.date,
  58. LOCALE_SYSTEM_DEFAULT,
  59. LOCALE_NOUSEROVERRIDE,
  60. &bstrValue
  61. );
  62. printf("\t%S", bstrValue);
  63. break;
  64. default:
  65. printf("\tData type is %d\n", varPropData.vt);
  66. break;
  67. }
  68. printf("\n");
  69. return(S_OK);
  70. }
  71. HRESULT
  72. PrintVariantArray(
  73. VARIANT var
  74. )
  75. {
  76. LONG dwSLBound = 0;
  77. LONG dwSUBound = 0;
  78. VARIANT v;
  79. LONG i;
  80. HRESULT hr = S_OK;
  81. if(!((V_VT(&var) & VT_VARIANT) && V_ISARRAY(&var))) {
  82. return(E_FAIL);
  83. }
  84. //
  85. // Check that there is only one dimension in this array
  86. //
  87. if ((V_ARRAY(&var))->cDims != 1) {
  88. hr = E_FAIL;
  89. BAIL_ON_FAILURE(hr);
  90. }
  91. //
  92. // Check that there is atleast one element in this array
  93. //
  94. if ((V_ARRAY(&var))->rgsabound[0].cElements == 0){
  95. hr = E_FAIL;
  96. BAIL_ON_FAILURE(hr);
  97. }
  98. //
  99. // We know that this is a valid single dimension array
  100. //
  101. hr = SafeArrayGetLBound(V_ARRAY(&var),
  102. 1,
  103. (long FAR *)&dwSLBound
  104. );
  105. BAIL_ON_FAILURE(hr);
  106. hr = SafeArrayGetUBound(V_ARRAY(&var),
  107. 1,
  108. (long FAR *)&dwSUBound
  109. );
  110. BAIL_ON_FAILURE(hr);
  111. printf("\t");
  112. for (i = dwSLBound; i <= dwSUBound; i++) {
  113. VariantInit(&v);
  114. hr = SafeArrayGetElement(V_ARRAY(&var),
  115. (long FAR *)&i,
  116. &v
  117. );
  118. if (FAILED(hr)) {
  119. continue;
  120. }
  121. if (i < dwSUBound) {
  122. printf("%S, ", v.bstrVal);
  123. }else{
  124. printf("%S",v.bstrVal);
  125. }
  126. }
  127. return(S_OK);
  128. error:
  129. return(hr);
  130. }
  131. HRESULT
  132. PrintProperty(
  133. BSTR bstrPropName,
  134. HRESULT hRetVal,
  135. VARIANT varPropData
  136. )
  137. {
  138. HRESULT hr = S_OK;
  139. switch (hRetVal) {
  140. case 0:
  141. printf("Property: %S", bstrPropName);
  142. PrintVariant(varPropData);
  143. break;
  144. case E_ADS_CANT_CONVERT_DATATYPE:
  145. printf("Property: %S", bstrPropName);
  146. printf("\tProperty data could not be converted to variant\n");
  147. break;
  148. default:
  149. printf("Property: %S", bstrPropName);
  150. printf("\tProperty not available in cache\n");
  151. break;
  152. }
  153. return(hr);
  154. }
  155. HRESULT
  156. GetPropertyList(
  157. IADs * pADs,
  158. VARIANT ** ppVariant,
  159. PDWORD pcElementFetched
  160. )
  161. {
  162. HRESULT hr= S_OK;
  163. BSTR bstrSchemaPath = NULL;
  164. IADsContainer * pADsContainer = NULL;
  165. IEnumVARIANT * pEnumVariant = NULL;
  166. BOOL fContinue = TRUE;
  167. ULONG cthisElement = 0L;
  168. ULONG cElementFetched = 0L;
  169. LPBYTE pVariantArray = NULL;
  170. VARIANT varProperty;
  171. DWORD cb = 0;
  172. hr = pADs->get_Schema(&bstrSchemaPath);
  173. BAIL_ON_FAILURE(hr);
  174. hr = ADsGetObject(
  175. bstrSchemaPath,
  176. IID_IADsContainer,
  177. (void **)&pADsContainer
  178. );
  179. BAIL_ON_FAILURE(hr);
  180. hr = ADsBuildEnumerator(
  181. pADsContainer,
  182. &pEnumVariant
  183. );
  184. BAIL_ON_FAILURE(hr);
  185. while (fContinue) {
  186. VariantInit(&varProperty);
  187. hr = ADsEnumerateNext(
  188. pEnumVariant,
  189. 1,
  190. &varProperty,
  191. &cthisElement
  192. );
  193. if (hr == S_FALSE) {
  194. fContinue = FALSE;
  195. break;
  196. }
  197. pVariantArray = (LPBYTE)realloc(
  198. (void *)pVariantArray,
  199. cb + sizeof(VARIANT)
  200. );
  201. if (!pVariantArray) {
  202. hr = E_OUTOFMEMORY;
  203. BAIL_ON_FAILURE(hr);
  204. }
  205. memcpy(pVariantArray + cb, (LPBYTE)&varProperty, sizeof(VARIANT));
  206. cb += sizeof(VARIANT);
  207. cElementFetched += cthisElement;
  208. }
  209. *ppVariant = (LPVARIANT)pVariantArray;
  210. *pcElementFetched = cElementFetched;
  211. error:
  212. if (bstrSchemaPath) {
  213. SysFreeString(bstrSchemaPath);
  214. }
  215. if (pADsContainer) {
  216. pADsContainer->Release();
  217. }
  218. return(hr);
  219. }