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.

358 lines
6.9 KiB

  1. //---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1996
  5. //
  6. // File: varconv.c
  7. //
  8. // Contents: Ansi to Unicode conversions
  9. //
  10. // History: SWilson Nov 1996
  11. //----------------------------------------------------------------------------
  12. #include <precomp.h>
  13. #pragma hdrstop
  14. HRESULT
  15. PackString2Variant(
  16. LPCWSTR lpszData,
  17. VARIANT * pvData
  18. )
  19. {
  20. BSTR bstrData = NULL;
  21. WCHAR String[] = L"";
  22. LPCWSTR pStr;
  23. pStr = lpszData ? lpszData : (LPCWSTR) String;
  24. VariantInit(pvData);
  25. V_VT(pvData) = VT_BSTR;
  26. bstrData = SysAllocString(pStr);
  27. if (!bstrData) {
  28. return MAKE_HRESULT(SEVERITY_ERROR,
  29. FACILITY_WIN32,
  30. ERROR_OUTOFMEMORY);
  31. }
  32. V_VT(pvData) = VT_BSTR;
  33. V_BSTR(pvData) = bstrData;
  34. return ERROR_SUCCESS;
  35. }
  36. HRESULT
  37. UnpackStringfromVariant(
  38. VARIANT varSrcData,
  39. BSTR * pbstrDestString
  40. )
  41. {
  42. HRESULT hr = S_OK;
  43. if( V_VT(&varSrcData) != VT_BSTR) {
  44. return E_ADS_CANT_CONVERT_DATATYPE;
  45. }
  46. if (!V_BSTR(&varSrcData)) {
  47. *pbstrDestString = NULL;
  48. return S_OK;
  49. }
  50. *pbstrDestString = SysAllocString(V_BSTR(&varSrcData));
  51. return hr;
  52. }
  53. HRESULT
  54. UnpackDispatchfromVariant(
  55. VARIANT varSrcData,
  56. IDispatch **ppDispatch
  57. )
  58. {
  59. HRESULT hr = S_OK;
  60. if( V_VT(&varSrcData) != VT_DISPATCH) {
  61. return E_ADS_CANT_CONVERT_DATATYPE;
  62. }
  63. if (!V_DISPATCH(&varSrcData)) {
  64. *ppDispatch = NULL;
  65. return S_OK;
  66. }
  67. *ppDispatch = V_DISPATCH(&varSrcData);
  68. return hr;
  69. }
  70. HRESULT
  71. PackDispatch2Variant(
  72. IDispatch *pDispatch,
  73. VARIANT *pvData
  74. )
  75. {
  76. if (!pvData)
  77. return E_FAIL;
  78. V_VT(pvData) = VT_DISPATCH;
  79. V_DISPATCH(pvData) = pDispatch;
  80. return S_OK;
  81. }
  82. HRESULT
  83. PackDWORD2Variant(
  84. DWORD dwData,
  85. VARIANT * pvData
  86. )
  87. {
  88. if (!pvData) {
  89. return(E_FAIL);
  90. }
  91. V_VT(pvData) = VT_I4;
  92. V_I4(pvData) = dwData;
  93. return S_OK;
  94. }
  95. HRESULT
  96. UnpackDWORDfromVariant(
  97. VARIANT varSrcData,
  98. DWORD *pdwData
  99. )
  100. {
  101. if( V_VT(&varSrcData) != VT_I4) {
  102. return E_ADS_CANT_CONVERT_DATATYPE;
  103. }
  104. *pdwData = V_I4(&varSrcData);
  105. return S_OK;
  106. }
  107. HRESULT
  108. PackBOOL2Variant(
  109. BOOL fData,
  110. VARIANT * pvData
  111. )
  112. {
  113. V_VT(pvData) = VT_BOOL;
  114. V_BOOL(pvData) = (BYTE) fData;
  115. return S_OK;
  116. }
  117. HRESULT
  118. PackVARIANTinVariant(
  119. VARIANT vaValue,
  120. VARIANT *pvarInputData
  121. )
  122. {
  123. VariantInit(pvarInputData);
  124. V_VT(pvarInputData) = VT_VARIANT;
  125. return VariantCopy( pvarInputData, &vaValue );
  126. }
  127. HRESULT
  128. MakeVariantFromStringArray(
  129. BSTR *bstrList,
  130. VARIANT *pvVariant
  131. )
  132. {
  133. HRESULT hr = S_OK;
  134. SAFEARRAY *aList = NULL;
  135. SAFEARRAYBOUND aBound;
  136. long i = 0;
  137. long j = 0;
  138. long nCount;
  139. if ( (bstrList != NULL) && (*bstrList != 0) ) {
  140. for (nCount = 0 ; bstrList[nCount] ; ++nCount)
  141. ;
  142. if ( nCount == 1 ) {
  143. VariantInit( pvVariant );
  144. V_VT(pvVariant) = VT_BSTR;
  145. if (!(V_BSTR(pvVariant) = SysAllocString( bstrList[0]))) {
  146. hr = E_OUTOFMEMORY;
  147. BAIL_ON_FAILURE(hr);
  148. }
  149. return hr;
  150. }
  151. aBound.lLbound = 0;
  152. aBound.cElements = nCount;
  153. aList = SafeArrayCreate( VT_VARIANT, 1, &aBound );
  154. if ( aList == NULL ) {
  155. hr = E_OUTOFMEMORY;
  156. BAIL_ON_FAILURE(hr);
  157. }
  158. for (i = 0 ; bstrList[i] ; ++i) {
  159. VARIANT v;
  160. VariantInit(&v);
  161. V_VT(&v) = VT_BSTR;
  162. if (!(V_BSTR(&v) = SysAllocString(bstrList[i]))) {
  163. hr = E_OUTOFMEMORY;
  164. BAIL_ON_FAILURE(hr);
  165. }
  166. hr = SafeArrayPutElement( aList,
  167. &i,
  168. &v );
  169. BAIL_ON_FAILURE(hr);
  170. }
  171. VariantInit( pvVariant );
  172. V_VT(pvVariant) = VT_ARRAY | VT_VARIANT;
  173. V_ARRAY(pvVariant) = aList;
  174. } else {
  175. aBound.lLbound = 0;
  176. aBound.cElements = 0;
  177. aList = SafeArrayCreate( VT_VARIANT, 1, &aBound );
  178. if ( aList == NULL ) {
  179. hr = E_OUTOFMEMORY;
  180. BAIL_ON_FAILURE(hr);
  181. }
  182. VariantInit( pvVariant );
  183. V_VT(pvVariant) = VT_ARRAY | VT_VARIANT;
  184. V_ARRAY(pvVariant) = aList;
  185. }
  186. return S_OK;
  187. error:
  188. if ( aList )
  189. SafeArrayDestroy( aList );
  190. return hr;
  191. }
  192. HRESULT
  193. PrintVariantArray(VARIANT var)
  194. {
  195. LONG dwSLBound = 0;
  196. LONG dwSUBound = 0;
  197. VARIANT v;
  198. LONG i;
  199. HRESULT hr = S_OK;
  200. if ( !( (V_VT(&var) & VT_VARIANT) && V_ISARRAY(&var)) )
  201. BAIL_ON_FAILURE(hr = E_FAIL);
  202. // Check that there is only one dimension in this array
  203. if ((V_ARRAY(&var))->cDims != 1)
  204. BAIL_ON_FAILURE(hr = E_FAIL);
  205. // Check that there is atleast one element in this array
  206. if ((V_ARRAY(&var))->rgsabound[0].cElements == 0)
  207. BAIL_ON_FAILURE(hr = E_FAIL);
  208. // We know that this is a valid single dimension array
  209. hr = SafeArrayGetLBound(V_ARRAY(&var), 1, (long FAR *)&dwSLBound);
  210. BAIL_ON_FAILURE(hr);
  211. hr = SafeArrayGetUBound(V_ARRAY(&var), 1, (long FAR *)&dwSUBound);
  212. BAIL_ON_FAILURE(hr);
  213. for (i = dwSLBound; i <= dwSUBound; i++)
  214. {
  215. VariantInit(&v);
  216. hr = SafeArrayGetElement(V_ARRAY(&var), (long FAR *)&i, &v);
  217. if ( FAILED(hr) )
  218. continue;
  219. DBGMSG(DBG_INFO,("%ws\n", v.bstrVal));
  220. }
  221. hr = S_OK;
  222. error:
  223. return hr;
  224. }
  225. HRESULT
  226. UI1Array2IID(
  227. VARIANT var,
  228. IID *pIID
  229. )
  230. {
  231. LONG dwSLBound = 0;
  232. LONG dwSUBound = 0;
  233. VARIANT v;
  234. LONG i;
  235. HRESULT hr = S_OK;
  236. UCHAR pGUID[16];
  237. if ( !( (V_VT(&var) & VT_UI1) && V_ISARRAY(&var)) )
  238. BAIL_ON_FAILURE(hr = E_FAIL);
  239. // Check that there is only one dimension in this array
  240. //DBGMSG(DBG_TRACE,("UI1Array, cDims = %d\n", (V_ARRAY(&var))->cDims));
  241. if ((V_ARRAY(&var))->cDims != 1)
  242. BAIL_ON_FAILURE(hr = E_FAIL);
  243. // Check that there is at least one element in this array
  244. if ((V_ARRAY(&var))->rgsabound[0].cElements != 16) {
  245. DBGMSG(DBG_ERROR,("UI1Array, cElements != 16\n", (V_ARRAY(&var))->rgsabound[0].cElements));
  246. BAIL_ON_FAILURE(hr = E_FAIL);
  247. }
  248. // We know that this is a valid single dimension array
  249. hr = SafeArrayGetLBound(V_ARRAY(&var), 1, (long FAR *)&dwSLBound);
  250. BAIL_ON_FAILURE(hr);
  251. hr = SafeArrayGetUBound(V_ARRAY(&var), 1, (long FAR *)&dwSUBound);
  252. BAIL_ON_FAILURE(hr);
  253. for (i = dwSLBound; i <= dwSUBound; i++)
  254. {
  255. hr = SafeArrayGetElement(V_ARRAY(&var), (long FAR *)&i, pGUID + i - dwSLBound);
  256. if ( FAILED(hr) )
  257. continue;
  258. //DBGMSG(DBG_TRACE,("UI1Array, GUID: %0x\n", pGUID[i - dwSLBound]));
  259. }
  260. *pIID = *(IID *) pGUID;
  261. hr = S_OK;
  262. error:
  263. return hr;
  264. }