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.

354 lines
7.1 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. //
  170. // SafeArrayPutElement actually copies the variant over,
  171. // this also copies the string so we need to clear our
  172. // copy.
  173. //
  174. VariantClear(&v);
  175. BAIL_ON_FAILURE(hr);
  176. }
  177. VariantInit( pvVariant );
  178. V_VT(pvVariant) = VT_ARRAY | VT_VARIANT;
  179. V_ARRAY(pvVariant) = aList;
  180. } else {
  181. aBound.lLbound = 0;
  182. aBound.cElements = 0;
  183. aList = SafeArrayCreate( VT_VARIANT, 1, &aBound );
  184. if ( aList == NULL ) {
  185. hr = E_OUTOFMEMORY;
  186. BAIL_ON_FAILURE(hr);
  187. }
  188. VariantInit( pvVariant );
  189. V_VT(pvVariant) = VT_ARRAY | VT_VARIANT;
  190. V_ARRAY(pvVariant) = aList;
  191. }
  192. return S_OK;
  193. error:
  194. if ( aList )
  195. SafeArrayDestroy( aList );
  196. return hr;
  197. }
  198. HRESULT
  199. UI1Array2IID(
  200. VARIANT var,
  201. IID *pIID
  202. )
  203. {
  204. LONG dwSLBound = 0;
  205. LONG dwSUBound = 0;
  206. VARIANT v;
  207. LONG i;
  208. HRESULT hr = S_OK;
  209. UCHAR pGUID[16];
  210. if ( !( (V_VT(&var) & VT_UI1) && V_ISARRAY(&var)) )
  211. BAIL_ON_FAILURE(hr = E_FAIL);
  212. // Check that there is only one dimension in this array
  213. if ((V_ARRAY(&var))->cDims != 1)
  214. BAIL_ON_FAILURE(hr = E_FAIL);
  215. // Check that there is at least one element in this array
  216. if ((V_ARRAY(&var))->rgsabound[0].cElements != 16) {
  217. DBGMSG(DBG_ERROR,("UI1Array, cElements != 16\n", (V_ARRAY(&var))->rgsabound[0].cElements));
  218. BAIL_ON_FAILURE(hr = E_FAIL);
  219. }
  220. // We know that this is a valid single dimension array
  221. hr = SafeArrayGetLBound(V_ARRAY(&var), 1, (long FAR *)&dwSLBound);
  222. BAIL_ON_FAILURE(hr);
  223. hr = SafeArrayGetUBound(V_ARRAY(&var), 1, (long FAR *)&dwSUBound);
  224. BAIL_ON_FAILURE(hr);
  225. for (i = dwSLBound; i <= dwSUBound; i++)
  226. {
  227. hr = SafeArrayGetElement(V_ARRAY(&var), (long FAR *)&i, pGUID + i - dwSLBound);
  228. if ( FAILED(hr) )
  229. continue;
  230. }
  231. *pIID = *(IID *) pGUID;
  232. hr = S_OK;
  233. error:
  234. return hr;
  235. }
  236. HRESULT
  237. UI1Array2SID(
  238. VARIANT var,
  239. LPWSTR *ppszSID
  240. )
  241. {
  242. HRESULT hr = S_OK;
  243. void HUGEP *pSID;
  244. if ( !( (V_VT(&var) & VT_UI1) && V_ISARRAY(&var)) )
  245. BAIL_ON_FAILURE(hr = E_FAIL);
  246. //
  247. // Check that there is only one dimension in this array
  248. //
  249. if ((V_ARRAY(&var))->cDims != 1)
  250. BAIL_ON_FAILURE(hr = E_FAIL);
  251. //
  252. // Check that there is at least one element in this array ( 28 is sizeof SID )
  253. //
  254. if ((V_ARRAY(&var))->rgsabound[0].cElements <= 0) {
  255. DBGMSG(DBG_ERROR,("UI1Array, cElements\n", (V_ARRAY(&var))->rgsabound[0].cElements));
  256. BAIL_ON_FAILURE(hr = E_FAIL);
  257. }
  258. hr = SafeArrayAccessData(V_ARRAY(&var),&pSID);
  259. BAIL_ON_FAILURE(hr);
  260. if (ConvertSidToStringSid(pSID, ppszSID)) {
  261. hr = S_OK;
  262. }
  263. SafeArrayUnaccessData(V_ARRAY(&var));
  264. error:
  265. return hr;
  266. }