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.

317 lines
9.4 KiB

  1. #ifndef _PROPUTIL_H
  2. #define _PROPUTIL_H
  3. #ifndef __wizchain_h__
  4. #include "wizchain.h"
  5. #endif
  6. // delete
  7. inline HRESULT DeleteProperty (IPropertyPagePropertyBag * pPPPBag, LPOLESTR szGUID)
  8. {
  9. USES_CONVERSION;
  10. if( !pPPPBag || !szGUID ) return E_POINTER;
  11. HRESULT hr = S_OK;
  12. BSTR szGuid = SysAllocString (szGUID);
  13. if (!szGuid)
  14. hr = E_OUTOFMEMORY;
  15. else {
  16. VARIANT var;
  17. VariantInit (&var);
  18. hr = pPPPBag->SetProperty (szGuid, &var, PPPBAG_TYPE_DELETION);
  19. SysFreeString (szGuid);
  20. }
  21. return hr;
  22. }
  23. // readers
  24. inline HRESULT ReadInt4 (IPropertyPagePropertyBag * pPPPBag, LPOLESTR szGUID, long * pl, BOOL * pbReadOnly)
  25. {
  26. USES_CONVERSION;
  27. if( !pPPPBag || !szGUID || !pl || !pbReadOnly ) return E_POINTER;
  28. *pbReadOnly = FALSE; // default
  29. HRESULT hr = S_OK;
  30. BSTR szGuid = SysAllocString (szGUID);
  31. if (!szGuid)
  32. hr = E_OUTOFMEMORY;
  33. else {
  34. VARIANT var;
  35. VariantInit (&var);
  36. PPPBAG_TYPE dwFlags = PPPBAG_TYPE_UNINITIALIZED;
  37. BOOL bIsOwner = FALSE;
  38. hr = pPPPBag->GetProperty (szGuid, &var, &dwFlags, &bIsOwner);
  39. if (hr == S_OK) {
  40. *pbReadOnly = FALSE;
  41. if (dwFlags == PPPBAG_TYPE_READONLY)
  42. if (bIsOwner == FALSE)
  43. *pbReadOnly = TRUE;
  44. if (V_VT(&var) == VT_I4) // make sure variant is a i4
  45. *pl = V_I4(&var);
  46. else
  47. if (V_VT(&var) == VT_I2) // VBScript uses i2
  48. *pl = V_I2(&var);
  49. else
  50. hr = E_UNEXPECTED;
  51. VariantClear (&var);
  52. }
  53. SysFreeString (szGuid);
  54. }
  55. return hr;
  56. }
  57. inline HRESULT ReadString (IPropertyPagePropertyBag * pPPPBag, LPOLESTR szGUID, CString& str, BOOL * pbReadOnly)
  58. {
  59. USES_CONVERSION;
  60. if( !pPPPBag || !szGUID || !pbReadOnly ) return E_POINTER;
  61. *pbReadOnly = FALSE; // default
  62. HRESULT hr = S_OK;
  63. BSTR szGuid = SysAllocString (szGUID);
  64. if (!szGuid)
  65. hr = E_OUTOFMEMORY;
  66. else {
  67. VARIANT var;
  68. VariantInit (&var);
  69. PPPBAG_TYPE dwFlags = PPPBAG_TYPE_UNINITIALIZED;
  70. BOOL bIsOwner = FALSE;
  71. hr = pPPPBag->GetProperty (szGuid, &var, &dwFlags, &bIsOwner);
  72. if (hr == S_OK) {
  73. if (V_VT(&var) != VT_BSTR) // make sure variant is a bstr
  74. hr = E_UNEXPECTED;
  75. else {
  76. *pbReadOnly = FALSE;
  77. if (dwFlags == PPPBAG_TYPE_READONLY)
  78. if (bIsOwner == FALSE)
  79. *pbReadOnly = TRUE;
  80. str = V_BSTR(&var);
  81. }
  82. VariantClear (&var);
  83. }
  84. SysFreeString (szGuid);
  85. }
  86. return hr;
  87. }
  88. inline HRESULT ReadBool (IPropertyPagePropertyBag * pPPPBag, LPOLESTR szGUID, BOOL * pb, BOOL * pbReadOnly)
  89. {
  90. USES_CONVERSION;
  91. if( !pPPPBag || !szGUID || !pb || !pbReadOnly) return E_POINTER;
  92. *pbReadOnly = FALSE; // defaults
  93. HRESULT hr = S_OK;
  94. BSTR szGuid = SysAllocString (szGUID);
  95. if ( NULL == szGuid )
  96. hr = E_OUTOFMEMORY;
  97. else {
  98. VARIANT var;
  99. VariantInit (&var);
  100. PPPBAG_TYPE dwFlags = PPPBAG_TYPE_UNINITIALIZED;
  101. BOOL bIsOwner = FALSE;
  102. hr = pPPPBag->GetProperty (szGuid, &var, &dwFlags, &bIsOwner);
  103. if ( S_OK == hr ) {
  104. if ( VT_BOOL != V_VT( &var )) // make sure variant is a bool
  105. hr = E_INVALIDARG;
  106. else {
  107. *pbReadOnly = FALSE;
  108. if ( PPPBAG_TYPE_READONLY == dwFlags )
  109. if ( FALSE == bIsOwner )
  110. *pbReadOnly = TRUE;
  111. if ( VARIANT_FALSE == V_BOOL( &var ))
  112. *pb = FALSE;
  113. else
  114. *pb = TRUE;
  115. }
  116. VariantClear (&var);
  117. }
  118. SysFreeString (szGuid);
  119. }
  120. return hr;
  121. }
  122. inline HRESULT ReadSafeArray (IPropertyPagePropertyBag * pPPPBag, LPOLESTR szGUID, SAFEARRAY ** ppSA, BOOL * pbReadOnly)
  123. {
  124. USES_CONVERSION;
  125. if( !pPPPBag || !szGUID || !ppSA || !pbReadOnly ) return E_POINTER;
  126. *ppSA = NULL;
  127. *pbReadOnly = FALSE; // defaults
  128. HRESULT hr = S_OK;
  129. BSTR szGuid = SysAllocString (szGUID);
  130. if (!szGuid)
  131. hr = E_OUTOFMEMORY;
  132. else {
  133. VARIANT var;
  134. VariantInit (&var);
  135. PPPBAG_TYPE dwFlags = PPPBAG_TYPE_UNINITIALIZED;
  136. BOOL bIsOwner = FALSE;
  137. hr = pPPPBag->GetProperty (szGuid, &var, &dwFlags, &bIsOwner);
  138. if (hr == S_OK) {
  139. if (!(V_VT(&var) & VT_ARRAY))// make sure variant is a safearray
  140. hr = E_UNEXPECTED;
  141. else {
  142. *pbReadOnly = FALSE;
  143. if (dwFlags == PPPBAG_TYPE_READONLY)
  144. if (bIsOwner == FALSE)
  145. *pbReadOnly = TRUE;
  146. if (V_VT(&var) & VT_BYREF) // VBScript does stuff byref
  147. hr = SafeArrayCopy (*V_ARRAYREF(&var), ppSA);
  148. else
  149. hr = SafeArrayCopy (V_ARRAY(&var), ppSA);
  150. }
  151. VariantClear (&var);
  152. }
  153. SysFreeString (szGuid);
  154. }
  155. return hr;
  156. }
  157. inline HRESULT ReadVariant (IPropertyPagePropertyBag * pPPPBag, LPOLESTR szGUID, VARIANT& var, BOOL * pbReadOnly)
  158. {
  159. USES_CONVERSION;
  160. if( !pPPPBag || !szGUID || !pbReadOnly ) return E_POINTER;
  161. *pbReadOnly = FALSE; // default
  162. HRESULT hr = S_OK;
  163. BSTR szGuid = SysAllocString (szGUID);
  164. if (!szGuid)
  165. hr = E_OUTOFMEMORY;
  166. else {
  167. VariantInit (&var);
  168. PPPBAG_TYPE dwFlags = PPPBAG_TYPE_UNINITIALIZED;
  169. BOOL bIsOwner = FALSE;
  170. hr = pPPPBag->GetProperty (szGuid, &var, &dwFlags, &bIsOwner);
  171. if (hr == S_OK) {
  172. *pbReadOnly = FALSE;
  173. if (dwFlags == PPPBAG_TYPE_READONLY)
  174. if (bIsOwner == FALSE)
  175. *pbReadOnly = TRUE;
  176. }
  177. SysFreeString (szGuid);
  178. }
  179. return hr;
  180. }
  181. // writers
  182. inline HRESULT WriteInt4 (IPropertyPagePropertyBag * pPPPBag, LPOLESTR szGUID, long l, BOOL bReadOnly)
  183. {
  184. USES_CONVERSION;
  185. if( !pPPPBag || !szGUID ) return E_POINTER;
  186. HRESULT hr = S_OK;
  187. BSTR szGuid = SysAllocString (szGUID);
  188. if (!szGuid)
  189. hr = E_OUTOFMEMORY;
  190. else {
  191. VARIANT var;
  192. VariantInit (&var);
  193. V_VT(&var) = VT_I4;
  194. V_INT(&var) = l; // signed int (I4, I hope)
  195. hr = pPPPBag->SetProperty (szGuid, &var, bReadOnly == FALSE ? PPPBAG_TYPE_READWRITE : PPPBAG_TYPE_READONLY);
  196. VariantClear (&var);
  197. SysFreeString (szGuid);
  198. }
  199. return hr;
  200. }
  201. inline HRESULT WriteString (IPropertyPagePropertyBag * pPPPBag, LPOLESTR szGUID, CString& str, BOOL bReadOnly)
  202. {
  203. USES_CONVERSION;
  204. if( !pPPPBag || !szGUID ) return E_POINTER;
  205. HRESULT hr = S_OK;
  206. BSTR szGuid = SysAllocString (szGUID);
  207. if (!szGuid)
  208. hr = E_OUTOFMEMORY;
  209. else {
  210. BSTR bstr = SysAllocString (T2BSTR(str));
  211. if (!bstr)
  212. hr = E_OUTOFMEMORY;
  213. else {
  214. VARIANT var;
  215. VariantInit (&var);
  216. V_VT(&var) = VT_BSTR;
  217. V_BSTR(&var) = bstr;
  218. hr = pPPPBag->SetProperty (szGuid, &var, bReadOnly == FALSE ? PPPBAG_TYPE_READWRITE : PPPBAG_TYPE_READONLY);
  219. VariantClear (&var);
  220. // the line above frees the bstr.
  221. }
  222. SysFreeString (szGuid);
  223. }
  224. return hr;
  225. }
  226. inline HRESULT WriteBool (IPropertyPagePropertyBag * pPPPBag, LPOLESTR szGUID, BOOL b, BOOL bReadOnly)
  227. {
  228. USES_CONVERSION;
  229. if( !pPPPBag || !szGUID ) return E_POINTER;
  230. HRESULT hr = S_OK;
  231. BSTR szGuid = SysAllocString (szGUID);
  232. if (!szGuid)
  233. hr = E_OUTOFMEMORY;
  234. else {
  235. VARIANT var;
  236. VariantInit (&var);
  237. V_VT(&var) = VT_BOOL;
  238. V_BOOL(&var) = b == FALSE ? VARIANT_FALSE : VARIANT_TRUE;
  239. hr = pPPPBag->SetProperty (szGuid, &var, bReadOnly == FALSE ? PPPBAG_TYPE_READWRITE : PPPBAG_TYPE_READONLY);
  240. VariantClear (&var);
  241. SysFreeString (szGuid);
  242. }
  243. return hr;
  244. }
  245. inline HRESULT WriteSafeArray (IPropertyPagePropertyBag * pPPPBag, LPOLESTR szGUID, SAFEARRAY * pSA, BOOL bReadOnly)
  246. {
  247. USES_CONVERSION;
  248. if( !pPPPBag || !szGUID || !pSA ) return E_POINTER;
  249. HRESULT hr = S_OK;
  250. BSTR szGuid = SysAllocString (szGUID);
  251. if (!szGuid)
  252. hr = E_OUTOFMEMORY;
  253. else {
  254. VARIANT var;
  255. VariantInit (&var);
  256. V_VT(&var) = VT_ARRAY | VT_VARIANT;
  257. SAFEARRAY * pSACopy = NULL;
  258. hr = SafeArrayCopy (pSA, &pSACopy);
  259. if (pSACopy) {
  260. V_ARRAY(&var) = pSACopy;
  261. hr = pPPPBag->SetProperty (szGuid, &var, bReadOnly == FALSE ? PPPBAG_TYPE_READWRITE : PPPBAG_TYPE_READONLY);
  262. // DON'T call VariantClear!
  263. }
  264. SysFreeString (szGuid);
  265. }
  266. return hr;
  267. }
  268. inline HRESULT WriteVariant (IPropertyPagePropertyBag * pPPPBag, LPOLESTR szGUID, VARIANT var, BOOL bReadOnly)
  269. {
  270. USES_CONVERSION;
  271. if( !pPPPBag || !szGUID ) return E_POINTER;
  272. HRESULT hr = S_OK;
  273. BSTR szGuid = SysAllocString (szGUID);
  274. if (!szGuid)
  275. hr = E_OUTOFMEMORY;
  276. else {
  277. hr = pPPPBag->SetProperty (szGuid, &var, bReadOnly == FALSE ? PPPBAG_TYPE_READWRITE : PPPBAG_TYPE_READONLY);
  278. SysFreeString (szGuid);
  279. }
  280. return hr;
  281. }
  282. #endif