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.

417 lines
8.0 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1995.
  5. //
  6. // File: winnt2var.cxx
  7. //
  8. // Contents: WinNT Object to Variant Copy Routines
  9. //
  10. // Functions:
  11. //
  12. // History: 06/12/96 RamV Created.
  13. // cloned off NDS conversion code.
  14. //
  15. // Issues: Check null ptrs for AllocADsMem and AllocADsStr
  16. //
  17. //----------------------------------------------------------------------------
  18. //
  19. // NTType objects copy code
  20. //
  21. #include "winnt.hxx"
  22. #pragma hdrstop
  23. #define INITGUID
  24. void
  25. VarTypeFreeVarObjects(
  26. PVARIANT pVarObject,
  27. DWORD dwNumValues
  28. )
  29. {
  30. DWORD i = 0;
  31. if( !pVarObject){
  32. return;
  33. }
  34. for (i = 0; i < dwNumValues; i++ ) {
  35. VariantClear(pVarObject + i);
  36. }
  37. FreeADsMem(pVarObject);
  38. return;
  39. }
  40. HRESULT
  41. NTTypeToVarTypeCopyOctetString(
  42. PNTOBJECT pNTSrcValue,
  43. PVARIANT lpVarDestObject
  44. )
  45. {
  46. HRESULT hr = S_OK;
  47. if(!lpVarDestObject){
  48. RRETURN(E_POINTER);
  49. }
  50. hr = BinaryToVariant(
  51. (pNTSrcValue->NTValue).octetstring.dwSize,
  52. (pNTSrcValue->NTValue).octetstring.pByte,
  53. lpVarDestObject
  54. );
  55. RRETURN(hr);
  56. }
  57. HRESULT
  58. NTTypeToVarTypeCopyBOOL(
  59. PNTOBJECT pNTSrcValue,
  60. PVARIANT lpVarDestObject
  61. )
  62. {
  63. HRESULT hr = S_OK;
  64. if(!lpVarDestObject){
  65. RRETURN(E_POINTER);
  66. }
  67. if(!pNTSrcValue){
  68. RRETURN(E_POINTER);
  69. }
  70. lpVarDestObject->vt = VT_BOOL;
  71. if((pNTSrcValue->NTValue).fValue){
  72. lpVarDestObject->boolVal = VARIANT_TRUE; // notation for TRUE in V_BOOL
  73. } else {
  74. lpVarDestObject->boolVal = VARIANT_FALSE;
  75. }
  76. RRETURN(hr);
  77. }
  78. HRESULT
  79. NTTypeToVarTypeCopySYSTEMTIME(
  80. PNTOBJECT pNTSrcValue,
  81. PVARIANT lpVarDestObject
  82. )
  83. {
  84. HRESULT hr;
  85. if(!pNTSrcValue){
  86. RRETURN(E_POINTER);
  87. }
  88. if(!lpVarDestObject){
  89. RRETURN(E_POINTER);
  90. }
  91. lpVarDestObject->vt = VT_DATE;
  92. hr = ConvertSystemTimeToDATE (pNTSrcValue->NTValue.stSystemTimeValue,
  93. &lpVarDestObject->date );
  94. RRETURN(hr);
  95. }
  96. HRESULT
  97. NTTypeToVarTypeCopyDWORD(
  98. PNTOBJECT pNTSrcValue,
  99. PVARIANT lpVarDestObject
  100. )
  101. {
  102. //
  103. // we cast the DWORD to a LONG
  104. //
  105. HRESULT hr = S_OK;
  106. if(!lpVarDestObject){
  107. RRETURN(E_POINTER);
  108. }
  109. if(!pNTSrcValue){
  110. RRETURN(E_POINTER);
  111. }
  112. lpVarDestObject->vt = VT_I4;
  113. lpVarDestObject->lVal = (LONG)(pNTSrcValue->NTValue).dwValue;
  114. RRETURN(hr);
  115. }
  116. HRESULT
  117. NTTypeToVarTypeCopyDATE70(
  118. PNTOBJECT pNTSrcValue,
  119. PVARIANT lpVarDestObject
  120. )
  121. {
  122. //
  123. // we cast the DWORD to a LONG
  124. //
  125. HRESULT hr = S_OK;
  126. if(!lpVarDestObject){
  127. RRETURN(E_POINTER);
  128. }
  129. if(!pNTSrcValue){
  130. RRETURN(E_POINTER);
  131. }
  132. lpVarDestObject->vt = VT_DATE;
  133. hr = ConvertDWORDtoDATE(
  134. (pNTSrcValue->NTValue).dwSeconds1970,
  135. &(lpVarDestObject->date)
  136. );
  137. RRETURN(hr);
  138. }
  139. HRESULT
  140. NTTypeToVarTypeCopyDATE(
  141. PNTOBJECT pNTSrcValue,
  142. PVARIANT lpVarDestObject
  143. )
  144. {
  145. HRESULT hr = S_OK;
  146. SYSTEMTIME stSystemTime;
  147. SYSTEMTIME LocalTime;
  148. DATE date;
  149. BOOL fRetval;
  150. GetSystemTime( &stSystemTime);
  151. fRetval = SystemTimeToTzSpecificLocalTime(
  152. NULL,
  153. &stSystemTime,
  154. &LocalTime
  155. );
  156. if(!fRetval){
  157. RRETURN(HRESULT_FROM_WIN32(GetLastError()));
  158. }
  159. stSystemTime.wHour = (WORD)(pNTSrcValue->NTValue.dwValue)/60;
  160. stSystemTime.wMinute = (WORD)(pNTSrcValue->NTValue.dwValue)%60;
  161. stSystemTime.wSecond =0;
  162. stSystemTime.wMilliseconds = 0;
  163. if(!lpVarDestObject){
  164. RRETURN(E_POINTER);
  165. }
  166. if(!pNTSrcValue){
  167. RRETURN(E_POINTER);
  168. }
  169. lpVarDestObject->vt = VT_DATE;
  170. hr = ConvertSystemTimeToDATE (stSystemTime,
  171. &date );
  172. BAIL_ON_FAILURE(hr);
  173. // We are casting date to DWORD and then subtracting it from date, which is double,
  174. // so that we only keep hours and minutes related info from the source DATE
  175. lpVarDestObject->date = date - (DWORD)date;
  176. error:
  177. RRETURN(hr);
  178. }
  179. HRESULT
  180. NTTypeToVarTypeCopyLPTSTR(
  181. PNTOBJECT pNTSrcValue,
  182. PVARIANT lpVarDestObject
  183. )
  184. {
  185. HRESULT hr = S_OK;
  186. if(!lpVarDestObject){
  187. RRETURN(E_POINTER);
  188. }
  189. lpVarDestObject->vt = VT_BSTR;
  190. if(!(pNTSrcValue->NTValue).pszValue){
  191. lpVarDestObject->bstrVal = NULL;
  192. hr = S_OK;
  193. goto error;
  194. }
  195. if(!pNTSrcValue){
  196. lpVarDestObject->bstrVal = NULL;
  197. } else {
  198. hr = ADsAllocString((pNTSrcValue->NTValue).pszValue,
  199. &(lpVarDestObject->bstrVal));
  200. }
  201. error:
  202. RRETURN(hr);
  203. }
  204. HRESULT
  205. NtTypeToVarTypeCopy(
  206. PNTOBJECT lpNtSrcObject,
  207. PVARIANT lpVarDestObject
  208. )
  209. {
  210. HRESULT hr = S_OK;
  211. switch (lpNtSrcObject->NTType) {
  212. case NT_SYNTAX_ID_BOOL:
  213. hr = NTTypeToVarTypeCopyBOOL(
  214. lpNtSrcObject,
  215. lpVarDestObject
  216. );
  217. break;
  218. case NT_SYNTAX_ID_SYSTEMTIME:
  219. hr = NTTypeToVarTypeCopySYSTEMTIME(
  220. lpNtSrcObject,
  221. lpVarDestObject
  222. );
  223. break;
  224. case NT_SYNTAX_ID_DWORD:
  225. hr = NTTypeToVarTypeCopyDWORD(
  226. lpNtSrcObject,
  227. lpVarDestObject
  228. );
  229. break;
  230. case NT_SYNTAX_ID_DATE:
  231. hr = NTTypeToVarTypeCopyDATE(
  232. lpNtSrcObject,
  233. lpVarDestObject
  234. );
  235. break;
  236. case NT_SYNTAX_ID_LPTSTR:
  237. hr = NTTypeToVarTypeCopyLPTSTR(
  238. lpNtSrcObject,
  239. lpVarDestObject
  240. );
  241. break;
  242. case NT_SYNTAX_ID_DelimitedString:
  243. hr = NTTypeToVarTypeCopyLPTSTR(
  244. lpNtSrcObject,
  245. lpVarDestObject
  246. );
  247. break;
  248. case NT_SYNTAX_ID_NulledString :
  249. hr = NTTypeToVarTypeCopyLPTSTR(
  250. lpNtSrcObject,
  251. lpVarDestObject
  252. );
  253. break;
  254. case NT_SYNTAX_ID_DATE_1970:
  255. hr = NTTypeToVarTypeCopyDATE70(
  256. lpNtSrcObject,
  257. lpVarDestObject
  258. );
  259. break;
  260. case NT_SYNTAX_ID_OCTETSTRING :
  261. hr = NTTypeToVarTypeCopyOctetString(
  262. lpNtSrcObject,
  263. lpVarDestObject
  264. );
  265. break;
  266. default:
  267. hr = E_FAIL;
  268. break;
  269. }
  270. RRETURN(hr);
  271. }
  272. HRESULT
  273. NtTypeToVarTypeCopyConstruct(
  274. LPNTOBJECT pNtSrcObjects,
  275. DWORD dwNumObjects,
  276. PVARIANT pVarDestObjects
  277. )
  278. {
  279. long i = 0;
  280. HRESULT hr = S_OK;
  281. VariantInit( pVarDestObjects );
  282. //
  283. // The following are for handling are multi-value properties
  284. //
  285. SAFEARRAY *aList = NULL;
  286. SAFEARRAYBOUND aBound;
  287. aBound.lLbound = 0;
  288. aBound.cElements = dwNumObjects;
  289. aList = SafeArrayCreate( VT_VARIANT, 1, &aBound );
  290. if ( aList == NULL )
  291. {
  292. hr = E_OUTOFMEMORY;
  293. BAIL_ON_FAILURE(hr);
  294. }
  295. for ( i = 0; i < (long) dwNumObjects; i++ )
  296. {
  297. VARIANT v;
  298. VariantInit(&v);
  299. hr = NtTypeToVarTypeCopy( pNtSrcObjects + i,
  300. &v );
  301. BAIL_ON_FAILURE(hr);
  302. hr = SafeArrayPutElement( aList, &i, &v );
  303. VariantClear(&v);
  304. BAIL_ON_FAILURE(hr);
  305. }
  306. V_VT(pVarDestObjects) = VT_ARRAY | VT_VARIANT;
  307. V_ARRAY(pVarDestObjects) = aList;
  308. RRETURN(S_OK);
  309. error:
  310. if ( aList )
  311. SafeArrayDestroy( aList );
  312. RRETURN(hr);
  313. }