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.

351 lines
6.5 KiB

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