Added YUV routines needed for v4l driver, and in the future possibly
[wine/gsoc-2012-control.git] / dlls / oleaut32 / variant.c
blobf57f0df77b2cde44684732fdebdb8c7d4577fa98
1 /*
2 * VARIANT
4 * Copyright 1998 Jean-Claude Cote
5 * Copyright 2003 Jon Griffiths
6 * Copyright 2005 Daniel Remenak
8 * The alorithm for conversion from Julian days to day/month/year is based on
9 * that devised by Henry Fliegel, as implemented in PostgreSQL, which is
10 * Copyright 1994-7 Regents of the University of California
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "config.h"
29 #include <string.h>
30 #include <stdlib.h>
31 #include <stdarg.h>
33 #define COBJMACROS
34 #define NONAMELESSUNION
35 #define NONAMELESSSTRUCT
37 #include "windef.h"
38 #include "winbase.h"
39 #include "wine/unicode.h"
40 #include "winerror.h"
41 #include "variant.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(variant);
46 const char* wine_vtypes[VT_CLSID] =
48 "VT_EMPTY","VT_NULL","VT_I2","VT_I4","VT_R4","VT_R8","VT_CY","VT_DATE",
49 "VT_BSTR","VT_DISPATCH","VT_ERROR","VT_BOOL","VT_VARIANT","VT_UNKNOWN",
50 "VT_DECIMAL","15","VT_I1","VT_UI1","VT_UI2","VT_UI4","VT_I8","VT_UI8",
51 "VT_INT","VT_UINT","VT_VOID","VT_HRESULT","VT_PTR","VT_SAFEARRAY",
52 "VT_CARRAY","VT_USERDEFINED","VT_LPSTR","VT_LPWSTR""32","33","34","35",
53 "VT_RECORD","VT_INT_PTR","VT_UINT_PTR","39","40","41","42","43","44","45",
54 "46","47","48","49","50","51","52","53","54","55","56","57","58","59","60",
55 "61","62","63","VT_FILETIME","VT_BLOB","VT_STREAM","VT_STORAGE",
56 "VT_STREAMED_OBJECT","VT_STORED_OBJECT","VT_BLOB_OBJECT","VT_CF","VT_CLSID"
59 const char* wine_vflags[16] =
61 "",
62 "|VT_VECTOR",
63 "|VT_ARRAY",
64 "|VT_VECTOR|VT_ARRAY",
65 "|VT_BYREF",
66 "|VT_VECTOR|VT_ARRAY",
67 "|VT_ARRAY|VT_BYREF",
68 "|VT_VECTOR|VT_ARRAY|VT_BYREF",
69 "|VT_HARDTYPE",
70 "|VT_VECTOR|VT_HARDTYPE",
71 "|VT_ARRAY|VT_HARDTYPE",
72 "|VT_VECTOR|VT_ARRAY|VT_HARDTYPE",
73 "|VT_BYREF|VT_HARDTYPE",
74 "|VT_VECTOR|VT_ARRAY|VT_HARDTYPE",
75 "|VT_ARRAY|VT_BYREF|VT_HARDTYPE",
76 "|VT_VECTOR|VT_ARRAY|VT_BYREF|VT_HARDTYPE",
79 /* Convert a variant from one type to another */
80 static inline HRESULT VARIANT_Coerce(VARIANTARG* pd, LCID lcid, USHORT wFlags,
81 VARIANTARG* ps, VARTYPE vt)
83 HRESULT res = DISP_E_TYPEMISMATCH;
84 VARTYPE vtFrom = V_TYPE(ps);
85 BOOL bIgnoreOverflow = FALSE;
86 DWORD dwFlags = 0;
88 TRACE("(%p->(%s%s),0x%08lx,0x%04x,%p->(%s%s),%s%s)\n", pd, debugstr_VT(pd),
89 debugstr_VF(pd), lcid, wFlags, ps, debugstr_VT(ps), debugstr_VF(ps),
90 debugstr_vt(vt), debugstr_vf(vt));
92 if (vt == VT_BSTR || vtFrom == VT_BSTR)
94 /* All flags passed to low level function are only used for
95 * changing to or from strings. Map these here.
97 if (wFlags & VARIANT_LOCALBOOL)
98 dwFlags |= VAR_LOCALBOOL;
99 if (wFlags & VARIANT_CALENDAR_HIJRI)
100 dwFlags |= VAR_CALENDAR_HIJRI;
101 if (wFlags & VARIANT_CALENDAR_THAI)
102 dwFlags |= VAR_CALENDAR_THAI;
103 if (wFlags & VARIANT_CALENDAR_GREGORIAN)
104 dwFlags |= VAR_CALENDAR_GREGORIAN;
105 if (wFlags & VARIANT_NOUSEROVERRIDE)
106 dwFlags |= LOCALE_NOUSEROVERRIDE;
107 if (wFlags & VARIANT_USE_NLS)
108 dwFlags |= LOCALE_USE_NLS;
111 /* Map int/uint to i4/ui4 */
112 if (vt == VT_INT)
113 vt = VT_I4;
114 else if (vt == VT_UINT)
115 vt = VT_UI4;
117 if (vtFrom == VT_INT)
118 vtFrom = VT_I4;
119 else if (vtFrom == VT_UINT)
121 vtFrom = VT_UI4;
122 if (vt == VT_I4)
123 bIgnoreOverflow = TRUE;
126 if (vt == vtFrom)
127 return VariantCopy(pd, ps);
129 if (wFlags & VARIANT_NOVALUEPROP && vtFrom == VT_DISPATCH && vt != VT_UNKNOWN)
131 /* VARIANT_NOVALUEPROP prevents IDispatch objects from being coerced by
132 * accessing the default object property.
134 return DISP_E_TYPEMISMATCH;
137 switch (vt)
139 case VT_EMPTY:
140 if (vtFrom == VT_NULL)
141 return DISP_E_TYPEMISMATCH;
142 /* ... Fall through */
143 case VT_NULL:
144 if (vtFrom <= VT_UINT && vtFrom != (VARTYPE)15 && vtFrom != VT_ERROR)
146 res = VariantClear( pd );
147 if (vt == VT_NULL && SUCCEEDED(res))
148 V_VT(pd) = VT_NULL;
150 return res;
152 case VT_I1:
153 switch (vtFrom)
155 case VT_EMPTY: V_I1(pd) = 0; return S_OK;
156 case VT_I2: return VarI1FromI2(V_I2(ps), &V_I1(pd));
157 case VT_I4: return VarI1FromI4(V_I4(ps), &V_I1(pd));
158 case VT_UI1: return VarI1FromUI1(V_UI1(ps), &V_I1(pd));
159 case VT_UI2: return VarI1FromUI2(V_UI2(ps), &V_I1(pd));
160 case VT_UI4: return VarI1FromUI4(V_UI4(ps), &V_I1(pd));
161 case VT_I8: return VarI1FromI8(V_I8(ps), &V_I1(pd));
162 case VT_UI8: return VarI1FromUI8(V_UI8(ps), &V_I1(pd));
163 case VT_R4: return VarI1FromR4(V_R4(ps), &V_I1(pd));
164 case VT_R8: return VarI1FromR8(V_R8(ps), &V_I1(pd));
165 case VT_DATE: return VarI1FromDate(V_DATE(ps), &V_I1(pd));
166 case VT_BOOL: return VarI1FromBool(V_BOOL(ps), &V_I1(pd));
167 case VT_CY: return VarI1FromCy(V_CY(ps), &V_I1(pd));
168 case VT_DECIMAL: return VarI1FromDec(&V_DECIMAL(ps), &V_I1(pd) );
169 case VT_DISPATCH: return VarI1FromDisp(V_DISPATCH(ps), lcid, &V_I1(pd) );
170 case VT_BSTR: return VarI1FromStr(V_BSTR(ps), lcid, dwFlags, &V_I1(pd) );
172 break;
174 case VT_I2:
175 switch (vtFrom)
177 case VT_EMPTY: V_I2(pd) = 0; return S_OK;
178 case VT_I1: return VarI2FromI1(V_I1(ps), &V_I2(pd));
179 case VT_I4: return VarI2FromI4(V_I4(ps), &V_I2(pd));
180 case VT_UI1: return VarI2FromUI1(V_UI1(ps), &V_I2(pd));
181 case VT_UI2: return VarI2FromUI2(V_UI2(ps), &V_I2(pd));
182 case VT_UI4: return VarI2FromUI4(V_UI4(ps), &V_I2(pd));
183 case VT_I8: return VarI2FromI8(V_I8(ps), &V_I2(pd));
184 case VT_UI8: return VarI2FromUI8(V_UI8(ps), &V_I2(pd));
185 case VT_R4: return VarI2FromR4(V_R4(ps), &V_I2(pd));
186 case VT_R8: return VarI2FromR8(V_R8(ps), &V_I2(pd));
187 case VT_DATE: return VarI2FromDate(V_DATE(ps), &V_I2(pd));
188 case VT_BOOL: return VarI2FromBool(V_BOOL(ps), &V_I2(pd));
189 case VT_CY: return VarI2FromCy(V_CY(ps), &V_I2(pd));
190 case VT_DECIMAL: return VarI2FromDec(&V_DECIMAL(ps), &V_I2(pd));
191 case VT_DISPATCH: return VarI2FromDisp(V_DISPATCH(ps), lcid, &V_I2(pd));
192 case VT_BSTR: return VarI2FromStr(V_BSTR(ps), lcid, dwFlags, &V_I2(pd));
194 break;
196 case VT_I4:
197 switch (vtFrom)
199 case VT_EMPTY: V_I4(pd) = 0; return S_OK;
200 case VT_I1: return VarI4FromI1(V_I1(ps), &V_I4(pd));
201 case VT_I2: return VarI4FromI2(V_I2(ps), &V_I4(pd));
202 case VT_UI1: return VarI4FromUI1(V_UI1(ps), &V_I4(pd));
203 case VT_UI2: return VarI4FromUI2(V_UI2(ps), &V_I4(pd));
204 case VT_UI4:
205 if (bIgnoreOverflow)
207 V_VT(pd) = VT_I4;
208 V_I4(pd) = V_I4(ps);
209 return S_OK;
211 return VarI4FromUI4(V_UI4(ps), &V_I4(pd));
212 case VT_I8: return VarI4FromI8(V_I8(ps), &V_I4(pd));
213 case VT_UI8: return VarI4FromUI8(V_UI8(ps), &V_I4(pd));
214 case VT_R4: return VarI4FromR4(V_R4(ps), &V_I4(pd));
215 case VT_R8: return VarI4FromR8(V_R8(ps), &V_I4(pd));
216 case VT_DATE: return VarI4FromDate(V_DATE(ps), &V_I4(pd));
217 case VT_BOOL: return VarI4FromBool(V_BOOL(ps), &V_I4(pd));
218 case VT_CY: return VarI4FromCy(V_CY(ps), &V_I4(pd));
219 case VT_DECIMAL: return VarI4FromDec(&V_DECIMAL(ps), &V_I4(pd));
220 case VT_DISPATCH: return VarI4FromDisp(V_DISPATCH(ps), lcid, &V_I4(pd));
221 case VT_BSTR: return VarI4FromStr(V_BSTR(ps), lcid, dwFlags, &V_I4(pd));
223 break;
225 case VT_UI1:
226 switch (vtFrom)
228 case VT_EMPTY: V_UI1(pd) = 0; return S_OK;
229 case VT_I1: return VarUI1FromI1(V_I1(ps), &V_UI1(pd));
230 case VT_I2: return VarUI1FromI2(V_I2(ps), &V_UI1(pd));
231 case VT_I4: return VarUI1FromI4(V_I4(ps), &V_UI1(pd));
232 case VT_UI2: return VarUI1FromUI2(V_UI2(ps), &V_UI1(pd));
233 case VT_UI4: return VarUI1FromUI4(V_UI4(ps), &V_UI1(pd));
234 case VT_I8: return VarUI1FromI8(V_I8(ps), &V_UI1(pd));
235 case VT_UI8: return VarUI1FromUI8(V_UI8(ps), &V_UI1(pd));
236 case VT_R4: return VarUI1FromR4(V_R4(ps), &V_UI1(pd));
237 case VT_R8: return VarUI1FromR8(V_R8(ps), &V_UI1(pd));
238 case VT_DATE: return VarUI1FromDate(V_DATE(ps), &V_UI1(pd));
239 case VT_BOOL: return VarUI1FromBool(V_BOOL(ps), &V_UI1(pd));
240 case VT_CY: return VarUI1FromCy(V_CY(ps), &V_UI1(pd));
241 case VT_DECIMAL: return VarUI1FromDec(&V_DECIMAL(ps), &V_UI1(pd));
242 case VT_DISPATCH: return VarUI1FromDisp(V_DISPATCH(ps), lcid, &V_UI1(pd));
243 case VT_BSTR: return VarUI1FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI1(pd));
245 break;
247 case VT_UI2:
248 switch (vtFrom)
250 case VT_EMPTY: V_UI2(pd) = 0; return S_OK;
251 case VT_I1: return VarUI2FromI1(V_I1(ps), &V_UI2(pd));
252 case VT_I2: return VarUI2FromI2(V_I2(ps), &V_UI2(pd));
253 case VT_I4: return VarUI2FromI4(V_I4(ps), &V_UI2(pd));
254 case VT_UI1: return VarUI2FromUI1(V_UI1(ps), &V_UI2(pd));
255 case VT_UI4: return VarUI2FromUI4(V_UI4(ps), &V_UI2(pd));
256 case VT_I8: return VarUI4FromI8(V_I8(ps), &V_UI4(pd));
257 case VT_UI8: return VarUI4FromUI8(V_UI8(ps), &V_UI4(pd));
258 case VT_R4: return VarUI2FromR4(V_R4(ps), &V_UI2(pd));
259 case VT_R8: return VarUI2FromR8(V_R8(ps), &V_UI2(pd));
260 case VT_DATE: return VarUI2FromDate(V_DATE(ps), &V_UI2(pd));
261 case VT_BOOL: return VarUI2FromBool(V_BOOL(ps), &V_UI2(pd));
262 case VT_CY: return VarUI2FromCy(V_CY(ps), &V_UI2(pd));
263 case VT_DECIMAL: return VarUI2FromDec(&V_DECIMAL(ps), &V_UI2(pd));
264 case VT_DISPATCH: return VarUI2FromDisp(V_DISPATCH(ps), lcid, &V_UI2(pd));
265 case VT_BSTR: return VarUI2FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI2(pd));
267 break;
269 case VT_UI4:
270 switch (vtFrom)
272 case VT_EMPTY: V_UI4(pd) = 0; return S_OK;
273 case VT_I1: return VarUI4FromI1(V_I1(ps), &V_UI4(pd));
274 case VT_I2: return VarUI4FromI2(V_I2(ps), &V_UI4(pd));
275 case VT_I4: return VarUI4FromI4(V_I4(ps), &V_UI4(pd));
276 case VT_UI1: return VarUI4FromUI1(V_UI1(ps), &V_UI4(pd));
277 case VT_UI2: return VarUI4FromUI2(V_UI2(ps), &V_UI4(pd));
278 case VT_I8: return VarUI4FromI8(V_I8(ps), &V_UI4(pd));
279 case VT_UI8: return VarUI4FromUI8(V_UI8(ps), &V_UI4(pd));
280 case VT_R4: return VarUI4FromR4(V_R4(ps), &V_UI4(pd));
281 case VT_R8: return VarUI4FromR8(V_R8(ps), &V_UI4(pd));
282 case VT_DATE: return VarUI4FromDate(V_DATE(ps), &V_UI4(pd));
283 case VT_BOOL: return VarUI4FromBool(V_BOOL(ps), &V_UI4(pd));
284 case VT_CY: return VarUI4FromCy(V_CY(ps), &V_UI4(pd));
285 case VT_DECIMAL: return VarUI4FromDec(&V_DECIMAL(ps), &V_UI4(pd));
286 case VT_DISPATCH: return VarUI4FromDisp(V_DISPATCH(ps), lcid, &V_UI4(pd));
287 case VT_BSTR: return VarUI4FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI4(pd));
289 break;
291 case VT_UI8:
292 switch (vtFrom)
294 case VT_EMPTY: V_UI8(pd) = 0; return S_OK;
295 case VT_I4: if (V_I4(ps) < 0) return DISP_E_OVERFLOW; V_UI8(pd) = V_I4(ps); return S_OK;
296 case VT_I1: return VarUI8FromI1(V_I1(ps), &V_UI8(pd));
297 case VT_I2: return VarUI8FromI2(V_I2(ps), &V_UI8(pd));
298 case VT_UI1: return VarUI8FromUI1(V_UI1(ps), &V_UI8(pd));
299 case VT_UI2: return VarUI8FromUI2(V_UI2(ps), &V_UI8(pd));
300 case VT_UI4: return VarUI8FromUI4(V_UI4(ps), &V_UI8(pd));
301 case VT_I8: return VarUI8FromI8(V_I8(ps), &V_UI8(pd));
302 case VT_R4: return VarUI8FromR4(V_R4(ps), &V_UI8(pd));
303 case VT_R8: return VarUI8FromR8(V_R8(ps), &V_UI8(pd));
304 case VT_DATE: return VarUI8FromDate(V_DATE(ps), &V_UI8(pd));
305 case VT_BOOL: return VarUI8FromBool(V_BOOL(ps), &V_UI8(pd));
306 case VT_CY: return VarUI8FromCy(V_CY(ps), &V_UI8(pd));
307 case VT_DECIMAL: return VarUI8FromDec(&V_DECIMAL(ps), &V_UI8(pd));
308 case VT_DISPATCH: return VarUI8FromDisp(V_DISPATCH(ps), lcid, &V_UI8(pd));
309 case VT_BSTR: return VarUI8FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI8(pd));
311 break;
313 case VT_I8:
314 switch (vtFrom)
316 case VT_EMPTY: V_I8(pd) = 0; return S_OK;
317 case VT_I4: V_I8(pd) = V_I4(ps); return S_OK;
318 case VT_I1: return VarI8FromI1(V_I1(ps), &V_I8(pd));
319 case VT_I2: return VarI8FromI2(V_I2(ps), &V_I8(pd));
320 case VT_UI1: return VarI8FromUI1(V_UI1(ps), &V_I8(pd));
321 case VT_UI2: return VarI8FromUI2(V_UI2(ps), &V_I8(pd));
322 case VT_UI4: return VarI8FromUI4(V_UI4(ps), &V_I8(pd));
323 case VT_UI8: return VarI8FromUI8(V_I8(ps), &V_I8(pd));
324 case VT_R4: return VarI8FromR4(V_R4(ps), &V_I8(pd));
325 case VT_R8: return VarI8FromR8(V_R8(ps), &V_I8(pd));
326 case VT_DATE: return VarI8FromDate(V_DATE(ps), &V_I8(pd));
327 case VT_BOOL: return VarI8FromBool(V_BOOL(ps), &V_I8(pd));
328 case VT_CY: return VarI8FromCy(V_CY(ps), &V_I8(pd));
329 case VT_DECIMAL: return VarI8FromDec(&V_DECIMAL(ps), &V_I8(pd));
330 case VT_DISPATCH: return VarI8FromDisp(V_DISPATCH(ps), lcid, &V_I8(pd));
331 case VT_BSTR: return VarI8FromStr(V_BSTR(ps), lcid, dwFlags, &V_I8(pd));
333 break;
335 case VT_R4:
336 switch (vtFrom)
338 case VT_EMPTY: V_R4(pd) = 0.0f; return S_OK;
339 case VT_I1: return VarR4FromI1(V_I1(ps), &V_R4(pd));
340 case VT_I2: return VarR4FromI2(V_I2(ps), &V_R4(pd));
341 case VT_I4: return VarR4FromI4(V_I4(ps), &V_R4(pd));
342 case VT_UI1: return VarR4FromUI1(V_UI1(ps), &V_R4(pd));
343 case VT_UI2: return VarR4FromUI2(V_UI2(ps), &V_R4(pd));
344 case VT_UI4: return VarR4FromUI4(V_UI4(ps), &V_R4(pd));
345 case VT_I8: return VarR4FromI8(V_I8(ps), &V_R4(pd));
346 case VT_UI8: return VarR4FromUI8(V_UI8(ps), &V_R4(pd));
347 case VT_R8: return VarR4FromR8(V_R8(ps), &V_R4(pd));
348 case VT_DATE: return VarR4FromDate(V_DATE(ps), &V_R4(pd));
349 case VT_BOOL: return VarR4FromBool(V_BOOL(ps), &V_R4(pd));
350 case VT_CY: return VarR4FromCy(V_CY(ps), &V_R4(pd));
351 case VT_DECIMAL: return VarR4FromDec(&V_DECIMAL(ps), &V_R4(pd));
352 case VT_DISPATCH: return VarR4FromDisp(V_DISPATCH(ps), lcid, &V_R4(pd));
353 case VT_BSTR: return VarR4FromStr(V_BSTR(ps), lcid, dwFlags, &V_R4(pd));
355 break;
357 case VT_R8:
358 switch (vtFrom)
360 case VT_EMPTY: V_R8(pd) = 0.0; return S_OK;
361 case VT_I1: return VarR8FromI1(V_I1(ps), &V_R8(pd));
362 case VT_I2: return VarR8FromI2(V_I2(ps), &V_R8(pd));
363 case VT_I4: return VarR8FromI4(V_I4(ps), &V_R8(pd));
364 case VT_UI1: return VarR8FromUI1(V_UI1(ps), &V_R8(pd));
365 case VT_UI2: return VarR8FromUI2(V_UI2(ps), &V_R8(pd));
366 case VT_UI4: return VarR8FromUI4(V_UI4(ps), &V_R8(pd));
367 case VT_I8: return VarR8FromI8(V_I8(ps), &V_R8(pd));
368 case VT_UI8: return VarR8FromUI8(V_UI8(ps), &V_R8(pd));
369 case VT_R4: return VarR8FromR4(V_R4(ps), &V_R8(pd));
370 case VT_DATE: return VarR8FromDate(V_DATE(ps), &V_R8(pd));
371 case VT_BOOL: return VarR8FromBool(V_BOOL(ps), &V_R8(pd));
372 case VT_CY: return VarR8FromCy(V_CY(ps), &V_R8(pd));
373 case VT_DECIMAL: return VarR8FromDec(&V_DECIMAL(ps), &V_R8(pd));
374 case VT_DISPATCH: return VarR8FromDisp(V_DISPATCH(ps), lcid, &V_R8(pd));
375 case VT_BSTR: return VarR8FromStr(V_BSTR(ps), lcid, dwFlags, &V_R8(pd));
377 break;
379 case VT_DATE:
380 switch (vtFrom)
382 case VT_EMPTY: V_DATE(pd) = 0.0; return S_OK;
383 case VT_I1: return VarDateFromI1(V_I1(ps), &V_DATE(pd));
384 case VT_I2: return VarDateFromI2(V_I2(ps), &V_DATE(pd));
385 case VT_I4: return VarDateFromI4(V_I4(ps), &V_DATE(pd));
386 case VT_UI1: return VarDateFromUI1(V_UI1(ps), &V_DATE(pd));
387 case VT_UI2: return VarDateFromUI2(V_UI2(ps), &V_DATE(pd));
388 case VT_UI4: return VarDateFromUI4(V_UI4(ps), &V_DATE(pd));
389 case VT_I8: return VarDateFromI8(V_I8(ps), &V_DATE(pd));
390 case VT_UI8: return VarDateFromUI8(V_UI8(ps), &V_DATE(pd));
391 case VT_R4: return VarDateFromR4(V_R4(ps), &V_DATE(pd));
392 case VT_R8: return VarDateFromR8(V_R8(ps), &V_DATE(pd));
393 case VT_BOOL: return VarDateFromBool(V_BOOL(ps), &V_DATE(pd));
394 case VT_CY: return VarDateFromCy(V_CY(ps), &V_DATE(pd));
395 case VT_DECIMAL: return VarDateFromDec(&V_DECIMAL(ps), &V_DATE(pd));
396 case VT_DISPATCH: return VarDateFromDisp(V_DISPATCH(ps), lcid, &V_DATE(pd));
397 case VT_BSTR: return VarDateFromStr(V_BSTR(ps), lcid, dwFlags, &V_DATE(pd));
399 break;
401 case VT_BOOL:
402 switch (vtFrom)
404 case VT_EMPTY: V_BOOL(pd) = 0; return S_OK;
405 case VT_I1: return VarBoolFromI1(V_I1(ps), &V_BOOL(pd));
406 case VT_I2: return VarBoolFromI2(V_I2(ps), &V_BOOL(pd));
407 case VT_I4: return VarBoolFromI4(V_I4(ps), &V_BOOL(pd));
408 case VT_UI1: return VarBoolFromUI1(V_UI1(ps), &V_BOOL(pd));
409 case VT_UI2: return VarBoolFromUI2(V_UI2(ps), &V_BOOL(pd));
410 case VT_UI4: return VarBoolFromUI4(V_UI4(ps), &V_BOOL(pd));
411 case VT_I8: return VarBoolFromI8(V_I8(ps), &V_BOOL(pd));
412 case VT_UI8: return VarBoolFromUI8(V_UI8(ps), &V_BOOL(pd));
413 case VT_R4: return VarBoolFromR4(V_R4(ps), &V_BOOL(pd));
414 case VT_R8: return VarBoolFromR8(V_R8(ps), &V_BOOL(pd));
415 case VT_DATE: return VarBoolFromDate(V_DATE(ps), &V_BOOL(pd));
416 case VT_CY: return VarBoolFromCy(V_CY(ps), &V_BOOL(pd));
417 case VT_DECIMAL: return VarBoolFromDec(&V_DECIMAL(ps), &V_BOOL(pd));
418 case VT_DISPATCH: return VarBoolFromDisp(V_DISPATCH(ps), lcid, &V_BOOL(pd));
419 case VT_BSTR: return VarBoolFromStr(V_BSTR(ps), lcid, dwFlags, &V_BOOL(pd));
421 break;
423 case VT_BSTR:
424 switch (vtFrom)
426 case VT_EMPTY:
427 V_BSTR(pd) = SysAllocStringLen(NULL, 0);
428 return V_BSTR(pd) ? S_OK : E_OUTOFMEMORY;
429 case VT_BOOL:
430 if (wFlags & (VARIANT_ALPHABOOL|VARIANT_LOCALBOOL))
431 return VarBstrFromBool(V_BOOL(ps), lcid, dwFlags, &V_BSTR(pd));
432 return VarBstrFromI2(V_BOOL(ps), lcid, dwFlags, &V_BSTR(pd));
433 case VT_I1: return VarBstrFromI1(V_I1(ps), lcid, dwFlags, &V_BSTR(pd));
434 case VT_I2: return VarBstrFromI2(V_I2(ps), lcid, dwFlags, &V_BSTR(pd));
435 case VT_I4: return VarBstrFromI4(V_I4(ps), lcid, dwFlags, &V_BSTR(pd));
436 case VT_UI1: return VarBstrFromUI1(V_UI1(ps), lcid, dwFlags, &V_BSTR(pd));
437 case VT_UI2: return VarBstrFromUI2(V_UI2(ps), lcid, dwFlags, &V_BSTR(pd));
438 case VT_UI4: return VarBstrFromUI4(V_UI4(ps), lcid, dwFlags, &V_BSTR(pd));
439 case VT_I8: return VarBstrFromI8(V_I8(ps), lcid, dwFlags, &V_BSTR(pd));
440 case VT_UI8: return VarBstrFromUI8(V_UI8(ps), lcid, dwFlags, &V_BSTR(pd));
441 case VT_R4: return VarBstrFromR4(V_R4(ps), lcid, dwFlags, &V_BSTR(pd));
442 case VT_R8: return VarBstrFromR8(V_R8(ps), lcid, dwFlags, &V_BSTR(pd));
443 case VT_DATE: return VarBstrFromDate(V_DATE(ps), lcid, dwFlags, &V_BSTR(pd));
444 case VT_CY: return VarBstrFromCy(V_CY(ps), lcid, dwFlags, &V_BSTR(pd));
445 case VT_DECIMAL: return VarBstrFromDec(&V_DECIMAL(ps), lcid, dwFlags, &V_BSTR(pd));
446 /* case VT_DISPATCH: return VarBstrFromDisp(V_DISPATCH(ps), lcid, dwFlags, &V_BSTR(pd)); */
448 break;
450 case VT_CY:
451 switch (vtFrom)
453 case VT_EMPTY: V_CY(pd).int64 = 0; return S_OK;
454 case VT_I1: return VarCyFromI1(V_I1(ps), &V_CY(pd));
455 case VT_I2: return VarCyFromI2(V_I2(ps), &V_CY(pd));
456 case VT_I4: return VarCyFromI4(V_I4(ps), &V_CY(pd));
457 case VT_UI1: return VarCyFromUI1(V_UI1(ps), &V_CY(pd));
458 case VT_UI2: return VarCyFromUI2(V_UI2(ps), &V_CY(pd));
459 case VT_UI4: return VarCyFromUI4(V_UI4(ps), &V_CY(pd));
460 case VT_I8: return VarCyFromI8(V_I8(ps), &V_CY(pd));
461 case VT_UI8: return VarCyFromUI8(V_UI8(ps), &V_CY(pd));
462 case VT_R4: return VarCyFromR4(V_R4(ps), &V_CY(pd));
463 case VT_R8: return VarCyFromR8(V_R8(ps), &V_CY(pd));
464 case VT_DATE: return VarCyFromDate(V_DATE(ps), &V_CY(pd));
465 case VT_BOOL: return VarCyFromBool(V_BOOL(ps), &V_CY(pd));
466 case VT_DECIMAL: return VarCyFromDec(&V_DECIMAL(ps), &V_CY(pd));
467 case VT_DISPATCH: return VarCyFromDisp(V_DISPATCH(ps), lcid, &V_CY(pd));
468 case VT_BSTR: return VarCyFromStr(V_BSTR(ps), lcid, dwFlags, &V_CY(pd));
470 break;
472 case VT_DECIMAL:
473 switch (vtFrom)
475 case VT_EMPTY:
476 case VT_BOOL:
477 DEC_SIGNSCALE(&V_DECIMAL(pd)) = SIGNSCALE(DECIMAL_POS,0);
478 DEC_HI32(&V_DECIMAL(pd)) = 0;
479 DEC_MID32(&V_DECIMAL(pd)) = 0;
480 /* VarDecFromBool() coerces to -1/0, ChangeTypeEx() coerces to 1/0.
481 * VT_NULL and VT_EMPTY always give a 0 value.
483 DEC_LO32(&V_DECIMAL(pd)) = vtFrom == VT_BOOL && V_BOOL(ps) ? 1 : 0;
484 return S_OK;
485 case VT_I1: return VarDecFromI1(V_I1(ps), &V_DECIMAL(pd));
486 case VT_I2: return VarDecFromI2(V_I2(ps), &V_DECIMAL(pd));
487 case VT_I4: return VarDecFromI4(V_I4(ps), &V_DECIMAL(pd));
488 case VT_UI1: return VarDecFromUI1(V_UI1(ps), &V_DECIMAL(pd));
489 case VT_UI2: return VarDecFromUI2(V_UI2(ps), &V_DECIMAL(pd));
490 case VT_UI4: return VarDecFromUI4(V_UI4(ps), &V_DECIMAL(pd));
491 case VT_I8: return VarDecFromI8(V_I8(ps), &V_DECIMAL(pd));
492 case VT_UI8: return VarDecFromUI8(V_UI8(ps), &V_DECIMAL(pd));
493 case VT_R4: return VarDecFromR4(V_R4(ps), &V_DECIMAL(pd));
494 case VT_R8: return VarDecFromR8(V_R8(ps), &V_DECIMAL(pd));
495 case VT_DATE: return VarDecFromDate(V_DATE(ps), &V_DECIMAL(pd));
496 case VT_CY: return VarDecFromCy(V_CY(ps), &V_DECIMAL(pd));
497 case VT_DISPATCH: return VarDecFromDisp(V_DISPATCH(ps), lcid, &V_DECIMAL(pd));
498 case VT_BSTR: return VarDecFromStr(V_BSTR(ps), lcid, dwFlags, &V_DECIMAL(pd));
500 break;
502 case VT_UNKNOWN:
503 switch (vtFrom)
505 case VT_DISPATCH:
506 if (V_DISPATCH(ps) == NULL)
507 V_UNKNOWN(pd) = NULL;
508 else
509 res = IDispatch_QueryInterface(V_DISPATCH(ps), &IID_IUnknown, (LPVOID*)&V_UNKNOWN(pd));
510 break;
512 break;
514 case VT_DISPATCH:
515 switch (vtFrom)
517 case VT_UNKNOWN:
518 if (V_UNKNOWN(ps) == NULL)
519 V_DISPATCH(pd) = NULL;
520 else
521 res = IUnknown_QueryInterface(V_UNKNOWN(ps), &IID_IDispatch, (LPVOID*)&V_DISPATCH(pd));
522 break;
524 break;
526 case VT_RECORD:
527 break;
529 return res;
532 /* Coerce to/from an array */
533 static inline HRESULT VARIANT_CoerceArray(VARIANTARG* pd, VARIANTARG* ps, VARTYPE vt)
535 if (vt == VT_BSTR && V_VT(ps) == (VT_ARRAY|VT_UI1))
536 return BstrFromVector(V_ARRAY(ps), &V_BSTR(pd));
538 if (V_VT(ps) == VT_BSTR && vt == (VT_ARRAY|VT_UI1))
539 return VectorFromBstr(V_BSTR(ps), &V_ARRAY(ps));
541 if (V_VT(ps) == vt)
542 return SafeArrayCopy(V_ARRAY(ps), &V_ARRAY(pd));
544 return DISP_E_TYPEMISMATCH;
547 /******************************************************************************
548 * Check if a variants type is valid.
550 static inline HRESULT VARIANT_ValidateType(VARTYPE vt)
552 VARTYPE vtExtra = vt & VT_EXTRA_TYPE;
554 vt &= VT_TYPEMASK;
556 if (!(vtExtra & (VT_VECTOR|VT_RESERVED)))
558 if (vt < VT_VOID || vt == VT_RECORD || vt == VT_CLSID)
560 if ((vtExtra & (VT_BYREF|VT_ARRAY)) && vt <= VT_NULL)
561 return DISP_E_BADVARTYPE;
562 if (vt != (VARTYPE)15)
563 return S_OK;
566 return DISP_E_BADVARTYPE;
569 /******************************************************************************
570 * VariantInit [OLEAUT32.8]
572 * Initialise a variant.
574 * PARAMS
575 * pVarg [O] Variant to initialise
577 * RETURNS
578 * Nothing.
580 * NOTES
581 * This function simply sets the type of the variant to VT_EMPTY. It does not
582 * free any existing value, use VariantClear() for that.
584 void WINAPI VariantInit(VARIANTARG* pVarg)
586 TRACE("(%p)\n", pVarg);
588 V_VT(pVarg) = VT_EMPTY; /* Native doesn't set any other fields */
591 /******************************************************************************
592 * VariantClear [OLEAUT32.9]
594 * Clear a variant.
596 * PARAMS
597 * pVarg [I/O] Variant to clear
599 * RETURNS
600 * Success: S_OK. Any previous value in pVarg is freed and its type is set to VT_EMPTY.
601 * Failure: DISP_E_BADVARTYPE, if the variant is a not a valid variant type.
603 HRESULT WINAPI VariantClear(VARIANTARG* pVarg)
605 HRESULT hres = S_OK;
607 TRACE("(%p->(%s%s))\n", pVarg, debugstr_VT(pVarg), debugstr_VF(pVarg));
609 hres = VARIANT_ValidateType(V_VT(pVarg));
611 if (SUCCEEDED(hres))
613 if (!V_ISBYREF(pVarg))
615 if (V_ISARRAY(pVarg) || V_VT(pVarg) == VT_SAFEARRAY)
617 if (V_ARRAY(pVarg))
618 hres = SafeArrayDestroy(V_ARRAY(pVarg));
620 else if (V_VT(pVarg) == VT_BSTR)
622 if (V_BSTR(pVarg))
623 SysFreeString(V_BSTR(pVarg));
625 else if (V_VT(pVarg) == VT_RECORD)
627 struct __tagBRECORD* pBr = &V_UNION(pVarg,brecVal);
628 if (pBr->pRecInfo)
630 IRecordInfo_RecordClear(pBr->pRecInfo, pBr->pvRecord);
631 IRecordInfo_Release(pBr->pRecInfo);
634 else if (V_VT(pVarg) == VT_DISPATCH ||
635 V_VT(pVarg) == VT_UNKNOWN)
637 if (V_UNKNOWN(pVarg))
638 IUnknown_Release(V_UNKNOWN(pVarg));
640 else if (V_VT(pVarg) == VT_VARIANT)
642 if (V_VARIANTREF(pVarg))
643 VariantClear(V_VARIANTREF(pVarg));
646 V_VT(pVarg) = VT_EMPTY;
648 return hres;
651 /******************************************************************************
652 * Copy an IRecordInfo object contained in a variant.
654 static HRESULT VARIANT_CopyIRecordInfo(struct __tagBRECORD* pBr)
656 HRESULT hres = S_OK;
658 if (pBr->pRecInfo)
660 ULONG ulSize;
662 hres = IRecordInfo_GetSize(pBr->pRecInfo, &ulSize);
663 if (SUCCEEDED(hres))
665 PVOID pvRecord = HeapAlloc(GetProcessHeap(), 0, ulSize);
666 if (!pvRecord)
667 hres = E_OUTOFMEMORY;
668 else
670 memcpy(pvRecord, pBr->pvRecord, ulSize);
671 pBr->pvRecord = pvRecord;
673 hres = IRecordInfo_RecordCopy(pBr->pRecInfo, pvRecord, pvRecord);
674 if (SUCCEEDED(hres))
675 IRecordInfo_AddRef(pBr->pRecInfo);
679 else if (pBr->pvRecord)
680 hres = E_INVALIDARG;
681 return hres;
684 /******************************************************************************
685 * VariantCopy [OLEAUT32.10]
687 * Copy a variant.
689 * PARAMS
690 * pvargDest [O] Destination for copy
691 * pvargSrc [I] Source variant to copy
693 * RETURNS
694 * Success: S_OK. pvargDest contains a copy of pvargSrc.
695 * Failure: DISP_E_BADVARTYPE, if either variant has an invalid type.
696 * E_OUTOFMEMORY, if memory cannot be allocated. Otherwise an
697 * HRESULT error code from SafeArrayCopy(), IRecordInfo_GetSize(),
698 * or IRecordInfo_RecordCopy(), depending on the type of pvargSrc.
700 * NOTES
701 * - If pvargSrc == pvargDest, this function does nothing, and succeeds if
702 * pvargSrc is valid. Otherwise, pvargDest is always cleared using
703 * VariantClear() before pvargSrc is copied to it. If clearing pvargDest
704 * fails, so does this function.
705 * - VT_CLSID is a valid type type for pvargSrc, but not for pvargDest.
706 * - For by-value non-intrinsic types, a deep copy is made, i.e. The whole value
707 * is copied rather than just any pointers to it.
708 * - For by-value object types the object pointer is copied and the objects
709 * reference count increased using IUnknown_AddRef().
710 * - For all by-reference types, only the referencing pointer is copied.
712 HRESULT WINAPI VariantCopy(VARIANTARG* pvargDest, VARIANTARG* pvargSrc)
714 HRESULT hres = S_OK;
716 TRACE("(%p->(%s%s),%p->(%s%s))\n", pvargDest, debugstr_VT(pvargDest),
717 debugstr_VF(pvargDest), pvargSrc, debugstr_VT(pvargSrc),
718 debugstr_VF(pvargSrc));
720 if (V_TYPE(pvargSrc) == VT_CLSID || /* VT_CLSID is a special case */
721 FAILED(VARIANT_ValidateType(V_VT(pvargSrc))))
722 return DISP_E_BADVARTYPE;
724 if (pvargSrc != pvargDest &&
725 SUCCEEDED(hres = VariantClear(pvargDest)))
727 *pvargDest = *pvargSrc; /* Shallow copy the value */
729 if (!V_ISBYREF(pvargSrc))
731 if (V_ISARRAY(pvargSrc))
733 if (V_ARRAY(pvargSrc))
734 hres = SafeArrayCopy(V_ARRAY(pvargSrc), &V_ARRAY(pvargDest));
736 else if (V_VT(pvargSrc) == VT_BSTR)
738 if (V_BSTR(pvargSrc))
740 V_BSTR(pvargDest) = SysAllocStringByteLen((char*)V_BSTR(pvargSrc), SysStringByteLen(V_BSTR(pvargSrc)));
741 if (!V_BSTR(pvargDest))
743 TRACE("!V_BSTR(pvargDest), SysAllocStringByteLen() failed to allocate %d bytes\n", SysStringByteLen(V_BSTR(pvargSrc)));
744 hres = E_OUTOFMEMORY;
748 else if (V_VT(pvargSrc) == VT_RECORD)
750 hres = VARIANT_CopyIRecordInfo(&V_UNION(pvargDest,brecVal));
752 else if (V_VT(pvargSrc) == VT_DISPATCH ||
753 V_VT(pvargSrc) == VT_UNKNOWN)
755 if (V_UNKNOWN(pvargSrc))
756 IUnknown_AddRef(V_UNKNOWN(pvargSrc));
760 return hres;
763 /* Return the byte size of a variants data */
764 static inline size_t VARIANT_DataSize(const VARIANT* pv)
766 switch (V_TYPE(pv))
768 case VT_I1:
769 case VT_UI1: return sizeof(BYTE);
770 case VT_I2:
771 case VT_UI2: return sizeof(SHORT);
772 case VT_INT:
773 case VT_UINT:
774 case VT_I4:
775 case VT_UI4: return sizeof(LONG);
776 case VT_I8:
777 case VT_UI8: return sizeof(LONGLONG);
778 case VT_R4: return sizeof(float);
779 case VT_R8: return sizeof(double);
780 case VT_DATE: return sizeof(DATE);
781 case VT_BOOL: return sizeof(VARIANT_BOOL);
782 case VT_DISPATCH:
783 case VT_UNKNOWN:
784 case VT_BSTR: return sizeof(void*);
785 case VT_CY: return sizeof(CY);
786 case VT_ERROR: return sizeof(SCODE);
788 TRACE("Shouldn't be called for vt %s%s!\n", debugstr_VT(pv), debugstr_VF(pv));
789 return 0;
792 /******************************************************************************
793 * VariantCopyInd [OLEAUT32.11]
795 * Copy a variant, dereferencing it it is by-reference.
797 * PARAMS
798 * pvargDest [O] Destination for copy
799 * pvargSrc [I] Source variant to copy
801 * RETURNS
802 * Success: S_OK. pvargDest contains a copy of pvargSrc.
803 * Failure: An HRESULT error code indicating the error.
805 * NOTES
806 * Failure: DISP_E_BADVARTYPE, if either variant has an invalid by-value type.
807 * E_INVALIDARG, if pvargSrc is an invalid by-reference type.
808 * E_OUTOFMEMORY, if memory cannot be allocated. Otherwise an
809 * HRESULT error code from SafeArrayCopy(), IRecordInfo_GetSize(),
810 * or IRecordInfo_RecordCopy(), depending on the type of pvargSrc.
812 * NOTES
813 * - If pvargSrc is by-value, this function behaves exactly as VariantCopy().
814 * - If pvargSrc is by-reference, the value copied to pvargDest is the pointed-to
815 * value.
816 * - if pvargSrc == pvargDest, this function dereferences in place. Otherwise,
817 * pvargDest is always cleared using VariantClear() before pvargSrc is copied
818 * to it. If clearing pvargDest fails, so does this function.
820 HRESULT WINAPI VariantCopyInd(VARIANT* pvargDest, VARIANTARG* pvargSrc)
822 VARIANTARG vTmp, *pSrc = pvargSrc;
823 VARTYPE vt;
824 HRESULT hres = S_OK;
826 TRACE("(%p->(%s%s),%p->(%s%s))\n", pvargDest, debugstr_VT(pvargDest),
827 debugstr_VF(pvargDest), pvargSrc, debugstr_VT(pvargSrc),
828 debugstr_VF(pvargSrc));
830 if (!V_ISBYREF(pvargSrc))
831 return VariantCopy(pvargDest, pvargSrc);
833 /* Argument checking is more lax than VariantCopy()... */
834 vt = V_TYPE(pvargSrc);
835 if (V_ISARRAY(pvargSrc) ||
836 (vt > VT_NULL && vt != (VARTYPE)15 && vt < VT_VOID &&
837 !(V_VT(pvargSrc) & (VT_VECTOR|VT_RESERVED))))
839 /* OK */
841 else
842 return E_INVALIDARG; /* ...And the return value for invalid types differs too */
844 if (pvargSrc == pvargDest)
846 /* In place copy. Use a shallow copy of pvargSrc & init pvargDest.
847 * This avoids an expensive VariantCopy() call - e.g. SafeArrayCopy().
849 vTmp = *pvargSrc;
850 pSrc = &vTmp;
851 V_VT(pvargDest) = VT_EMPTY;
853 else
855 /* Copy into another variant. Free the variant in pvargDest */
856 if (FAILED(hres = VariantClear(pvargDest)))
858 TRACE("VariantClear() of destination failed\n");
859 return hres;
863 if (V_ISARRAY(pSrc))
865 /* Native doesn't check that *V_ARRAYREF(pSrc) is valid */
866 hres = SafeArrayCopy(*V_ARRAYREF(pSrc), &V_ARRAY(pvargDest));
868 else if (V_VT(pSrc) == (VT_BSTR|VT_BYREF))
870 /* Native doesn't check that *V_BSTRREF(pSrc) is valid */
871 V_BSTR(pvargDest) = SysAllocStringByteLen((char*)*V_BSTRREF(pSrc), SysStringByteLen(*V_BSTRREF(pSrc)));
873 else if (V_VT(pSrc) == (VT_RECORD|VT_BYREF))
875 V_UNION(pvargDest,brecVal) = V_UNION(pvargSrc,brecVal);
876 hres = VARIANT_CopyIRecordInfo(&V_UNION(pvargDest,brecVal));
878 else if (V_VT(pSrc) == (VT_DISPATCH|VT_BYREF) ||
879 V_VT(pSrc) == (VT_UNKNOWN|VT_BYREF))
881 /* Native doesn't check that *V_UNKNOWNREF(pSrc) is valid */
882 V_UNKNOWN(pvargDest) = *V_UNKNOWNREF(pSrc);
883 if (*V_UNKNOWNREF(pSrc))
884 IUnknown_AddRef(*V_UNKNOWNREF(pSrc));
886 else if (V_VT(pSrc) == (VT_VARIANT|VT_BYREF))
888 /* Native doesn't check that *V_VARIANTREF(pSrc) is valid */
889 if (V_VT(V_VARIANTREF(pSrc)) == (VT_VARIANT|VT_BYREF))
890 hres = E_INVALIDARG; /* Don't dereference more than one level */
891 else
892 hres = VariantCopyInd(pvargDest, V_VARIANTREF(pSrc));
894 /* Use the dereferenced variants type value, not VT_VARIANT */
895 goto VariantCopyInd_Return;
897 else if (V_VT(pSrc) == (VT_DECIMAL|VT_BYREF))
899 memcpy(&DEC_SCALE(&V_DECIMAL(pvargDest)), &DEC_SCALE(V_DECIMALREF(pSrc)),
900 sizeof(DECIMAL) - sizeof(USHORT));
902 else
904 /* Copy the pointed to data into this variant */
905 memcpy(&V_BYREF(pvargDest), V_BYREF(pSrc), VARIANT_DataSize(pSrc));
908 V_VT(pvargDest) = V_VT(pSrc) & ~VT_BYREF;
910 VariantCopyInd_Return:
912 if (pSrc != pvargSrc)
913 VariantClear(pSrc);
915 TRACE("returning 0x%08lx, %p->(%s%s)\n", hres, pvargDest,
916 debugstr_VT(pvargDest), debugstr_VF(pvargDest));
917 return hres;
920 /******************************************************************************
921 * VariantChangeType [OLEAUT32.12]
923 * Change the type of a variant.
925 * PARAMS
926 * pvargDest [O] Destination for the converted variant
927 * pvargSrc [O] Source variant to change the type of
928 * wFlags [I] VARIANT_ flags from "oleauto.h"
929 * vt [I] Variant type to change pvargSrc into
931 * RETURNS
932 * Success: S_OK. pvargDest contains the converted value.
933 * Failure: An HRESULT error code describing the failure.
935 * NOTES
936 * The LCID used for the conversion is LOCALE_USER_DEFAULT.
937 * See VariantChangeTypeEx.
939 HRESULT WINAPI VariantChangeType(VARIANTARG* pvargDest, VARIANTARG* pvargSrc,
940 USHORT wFlags, VARTYPE vt)
942 return VariantChangeTypeEx( pvargDest, pvargSrc, LOCALE_USER_DEFAULT, wFlags, vt );
945 /******************************************************************************
946 * VariantChangeTypeEx [OLEAUT32.147]
948 * Change the type of a variant.
950 * PARAMS
951 * pvargDest [O] Destination for the converted variant
952 * pvargSrc [O] Source variant to change the type of
953 * lcid [I] LCID for the conversion
954 * wFlags [I] VARIANT_ flags from "oleauto.h"
955 * vt [I] Variant type to change pvargSrc into
957 * RETURNS
958 * Success: S_OK. pvargDest contains the converted value.
959 * Failure: An HRESULT error code describing the failure.
961 * NOTES
962 * pvargDest and pvargSrc can point to the same variant to perform an in-place
963 * conversion. If the conversion is successful, pvargSrc will be freed.
965 HRESULT WINAPI VariantChangeTypeEx(VARIANTARG* pvargDest, VARIANTARG* pvargSrc,
966 LCID lcid, USHORT wFlags, VARTYPE vt)
968 HRESULT res = S_OK;
970 TRACE("(%p->(%s%s),%p->(%s%s),0x%08lx,0x%04x,%s%s)\n", pvargDest,
971 debugstr_VT(pvargDest), debugstr_VF(pvargDest), pvargSrc,
972 debugstr_VT(pvargSrc), debugstr_VF(pvargSrc), lcid, wFlags,
973 debugstr_vt(vt), debugstr_vf(vt));
975 if (vt == VT_CLSID)
976 res = DISP_E_BADVARTYPE;
977 else
979 res = VARIANT_ValidateType(V_VT(pvargSrc));
981 if (SUCCEEDED(res))
983 res = VARIANT_ValidateType(vt);
985 if (SUCCEEDED(res))
987 VARIANTARG vTmp, vSrcDeref;
989 if(V_ISBYREF(pvargSrc) && !V_BYREF(pvargSrc))
990 res = DISP_E_TYPEMISMATCH;
991 else
993 V_VT(&vTmp) = VT_EMPTY;
994 V_VT(&vSrcDeref) = VT_EMPTY;
995 VariantClear(&vTmp);
996 VariantClear(&vSrcDeref);
999 if (SUCCEEDED(res))
1001 res = VariantCopyInd(&vSrcDeref, pvargSrc);
1002 if (SUCCEEDED(res))
1004 if (V_ISARRAY(&vSrcDeref) || (vt & VT_ARRAY))
1005 res = VARIANT_CoerceArray(&vTmp, &vSrcDeref, vt);
1006 else
1007 res = VARIANT_Coerce(&vTmp, lcid, wFlags, &vSrcDeref, vt);
1009 if (SUCCEEDED(res)) {
1010 V_VT(&vTmp) = vt;
1011 VariantCopy(pvargDest, &vTmp);
1013 VariantClear(&vTmp);
1014 VariantClear(&vSrcDeref);
1021 TRACE("returning 0x%08lx, %p->(%s%s)\n", res, pvargDest,
1022 debugstr_VT(pvargDest), debugstr_VF(pvargDest));
1023 return res;
1026 /* Date Conversions */
1028 #define IsLeapYear(y) (((y % 4) == 0) && (((y % 100) != 0) || ((y % 400) == 0)))
1030 /* Convert a VT_DATE value to a Julian Date */
1031 static inline int VARIANT_JulianFromDate(int dateIn)
1033 int julianDays = dateIn;
1035 julianDays -= DATE_MIN; /* Convert to + days from 1 Jan 100 AD */
1036 julianDays += 1757585; /* Convert to + days from 23 Nov 4713 BC (Julian) */
1037 return julianDays;
1040 /* Convert a Julian Date to a VT_DATE value */
1041 static inline int VARIANT_DateFromJulian(int dateIn)
1043 int julianDays = dateIn;
1045 julianDays -= 1757585; /* Convert to + days from 1 Jan 100 AD */
1046 julianDays += DATE_MIN; /* Convert to +/- days from 1 Jan 1899 AD */
1047 return julianDays;
1050 /* Convert a Julian date to Day/Month/Year - from PostgreSQL */
1051 static inline void VARIANT_DMYFromJulian(int jd, USHORT *year, USHORT *month, USHORT *day)
1053 int j, i, l, n;
1055 l = jd + 68569;
1056 n = l * 4 / 146097;
1057 l -= (n * 146097 + 3) / 4;
1058 i = (4000 * (l + 1)) / 1461001;
1059 l += 31 - (i * 1461) / 4;
1060 j = (l * 80) / 2447;
1061 *day = l - (j * 2447) / 80;
1062 l = j / 11;
1063 *month = (j + 2) - (12 * l);
1064 *year = 100 * (n - 49) + i + l;
1067 /* Convert Day/Month/Year to a Julian date - from PostgreSQL */
1068 static inline double VARIANT_JulianFromDMY(USHORT year, USHORT month, USHORT day)
1070 int m12 = (month - 14) / 12;
1072 return ((1461 * (year + 4800 + m12)) / 4 + (367 * (month - 2 - 12 * m12)) / 12 -
1073 (3 * ((year + 4900 + m12) / 100)) / 4 + day - 32075);
1076 /* Macros for accessing DOS format date/time fields */
1077 #define DOS_YEAR(x) (1980 + (x >> 9))
1078 #define DOS_MONTH(x) ((x >> 5) & 0xf)
1079 #define DOS_DAY(x) (x & 0x1f)
1080 #define DOS_HOUR(x) (x >> 11)
1081 #define DOS_MINUTE(x) ((x >> 5) & 0x3f)
1082 #define DOS_SECOND(x) ((x & 0x1f) << 1)
1083 /* Create a DOS format date/time */
1084 #define DOS_DATE(d,m,y) (d | (m << 5) | ((y-1980) << 9))
1085 #define DOS_TIME(h,m,s) ((s >> 1) | (m << 5) | (h << 11))
1087 /* Roll a date forwards or backwards to correct it */
1088 static HRESULT VARIANT_RollUdate(UDATE *lpUd)
1090 static const BYTE days[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
1092 TRACE("Raw date: %d/%d/%d %d:%d:%d\n", lpUd->st.wDay, lpUd->st.wMonth,
1093 lpUd->st.wYear, lpUd->st.wHour, lpUd->st.wMinute, lpUd->st.wSecond);
1095 /* Years < 100 are treated as 1900 + year */
1096 if (lpUd->st.wYear < 100)
1097 lpUd->st.wYear += 1900;
1099 if (!lpUd->st.wMonth)
1101 /* Roll back to December of the previous year */
1102 lpUd->st.wMonth = 12;
1103 lpUd->st.wYear--;
1105 else while (lpUd->st.wMonth > 12)
1107 /* Roll forward the correct number of months */
1108 lpUd->st.wYear++;
1109 lpUd->st.wMonth -= 12;
1112 if (lpUd->st.wYear > 9999 || lpUd->st.wHour > 23 ||
1113 lpUd->st.wMinute > 59 || lpUd->st.wSecond > 59)
1114 return E_INVALIDARG; /* Invalid values */
1116 if (!lpUd->st.wDay)
1118 /* Roll back the date one day */
1119 if (lpUd->st.wMonth == 1)
1121 /* Roll back to December 31 of the previous year */
1122 lpUd->st.wDay = 31;
1123 lpUd->st.wMonth = 12;
1124 lpUd->st.wYear--;
1126 else
1128 lpUd->st.wMonth--; /* Previous month */
1129 if (lpUd->st.wMonth == 2 && IsLeapYear(lpUd->st.wYear))
1130 lpUd->st.wDay = 29; /* Februaury has 29 days on leap years */
1131 else
1132 lpUd->st.wDay = days[lpUd->st.wMonth]; /* Last day of the month */
1135 else if (lpUd->st.wDay > 28)
1137 int rollForward = 0;
1139 /* Possibly need to roll the date forward */
1140 if (lpUd->st.wMonth == 2 && IsLeapYear(lpUd->st.wYear))
1141 rollForward = lpUd->st.wDay - 29; /* Februaury has 29 days on leap years */
1142 else
1143 rollForward = lpUd->st.wDay - days[lpUd->st.wMonth];
1145 if (rollForward > 0)
1147 lpUd->st.wDay = rollForward;
1148 lpUd->st.wMonth++;
1149 if (lpUd->st.wMonth > 12)
1151 lpUd->st.wMonth = 1; /* Roll forward into January of the next year */
1152 lpUd->st.wYear++;
1156 TRACE("Rolled date: %d/%d/%d %d:%d:%d\n", lpUd->st.wDay, lpUd->st.wMonth,
1157 lpUd->st.wYear, lpUd->st.wHour, lpUd->st.wMinute, lpUd->st.wSecond);
1158 return S_OK;
1161 /**********************************************************************
1162 * DosDateTimeToVariantTime [OLEAUT32.14]
1164 * Convert a Dos format date and time into variant VT_DATE format.
1166 * PARAMS
1167 * wDosDate [I] Dos format date
1168 * wDosTime [I] Dos format time
1169 * pDateOut [O] Destination for VT_DATE format
1171 * RETURNS
1172 * Success: TRUE. pDateOut contains the converted time.
1173 * Failure: FALSE, if wDosDate or wDosTime are invalid (see notes).
1175 * NOTES
1176 * - Dos format dates can only hold dates from 1-Jan-1980 to 31-Dec-2099.
1177 * - Dos format times are accurate to only 2 second precision.
1178 * - The format of a Dos Date is:
1179 *| Bits Values Meaning
1180 *| ---- ------ -------
1181 *| 0-4 1-31 Day of the week. 0 rolls back one day. A value greater than
1182 *| the days in the month rolls forward the extra days.
1183 *| 5-8 1-12 Month of the year. 0 rolls back to December of the previous
1184 *| year. 13-15 are invalid.
1185 *| 9-15 0-119 Year based from 1980 (Max 2099). 120-127 are invalid.
1186 * - The format of a Dos Time is:
1187 *| Bits Values Meaning
1188 *| ---- ------ -------
1189 *| 0-4 0-29 Seconds/2. 30 and 31 are invalid.
1190 *| 5-10 0-59 Minutes. 60-63 are invalid.
1191 *| 11-15 0-23 Hours (24 hour clock). 24-32 are invalid.
1193 INT WINAPI DosDateTimeToVariantTime(USHORT wDosDate, USHORT wDosTime,
1194 double *pDateOut)
1196 UDATE ud;
1198 TRACE("(0x%x(%d/%d/%d),0x%x(%d:%d:%d),%p)\n",
1199 wDosDate, DOS_YEAR(wDosDate), DOS_MONTH(wDosDate), DOS_DAY(wDosDate),
1200 wDosTime, DOS_HOUR(wDosTime), DOS_MINUTE(wDosTime), DOS_SECOND(wDosTime),
1201 pDateOut);
1203 ud.st.wYear = DOS_YEAR(wDosDate);
1204 ud.st.wMonth = DOS_MONTH(wDosDate);
1205 if (ud.st.wYear > 2099 || ud.st.wMonth > 12)
1206 return FALSE;
1207 ud.st.wDay = DOS_DAY(wDosDate);
1208 ud.st.wHour = DOS_HOUR(wDosTime);
1209 ud.st.wMinute = DOS_MINUTE(wDosTime);
1210 ud.st.wSecond = DOS_SECOND(wDosTime);
1211 ud.st.wDayOfWeek = ud.st.wMilliseconds = 0;
1213 return !VarDateFromUdate(&ud, 0, pDateOut);
1216 /**********************************************************************
1217 * VariantTimeToDosDateTime [OLEAUT32.13]
1219 * Convert a variant format date into a Dos format date and time.
1221 * dateIn [I] VT_DATE time format
1222 * pwDosDate [O] Destination for Dos format date
1223 * pwDosTime [O] Destination for Dos format time
1225 * RETURNS
1226 * Success: TRUE. pwDosDate and pwDosTime contains the converted values.
1227 * Failure: FALSE, if dateIn cannot be represented in Dos format.
1229 * NOTES
1230 * See DosDateTimeToVariantTime() for Dos format details and bugs.
1232 INT WINAPI VariantTimeToDosDateTime(double dateIn, USHORT *pwDosDate, USHORT *pwDosTime)
1234 UDATE ud;
1236 TRACE("(%g,%p,%p)\n", dateIn, pwDosDate, pwDosTime);
1238 if (FAILED(VarUdateFromDate(dateIn, 0, &ud)))
1239 return FALSE;
1241 if (ud.st.wYear < 1980 || ud.st.wYear > 2099)
1242 return FALSE;
1244 *pwDosDate = DOS_DATE(ud.st.wDay, ud.st.wMonth, ud.st.wYear);
1245 *pwDosTime = DOS_TIME(ud.st.wHour, ud.st.wMinute, ud.st.wSecond);
1247 TRACE("Returning 0x%x(%d/%d/%d), 0x%x(%d:%d:%d)\n",
1248 *pwDosDate, DOS_YEAR(*pwDosDate), DOS_MONTH(*pwDosDate), DOS_DAY(*pwDosDate),
1249 *pwDosTime, DOS_HOUR(*pwDosTime), DOS_MINUTE(*pwDosTime), DOS_SECOND(*pwDosTime));
1250 return TRUE;
1253 /***********************************************************************
1254 * SystemTimeToVariantTime [OLEAUT32.184]
1256 * Convert a System format date and time into variant VT_DATE format.
1258 * PARAMS
1259 * lpSt [I] System format date and time
1260 * pDateOut [O] Destination for VT_DATE format date
1262 * RETURNS
1263 * Success: TRUE. *pDateOut contains the converted value.
1264 * Failure: FALSE, if lpSt cannot be represented in VT_DATE format.
1266 INT WINAPI SystemTimeToVariantTime(LPSYSTEMTIME lpSt, double *pDateOut)
1268 UDATE ud;
1270 TRACE("(%p->%d/%d/%d %d:%d:%d,%p)\n", lpSt, lpSt->wDay, lpSt->wMonth,
1271 lpSt->wYear, lpSt->wHour, lpSt->wMinute, lpSt->wSecond, pDateOut);
1273 if (lpSt->wMonth > 12)
1274 return FALSE;
1276 memcpy(&ud.st, lpSt, sizeof(ud.st));
1277 return !VarDateFromUdate(&ud, 0, pDateOut);
1280 /***********************************************************************
1281 * VariantTimeToSystemTime [OLEAUT32.185]
1283 * Convert a variant VT_DATE into a System format date and time.
1285 * PARAMS
1286 * datein [I] Variant VT_DATE format date
1287 * lpSt [O] Destination for System format date and time
1289 * RETURNS
1290 * Success: TRUE. *lpSt contains the converted value.
1291 * Failure: FALSE, if dateIn is too large or small.
1293 INT WINAPI VariantTimeToSystemTime(double dateIn, LPSYSTEMTIME lpSt)
1295 UDATE ud;
1297 TRACE("(%g,%p)\n", dateIn, lpSt);
1299 if (FAILED(VarUdateFromDate(dateIn, 0, &ud)))
1300 return FALSE;
1302 memcpy(lpSt, &ud.st, sizeof(ud.st));
1303 return TRUE;
1306 /***********************************************************************
1307 * VarDateFromUdateEx [OLEAUT32.319]
1309 * Convert an unpacked format date and time to a variant VT_DATE.
1311 * PARAMS
1312 * pUdateIn [I] Unpacked format date and time to convert
1313 * lcid [I] Locale identifier for the conversion
1314 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1315 * pDateOut [O] Destination for variant VT_DATE.
1317 * RETURNS
1318 * Success: S_OK. *pDateOut contains the converted value.
1319 * Failure: E_INVALIDARG, if pUdateIn cannot be represented in VT_DATE format.
1321 HRESULT WINAPI VarDateFromUdateEx(UDATE *pUdateIn, LCID lcid, ULONG dwFlags, DATE *pDateOut)
1323 UDATE ud;
1324 double dateVal;
1326 TRACE("(%p->%d/%d/%d %d:%d:%d:%d %d %d,0x%08lx,0x%08lx,%p)\n", pUdateIn,
1327 pUdateIn->st.wMonth, pUdateIn->st.wDay, pUdateIn->st.wYear,
1328 pUdateIn->st.wHour, pUdateIn->st.wMinute, pUdateIn->st.wSecond,
1329 pUdateIn->st.wMilliseconds, pUdateIn->st.wDayOfWeek,
1330 pUdateIn->wDayOfYear, lcid, dwFlags, pDateOut);
1332 if (lcid != MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT))
1333 FIXME("lcid possibly not handled, treating as en-us\n");
1335 memcpy(&ud, pUdateIn, sizeof(ud));
1337 if (dwFlags & VAR_VALIDDATE)
1338 WARN("Ignoring VAR_VALIDDATE\n");
1340 if (FAILED(VARIANT_RollUdate(&ud)))
1341 return E_INVALIDARG;
1343 /* Date */
1344 dateVal = VARIANT_DateFromJulian(VARIANT_JulianFromDMY(ud.st.wYear, ud.st.wMonth, ud.st.wDay));
1346 /* Time */
1347 dateVal += ud.st.wHour / 24.0;
1348 dateVal += ud.st.wMinute / 1440.0;
1349 dateVal += ud.st.wSecond / 86400.0;
1350 dateVal += ud.st.wMilliseconds / 86400000.0;
1352 TRACE("Returning %g\n", dateVal);
1353 *pDateOut = dateVal;
1354 return S_OK;
1357 /***********************************************************************
1358 * VarDateFromUdate [OLEAUT32.330]
1360 * Convert an unpacked format date and time to a variant VT_DATE.
1362 * PARAMS
1363 * pUdateIn [I] Unpacked format date and time to convert
1364 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1365 * pDateOut [O] Destination for variant VT_DATE.
1367 * RETURNS
1368 * Success: S_OK. *pDateOut contains the converted value.
1369 * Failure: E_INVALIDARG, if pUdateIn cannot be represented in VT_DATE format.
1371 * NOTES
1372 * This function uses the United States English locale for the conversion. Use
1373 * VarDateFromUdateEx() for alternate locales.
1375 HRESULT WINAPI VarDateFromUdate(UDATE *pUdateIn, ULONG dwFlags, DATE *pDateOut)
1377 LCID lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT);
1379 return VarDateFromUdateEx(pUdateIn, lcid, dwFlags, pDateOut);
1382 /***********************************************************************
1383 * VarUdateFromDate [OLEAUT32.331]
1385 * Convert a variant VT_DATE into an unpacked format date and time.
1387 * PARAMS
1388 * datein [I] Variant VT_DATE format date
1389 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1390 * lpUdate [O] Destination for unpacked format date and time
1392 * RETURNS
1393 * Success: S_OK. *lpUdate contains the converted value.
1394 * Failure: E_INVALIDARG, if dateIn is too large or small.
1396 HRESULT WINAPI VarUdateFromDate(DATE dateIn, ULONG dwFlags, UDATE *lpUdate)
1398 /* Cumulative totals of days per month */
1399 static const USHORT cumulativeDays[] =
1401 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
1403 double datePart, timePart;
1404 int julianDays;
1406 TRACE("(%g,0x%08lx,%p)\n", dateIn, dwFlags, lpUdate);
1408 if (dateIn <= (DATE_MIN - 1.0) || dateIn >= (DATE_MAX + 1.0))
1409 return E_INVALIDARG;
1411 datePart = dateIn < 0.0 ? ceil(dateIn) : floor(dateIn);
1412 /* Compensate for int truncation (always downwards) */
1413 timePart = dateIn - datePart + 0.00000000001;
1414 if (timePart >= 1.0)
1415 timePart -= 0.00000000001;
1417 /* Date */
1418 julianDays = VARIANT_JulianFromDate(dateIn);
1419 VARIANT_DMYFromJulian(julianDays, &lpUdate->st.wYear, &lpUdate->st.wMonth,
1420 &lpUdate->st.wDay);
1422 datePart = (datePart + 1.5) / 7.0;
1423 lpUdate->st.wDayOfWeek = (datePart - floor(datePart)) * 7;
1424 if (lpUdate->st.wDayOfWeek == 0)
1425 lpUdate->st.wDayOfWeek = 5;
1426 else if (lpUdate->st.wDayOfWeek == 1)
1427 lpUdate->st.wDayOfWeek = 6;
1428 else
1429 lpUdate->st.wDayOfWeek -= 2;
1431 if (lpUdate->st.wMonth > 2 && IsLeapYear(lpUdate->st.wYear))
1432 lpUdate->wDayOfYear = 1; /* After February, in a leap year */
1433 else
1434 lpUdate->wDayOfYear = 0;
1436 lpUdate->wDayOfYear += cumulativeDays[lpUdate->st.wMonth];
1437 lpUdate->wDayOfYear += lpUdate->st.wDay;
1439 /* Time */
1440 timePart *= 24.0;
1441 lpUdate->st.wHour = timePart;
1442 timePart -= lpUdate->st.wHour;
1443 timePart *= 60.0;
1444 lpUdate->st.wMinute = timePart;
1445 timePart -= lpUdate->st.wMinute;
1446 timePart *= 60.0;
1447 lpUdate->st.wSecond = timePart;
1448 timePart -= lpUdate->st.wSecond;
1449 lpUdate->st.wMilliseconds = 0;
1450 if (timePart > 0.5)
1452 /* Round the milliseconds, adjusting the time/date forward if needed */
1453 if (lpUdate->st.wSecond < 59)
1454 lpUdate->st.wSecond++;
1455 else
1457 lpUdate->st.wSecond = 0;
1458 if (lpUdate->st.wMinute < 59)
1459 lpUdate->st.wMinute++;
1460 else
1462 lpUdate->st.wMinute = 0;
1463 if (lpUdate->st.wHour < 23)
1464 lpUdate->st.wHour++;
1465 else
1467 lpUdate->st.wHour = 0;
1468 /* Roll over a whole day */
1469 if (++lpUdate->st.wDay > 28)
1470 VARIANT_RollUdate(lpUdate);
1475 return S_OK;
1478 #define GET_NUMBER_TEXT(fld,name) \
1479 buff[0] = 0; \
1480 if (!GetLocaleInfoW(lcid, lctype|fld, buff, 2)) \
1481 WARN("buffer too small for " #fld "\n"); \
1482 else \
1483 if (buff[0]) lpChars->name = buff[0]; \
1484 TRACE("lcid 0x%lx, " #name "=%d '%c'\n", lcid, lpChars->name, lpChars->name)
1486 /* Get the valid number characters for an lcid */
1487 void VARIANT_GetLocalisedNumberChars(VARIANT_NUMBER_CHARS *lpChars, LCID lcid, DWORD dwFlags)
1489 static const VARIANT_NUMBER_CHARS defaultChars = { '-','+','.',',','$',0,'.',',' };
1490 LCTYPE lctype = dwFlags & LOCALE_NOUSEROVERRIDE;
1491 WCHAR buff[4];
1493 memcpy(lpChars, &defaultChars, sizeof(defaultChars));
1494 GET_NUMBER_TEXT(LOCALE_SNEGATIVESIGN, cNegativeSymbol);
1495 GET_NUMBER_TEXT(LOCALE_SPOSITIVESIGN, cPositiveSymbol);
1496 GET_NUMBER_TEXT(LOCALE_SDECIMAL, cDecimalPoint);
1497 GET_NUMBER_TEXT(LOCALE_STHOUSAND, cDigitSeperator);
1498 GET_NUMBER_TEXT(LOCALE_SMONDECIMALSEP, cCurrencyDecimalPoint);
1499 GET_NUMBER_TEXT(LOCALE_SMONTHOUSANDSEP, cCurrencyDigitSeperator);
1501 /* Local currency symbols are often 2 characters */
1502 lpChars->cCurrencyLocal2 = '\0';
1503 switch(GetLocaleInfoW(lcid, lctype|LOCALE_SCURRENCY, buff, sizeof(buff)/sizeof(WCHAR)))
1505 case 3: lpChars->cCurrencyLocal2 = buff[1]; /* Fall through */
1506 case 2: lpChars->cCurrencyLocal = buff[0];
1507 break;
1508 default: WARN("buffer too small for LOCALE_SCURRENCY\n");
1510 TRACE("lcid 0x%lx, cCurrencyLocal =%d,%d '%c','%c'\n", lcid, lpChars->cCurrencyLocal,
1511 lpChars->cCurrencyLocal2, lpChars->cCurrencyLocal, lpChars->cCurrencyLocal2);
1514 /* Number Parsing States */
1515 #define B_PROCESSING_EXPONENT 0x1
1516 #define B_NEGATIVE_EXPONENT 0x2
1517 #define B_EXPONENT_START 0x4
1518 #define B_INEXACT_ZEROS 0x8
1519 #define B_LEADING_ZERO 0x10
1520 #define B_PROCESSING_HEX 0x20
1521 #define B_PROCESSING_OCT 0x40
1523 /**********************************************************************
1524 * VarParseNumFromStr [OLEAUT32.46]
1526 * Parse a string containing a number into a NUMPARSE structure.
1528 * PARAMS
1529 * lpszStr [I] String to parse number from
1530 * lcid [I] Locale Id for the conversion
1531 * dwFlags [I] 0, or LOCALE_NOUSEROVERRIDE to use system default number chars
1532 * pNumprs [I/O] Destination for parsed number
1533 * rgbDig [O] Destination for digits read in
1535 * RETURNS
1536 * Success: S_OK. pNumprs and rgbDig contain the parsed representation of
1537 * the number.
1538 * Failure: E_INVALIDARG, if any parameter is invalid.
1539 * DISP_E_TYPEMISMATCH, if the string is not a number or is formatted
1540 * incorrectly.
1541 * DISP_E_OVERFLOW, if rgbDig is too small to hold the number.
1543 * NOTES
1544 * pNumprs must have the following fields set:
1545 * cDig: Set to the size of rgbDig.
1546 * dwInFlags: Set to the allowable syntax of the number using NUMPRS_ flags
1547 * from "oleauto.h".
1549 * FIXME
1550 * - I am unsure if this function should parse non-arabic (e.g. Thai)
1551 * numerals, so this has not been implemented.
1553 HRESULT WINAPI VarParseNumFromStr(OLECHAR *lpszStr, LCID lcid, ULONG dwFlags,
1554 NUMPARSE *pNumprs, BYTE *rgbDig)
1556 VARIANT_NUMBER_CHARS chars;
1557 BYTE rgbTmp[1024];
1558 DWORD dwState = B_EXPONENT_START|B_INEXACT_ZEROS;
1559 int iMaxDigits = sizeof(rgbTmp) / sizeof(BYTE);
1560 int cchUsed = 0;
1562 TRACE("(%s,%ld,0x%08lx,%p,%p)\n", debugstr_w(lpszStr), lcid, dwFlags, pNumprs, rgbDig);
1564 if (!pNumprs || !rgbDig)
1565 return E_INVALIDARG;
1567 if (pNumprs->cDig < iMaxDigits)
1568 iMaxDigits = pNumprs->cDig;
1570 pNumprs->cDig = 0;
1571 pNumprs->dwOutFlags = 0;
1572 pNumprs->cchUsed = 0;
1573 pNumprs->nBaseShift = 0;
1574 pNumprs->nPwr10 = 0;
1576 if (!lpszStr)
1577 return DISP_E_TYPEMISMATCH;
1579 VARIANT_GetLocalisedNumberChars(&chars, lcid, dwFlags);
1581 /* First consume all the leading symbols and space from the string */
1582 while (1)
1584 if (pNumprs->dwInFlags & NUMPRS_LEADING_WHITE && isspaceW(*lpszStr))
1586 pNumprs->dwOutFlags |= NUMPRS_LEADING_WHITE;
1589 cchUsed++;
1590 lpszStr++;
1591 } while (isspaceW(*lpszStr));
1593 else if (pNumprs->dwInFlags & NUMPRS_LEADING_PLUS &&
1594 *lpszStr == chars.cPositiveSymbol &&
1595 !(pNumprs->dwOutFlags & NUMPRS_LEADING_PLUS))
1597 pNumprs->dwOutFlags |= NUMPRS_LEADING_PLUS;
1598 cchUsed++;
1599 lpszStr++;
1601 else if (pNumprs->dwInFlags & NUMPRS_LEADING_MINUS &&
1602 *lpszStr == chars.cNegativeSymbol &&
1603 !(pNumprs->dwOutFlags & NUMPRS_LEADING_MINUS))
1605 pNumprs->dwOutFlags |= (NUMPRS_LEADING_MINUS|NUMPRS_NEG);
1606 cchUsed++;
1607 lpszStr++;
1609 else if (pNumprs->dwInFlags & NUMPRS_CURRENCY &&
1610 !(pNumprs->dwOutFlags & NUMPRS_CURRENCY) &&
1611 *lpszStr == chars.cCurrencyLocal &&
1612 (!chars.cCurrencyLocal2 || lpszStr[1] == chars.cCurrencyLocal2))
1614 pNumprs->dwOutFlags |= NUMPRS_CURRENCY;
1615 cchUsed++;
1616 lpszStr++;
1617 /* Only accept currency characters */
1618 chars.cDecimalPoint = chars.cCurrencyDecimalPoint;
1619 chars.cDigitSeperator = chars.cCurrencyDigitSeperator;
1621 else if (pNumprs->dwInFlags & NUMPRS_PARENS && *lpszStr == '(' &&
1622 !(pNumprs->dwOutFlags & NUMPRS_PARENS))
1624 pNumprs->dwOutFlags |= NUMPRS_PARENS;
1625 cchUsed++;
1626 lpszStr++;
1628 else
1629 break;
1632 if (!(pNumprs->dwOutFlags & NUMPRS_CURRENCY))
1634 /* Only accept non-currency characters */
1635 chars.cCurrencyDecimalPoint = chars.cDecimalPoint;
1636 chars.cCurrencyDigitSeperator = chars.cDigitSeperator;
1639 if ((*lpszStr == '&' && (*(lpszStr+1) == 'H' || *(lpszStr+1) == 'h')) &&
1640 pNumprs->dwInFlags & NUMPRS_HEX_OCT)
1642 dwState |= B_PROCESSING_HEX;
1643 pNumprs->dwOutFlags |= NUMPRS_HEX_OCT;
1644 cchUsed=cchUsed+2;
1645 lpszStr=lpszStr+2;
1647 else if ((*lpszStr == '&' && (*(lpszStr+1) == 'O' || *(lpszStr+1) == 'o')) &&
1648 pNumprs->dwInFlags & NUMPRS_HEX_OCT)
1650 dwState |= B_PROCESSING_OCT;
1651 pNumprs->dwOutFlags |= NUMPRS_HEX_OCT;
1652 cchUsed=cchUsed+2;
1653 lpszStr=lpszStr+2;
1656 /* Strip Leading zeros */
1657 while (*lpszStr == '0')
1659 dwState |= B_LEADING_ZERO;
1660 cchUsed++;
1661 lpszStr++;
1664 while (*lpszStr)
1666 if (isdigitW(*lpszStr))
1668 if (dwState & B_PROCESSING_EXPONENT)
1670 int exponentSize = 0;
1671 if (dwState & B_EXPONENT_START)
1673 if (!isdigitW(*lpszStr))
1674 break; /* No exponent digits - invalid */
1675 while (*lpszStr == '0')
1677 /* Skip leading zero's in the exponent */
1678 cchUsed++;
1679 lpszStr++;
1683 while (isdigitW(*lpszStr))
1685 exponentSize *= 10;
1686 exponentSize += *lpszStr - '0';
1687 cchUsed++;
1688 lpszStr++;
1690 if (dwState & B_NEGATIVE_EXPONENT)
1691 exponentSize = -exponentSize;
1692 /* Add the exponent into the powers of 10 */
1693 pNumprs->nPwr10 += exponentSize;
1694 dwState &= ~(B_PROCESSING_EXPONENT|B_EXPONENT_START);
1695 lpszStr--; /* back up to allow processing of next char */
1697 else
1699 if ((pNumprs->cDig >= iMaxDigits) && !(dwState & B_PROCESSING_HEX)
1700 && !(dwState & B_PROCESSING_OCT))
1702 pNumprs->dwOutFlags |= NUMPRS_INEXACT;
1704 if (*lpszStr != '0')
1705 dwState &= ~B_INEXACT_ZEROS; /* Inexact number with non-trailing zeros */
1707 /* This digit can't be represented, but count it in nPwr10 */
1708 if (pNumprs->dwOutFlags & NUMPRS_DECIMAL)
1709 pNumprs->nPwr10--;
1710 else
1711 pNumprs->nPwr10++;
1713 else
1715 if ((dwState & B_PROCESSING_OCT) && ((*lpszStr == '8') || (*lpszStr == '9'))) {
1716 return DISP_E_TYPEMISMATCH;
1719 if (pNumprs->dwOutFlags & NUMPRS_DECIMAL)
1720 pNumprs->nPwr10--; /* Count decimal points in nPwr10 */
1722 rgbTmp[pNumprs->cDig] = *lpszStr - '0';
1724 pNumprs->cDig++;
1725 cchUsed++;
1728 else if (*lpszStr == chars.cDigitSeperator && pNumprs->dwInFlags & NUMPRS_THOUSANDS)
1730 pNumprs->dwOutFlags |= NUMPRS_THOUSANDS;
1731 cchUsed++;
1733 else if (*lpszStr == chars.cDecimalPoint &&
1734 pNumprs->dwInFlags & NUMPRS_DECIMAL &&
1735 !(pNumprs->dwOutFlags & (NUMPRS_DECIMAL|NUMPRS_EXPONENT)))
1737 pNumprs->dwOutFlags |= NUMPRS_DECIMAL;
1738 cchUsed++;
1740 /* If we have no digits so far, skip leading zeros */
1741 if (!pNumprs->cDig)
1743 while (lpszStr[1] == '0')
1745 dwState |= B_LEADING_ZERO;
1746 cchUsed++;
1747 lpszStr++;
1748 pNumprs->nPwr10--;
1752 else if ((*lpszStr == 'e' || *lpszStr == 'E') &&
1753 pNumprs->dwInFlags & NUMPRS_EXPONENT &&
1754 !(pNumprs->dwOutFlags & NUMPRS_EXPONENT))
1756 dwState |= B_PROCESSING_EXPONENT;
1757 pNumprs->dwOutFlags |= NUMPRS_EXPONENT;
1758 cchUsed++;
1760 else if (dwState & B_PROCESSING_EXPONENT && *lpszStr == chars.cPositiveSymbol)
1762 cchUsed++; /* Ignore positive exponent */
1764 else if (dwState & B_PROCESSING_EXPONENT && *lpszStr == chars.cNegativeSymbol)
1766 dwState |= B_NEGATIVE_EXPONENT;
1767 cchUsed++;
1769 else if (((*lpszStr >= 'a' && *lpszStr <= 'f') ||
1770 (*lpszStr >= 'A' && *lpszStr <= 'F')) &&
1771 dwState & B_PROCESSING_HEX)
1773 if (pNumprs->cDig >= iMaxDigits)
1775 return DISP_E_OVERFLOW;
1777 else
1779 if (*lpszStr >= 'a')
1780 rgbTmp[pNumprs->cDig] = *lpszStr - 'a' + 10;
1781 else
1782 rgbTmp[pNumprs->cDig] = *lpszStr - 'A' + 10;
1784 pNumprs->cDig++;
1785 cchUsed++;
1787 else
1788 break; /* Stop at an unrecognised character */
1790 lpszStr++;
1793 if (!pNumprs->cDig && dwState & B_LEADING_ZERO)
1795 /* Ensure a 0 on its own gets stored */
1796 pNumprs->cDig = 1;
1797 rgbTmp[0] = 0;
1800 if (pNumprs->dwOutFlags & NUMPRS_EXPONENT && dwState & B_PROCESSING_EXPONENT)
1802 pNumprs->cchUsed = cchUsed;
1803 return DISP_E_TYPEMISMATCH; /* Failed to completely parse the exponent */
1806 if (pNumprs->dwOutFlags & NUMPRS_INEXACT)
1808 if (dwState & B_INEXACT_ZEROS)
1809 pNumprs->dwOutFlags &= ~NUMPRS_INEXACT; /* All zeros doesn't set NUMPRS_INEXACT */
1810 } else if(pNumprs->dwInFlags & NUMPRS_HEX_OCT)
1812 /* copy all of the digits into the output digit buffer */
1813 /* this is exactly what windows does although it also returns */
1814 /* cDig of X and writes X+Y where Y>=0 number of digits to rgbDig */
1815 memcpy(rgbDig, rgbTmp, pNumprs->cDig * sizeof(BYTE));
1817 if (dwState & B_PROCESSING_HEX) {
1818 /* hex numbers have always the same format */
1819 pNumprs->nPwr10=0;
1820 pNumprs->nBaseShift=4;
1821 } else {
1822 if (dwState & B_PROCESSING_OCT) {
1823 /* oct numbers have always the same format */
1824 pNumprs->nPwr10=0;
1825 pNumprs->nBaseShift=3;
1826 } else {
1827 while (pNumprs->cDig > 1 && !rgbTmp[pNumprs->cDig - 1])
1829 pNumprs->nPwr10++;
1830 pNumprs->cDig--;
1834 } else
1836 /* Remove trailing zeros from the last (whole number or decimal) part */
1837 while (pNumprs->cDig > 1 && !rgbTmp[pNumprs->cDig - 1])
1839 pNumprs->nPwr10++;
1840 pNumprs->cDig--;
1844 if (pNumprs->cDig <= iMaxDigits)
1845 pNumprs->dwOutFlags &= ~NUMPRS_INEXACT; /* Ignore stripped zeros for NUMPRS_INEXACT */
1846 else
1847 pNumprs->cDig = iMaxDigits; /* Only return iMaxDigits worth of digits */
1849 /* Copy the digits we processed into rgbDig */
1850 memcpy(rgbDig, rgbTmp, pNumprs->cDig * sizeof(BYTE));
1852 /* Consume any trailing symbols and space */
1853 while (1)
1855 if ((pNumprs->dwInFlags & NUMPRS_TRAILING_WHITE) && isspaceW(*lpszStr))
1857 pNumprs->dwOutFlags |= NUMPRS_TRAILING_WHITE;
1860 cchUsed++;
1861 lpszStr++;
1862 } while (isspaceW(*lpszStr));
1864 else if (pNumprs->dwInFlags & NUMPRS_TRAILING_PLUS &&
1865 !(pNumprs->dwOutFlags & NUMPRS_LEADING_PLUS) &&
1866 *lpszStr == chars.cPositiveSymbol)
1868 pNumprs->dwOutFlags |= NUMPRS_TRAILING_PLUS;
1869 cchUsed++;
1870 lpszStr++;
1872 else if (pNumprs->dwInFlags & NUMPRS_TRAILING_MINUS &&
1873 !(pNumprs->dwOutFlags & NUMPRS_LEADING_MINUS) &&
1874 *lpszStr == chars.cNegativeSymbol)
1876 pNumprs->dwOutFlags |= (NUMPRS_TRAILING_MINUS|NUMPRS_NEG);
1877 cchUsed++;
1878 lpszStr++;
1880 else if (pNumprs->dwInFlags & NUMPRS_PARENS && *lpszStr == ')' &&
1881 pNumprs->dwOutFlags & NUMPRS_PARENS)
1883 cchUsed++;
1884 lpszStr++;
1885 pNumprs->dwOutFlags |= NUMPRS_NEG;
1887 else
1888 break;
1891 if (pNumprs->dwOutFlags & NUMPRS_PARENS && !(pNumprs->dwOutFlags & NUMPRS_NEG))
1893 pNumprs->cchUsed = cchUsed;
1894 return DISP_E_TYPEMISMATCH; /* Opening parenthesis not matched */
1897 if (pNumprs->dwInFlags & NUMPRS_USE_ALL && *lpszStr != '\0')
1898 return DISP_E_TYPEMISMATCH; /* Not all chars were consumed */
1900 if (!pNumprs->cDig)
1901 return DISP_E_TYPEMISMATCH; /* No Number found */
1903 pNumprs->cchUsed = cchUsed;
1904 return S_OK;
1907 /* VTBIT flags indicating an integer value */
1908 #define INTEGER_VTBITS (VTBIT_I1|VTBIT_UI1|VTBIT_I2|VTBIT_UI2|VTBIT_I4|VTBIT_UI4|VTBIT_I8|VTBIT_UI8)
1909 /* VTBIT flags indicating a real number value */
1910 #define REAL_VTBITS (VTBIT_R4|VTBIT_R8|VTBIT_CY)
1912 /* Helper macros to check whether bit pattern fits in VARIANT (x is a ULONG64 ) */
1913 #define FITS_AS_I1(x) ((x) >> 8 == 0)
1914 #define FITS_AS_I2(x) ((x) >> 16 == 0)
1915 #define FITS_AS_I4(x) ((x) >> 32 == 0)
1917 /**********************************************************************
1918 * VarNumFromParseNum [OLEAUT32.47]
1920 * Convert a NUMPARSE structure into a numeric Variant type.
1922 * PARAMS
1923 * pNumprs [I] Source for parsed number. cDig must be set to the size of rgbDig
1924 * rgbDig [I] Source for the numbers digits
1925 * dwVtBits [I] VTBIT_ flags from "oleauto.h" indicating the acceptable dest types
1926 * pVarDst [O] Destination for the converted Variant value.
1928 * RETURNS
1929 * Success: S_OK. pVarDst contains the converted value.
1930 * Failure: E_INVALIDARG, if any parameter is invalid.
1931 * DISP_E_OVERFLOW, if the number is too big for the types set in dwVtBits.
1933 * NOTES
1934 * - The smallest favoured type present in dwVtBits that can represent the
1935 * number in pNumprs without losing precision is used.
1936 * - Signed types are preferrred over unsigned types of the same size.
1937 * - Preferred types in order are: integer, float, double, currency then decimal.
1938 * - Rounding (dropping of decimal points) occurs without error. See VarI8FromR8()
1939 * for details of the rounding method.
1940 * - pVarDst is not cleared before the result is stored in it.
1942 HRESULT WINAPI VarNumFromParseNum(NUMPARSE *pNumprs, BYTE *rgbDig,
1943 ULONG dwVtBits, VARIANT *pVarDst)
1945 /* Scale factors and limits for double arithmetic */
1946 static const double dblMultipliers[11] = {
1947 1.0, 10.0, 100.0, 1000.0, 10000.0, 100000.0,
1948 1000000.0, 10000000.0, 100000000.0, 1000000000.0, 10000000000.0
1950 static const double dblMinimums[11] = {
1951 R8_MIN, R8_MIN*10.0, R8_MIN*100.0, R8_MIN*1000.0, R8_MIN*10000.0,
1952 R8_MIN*100000.0, R8_MIN*1000000.0, R8_MIN*10000000.0,
1953 R8_MIN*100000000.0, R8_MIN*1000000000.0, R8_MIN*10000000000.0
1955 static const double dblMaximums[11] = {
1956 R8_MAX, R8_MAX/10.0, R8_MAX/100.0, R8_MAX/1000.0, R8_MAX/10000.0,
1957 R8_MAX/100000.0, R8_MAX/1000000.0, R8_MAX/10000000.0,
1958 R8_MAX/100000000.0, R8_MAX/1000000000.0, R8_MAX/10000000000.0
1961 int wholeNumberDigits, fractionalDigits, divisor10 = 0, multiplier10 = 0;
1963 TRACE("(%p,%p,0x%lx,%p)\n", pNumprs, rgbDig, dwVtBits, pVarDst);
1965 if (pNumprs->nBaseShift)
1967 /* nBaseShift indicates a hex or octal number */
1968 ULONG64 ul64 = 0;
1969 LONG64 l64;
1970 int i;
1972 /* Convert the hex or octal number string into a UI64 */
1973 for (i = 0; i < pNumprs->cDig; i++)
1975 if (ul64 > ((UI8_MAX>>pNumprs->nBaseShift) - rgbDig[i]))
1977 TRACE("Overflow multiplying digits\n");
1978 return DISP_E_OVERFLOW;
1980 ul64 = (ul64<<pNumprs->nBaseShift) + rgbDig[i];
1983 /* also make a negative representation */
1984 l64=-ul64;
1986 /* Try signed and unsigned types in size order */
1987 if (dwVtBits & VTBIT_I1 && FITS_AS_I1(ul64))
1989 V_VT(pVarDst) = VT_I1;
1990 V_I1(pVarDst) = ul64;
1991 return S_OK;
1993 else if (dwVtBits & VTBIT_UI1 && FITS_AS_I1(ul64))
1995 V_VT(pVarDst) = VT_UI1;
1996 V_UI1(pVarDst) = ul64;
1997 return S_OK;
1999 else if (dwVtBits & VTBIT_I2 && FITS_AS_I2(ul64))
2001 V_VT(pVarDst) = VT_I2;
2002 V_I2(pVarDst) = ul64;
2003 return S_OK;
2005 else if (dwVtBits & VTBIT_UI2 && FITS_AS_I2(ul64))
2007 V_VT(pVarDst) = VT_UI2;
2008 V_UI2(pVarDst) = ul64;
2009 return S_OK;
2011 else if (dwVtBits & VTBIT_I4 && FITS_AS_I4(ul64))
2013 V_VT(pVarDst) = VT_I4;
2014 V_I4(pVarDst) = ul64;
2015 return S_OK;
2017 else if (dwVtBits & VTBIT_UI4 && FITS_AS_I4(ul64))
2019 V_VT(pVarDst) = VT_UI4;
2020 V_UI4(pVarDst) = ul64;
2021 return S_OK;
2023 else if (dwVtBits & VTBIT_I8 && ((ul64 <= I8_MAX)||(l64>=I8_MIN)))
2025 V_VT(pVarDst) = VT_I8;
2026 V_I8(pVarDst) = ul64;
2027 return S_OK;
2029 else if (dwVtBits & VTBIT_UI8)
2031 V_VT(pVarDst) = VT_UI8;
2032 V_UI8(pVarDst) = ul64;
2033 return S_OK;
2035 else if ((dwVtBits & REAL_VTBITS) == VTBIT_DECIMAL)
2037 V_VT(pVarDst) = VT_DECIMAL;
2038 DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_POS,0);
2039 DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2040 DEC_LO64(&V_DECIMAL(pVarDst)) = ul64;
2041 return S_OK;
2043 else if (dwVtBits & VTBIT_R4 && ((ul64 <= I4_MAX)||(l64 >= I4_MIN)))
2045 V_VT(pVarDst) = VT_R4;
2046 if (ul64 <= I4_MAX)
2047 V_R4(pVarDst) = ul64;
2048 else
2049 V_R4(pVarDst) = l64;
2050 return S_OK;
2052 else if (dwVtBits & VTBIT_R8 && ((ul64 <= I4_MAX)||(l64 >= I4_MIN)))
2054 V_VT(pVarDst) = VT_R8;
2055 if (ul64 <= I4_MAX)
2056 V_R8(pVarDst) = ul64;
2057 else
2058 V_R8(pVarDst) = l64;
2059 return S_OK;
2062 TRACE("Overflow: possible return types: 0x%lx, value: %s\n", dwVtBits, wine_dbgstr_longlong(ul64));
2063 return DISP_E_OVERFLOW;
2066 /* Count the number of relevant fractional and whole digits stored,
2067 * And compute the divisor/multiplier to scale the number by.
2069 if (pNumprs->nPwr10 < 0)
2071 if (-pNumprs->nPwr10 >= pNumprs->cDig)
2073 /* A real number < +/- 1.0 e.g. 0.1024 or 0.01024 */
2074 wholeNumberDigits = 0;
2075 fractionalDigits = pNumprs->cDig;
2076 divisor10 = -pNumprs->nPwr10;
2078 else
2080 /* An exactly represented real number e.g. 1.024 */
2081 wholeNumberDigits = pNumprs->cDig + pNumprs->nPwr10;
2082 fractionalDigits = pNumprs->cDig - wholeNumberDigits;
2083 divisor10 = pNumprs->cDig - wholeNumberDigits;
2086 else if (pNumprs->nPwr10 == 0)
2088 /* An exactly represented whole number e.g. 1024 */
2089 wholeNumberDigits = pNumprs->cDig;
2090 fractionalDigits = 0;
2092 else /* pNumprs->nPwr10 > 0 */
2094 /* A whole number followed by nPwr10 0's e.g. 102400 */
2095 wholeNumberDigits = pNumprs->cDig;
2096 fractionalDigits = 0;
2097 multiplier10 = pNumprs->nPwr10;
2100 TRACE("cDig %d; nPwr10 %d, whole %d, frac %d ", pNumprs->cDig,
2101 pNumprs->nPwr10, wholeNumberDigits, fractionalDigits);
2102 TRACE("mult %d; div %d\n", multiplier10, divisor10);
2104 if (dwVtBits & (INTEGER_VTBITS|VTBIT_DECIMAL) &&
2105 (!fractionalDigits || !(dwVtBits & (REAL_VTBITS|VTBIT_CY|VTBIT_DECIMAL))))
2107 /* We have one or more integer output choices, and either:
2108 * 1) An integer input value, or
2109 * 2) A real number input value but no floating output choices.
2110 * Alternately, we have a DECIMAL output available and an integer input.
2112 * So, place the integer value into pVarDst, using the smallest type
2113 * possible and preferring signed over unsigned types.
2115 BOOL bOverflow = FALSE, bNegative;
2116 ULONG64 ul64 = 0;
2117 int i;
2119 /* Convert the integer part of the number into a UI8 */
2120 for (i = 0; i < wholeNumberDigits; i++)
2122 if (ul64 > (UI8_MAX / 10 - rgbDig[i]))
2124 TRACE("Overflow multiplying digits\n");
2125 bOverflow = TRUE;
2126 break;
2128 ul64 = ul64 * 10 + rgbDig[i];
2131 /* Account for the scale of the number */
2132 if (!bOverflow && multiplier10)
2134 for (i = 0; i < multiplier10; i++)
2136 if (ul64 > (UI8_MAX / 10))
2138 TRACE("Overflow scaling number\n");
2139 bOverflow = TRUE;
2140 break;
2142 ul64 = ul64 * 10;
2146 /* If we have any fractional digits, round the value.
2147 * Note we don't have to do this if divisor10 is < 1,
2148 * because this means the fractional part must be < 0.5
2150 if (!bOverflow && fractionalDigits && divisor10 > 0)
2152 const BYTE* fracDig = rgbDig + wholeNumberDigits;
2153 BOOL bAdjust = FALSE;
2155 TRACE("first decimal value is %d\n", *fracDig);
2157 if (*fracDig > 5)
2158 bAdjust = TRUE; /* > 0.5 */
2159 else if (*fracDig == 5)
2161 for (i = 1; i < fractionalDigits; i++)
2163 if (fracDig[i])
2165 bAdjust = TRUE; /* > 0.5 */
2166 break;
2169 /* If exactly 0.5, round only odd values */
2170 if (i == fractionalDigits && (ul64 & 1))
2171 bAdjust = TRUE;
2174 if (bAdjust)
2176 if (ul64 == UI8_MAX)
2178 TRACE("Overflow after rounding\n");
2179 bOverflow = TRUE;
2181 ul64++;
2185 /* Zero is not a negative number */
2186 bNegative = pNumprs->dwOutFlags & NUMPRS_NEG && ul64 ? TRUE : FALSE;
2188 TRACE("Integer value is %lld, bNeg %d\n", ul64, bNegative);
2190 /* For negative integers, try the signed types in size order */
2191 if (!bOverflow && bNegative)
2193 if (dwVtBits & (VTBIT_I1|VTBIT_I2|VTBIT_I4|VTBIT_I8))
2195 if (dwVtBits & VTBIT_I1 && ul64 <= -I1_MIN)
2197 V_VT(pVarDst) = VT_I1;
2198 V_I1(pVarDst) = -ul64;
2199 return S_OK;
2201 else if (dwVtBits & VTBIT_I2 && ul64 <= -I2_MIN)
2203 V_VT(pVarDst) = VT_I2;
2204 V_I2(pVarDst) = -ul64;
2205 return S_OK;
2207 else if (dwVtBits & VTBIT_I4 && ul64 <= -((LONGLONG)I4_MIN))
2209 V_VT(pVarDst) = VT_I4;
2210 V_I4(pVarDst) = -ul64;
2211 return S_OK;
2213 else if (dwVtBits & VTBIT_I8 && ul64 <= (ULONGLONG)I8_MAX + 1)
2215 V_VT(pVarDst) = VT_I8;
2216 V_I8(pVarDst) = -ul64;
2217 return S_OK;
2219 else if ((dwVtBits & REAL_VTBITS) == VTBIT_DECIMAL)
2221 /* Decimal is only output choice left - fast path */
2222 V_VT(pVarDst) = VT_DECIMAL;
2223 DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_NEG,0);
2224 DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2225 DEC_LO64(&V_DECIMAL(pVarDst)) = -ul64;
2226 return S_OK;
2230 else if (!bOverflow)
2232 /* For positive integers, try signed then unsigned types in size order */
2233 if (dwVtBits & VTBIT_I1 && ul64 <= I1_MAX)
2235 V_VT(pVarDst) = VT_I1;
2236 V_I1(pVarDst) = ul64;
2237 return S_OK;
2239 else if (dwVtBits & VTBIT_UI1 && ul64 <= UI1_MAX)
2241 V_VT(pVarDst) = VT_UI1;
2242 V_UI1(pVarDst) = ul64;
2243 return S_OK;
2245 else if (dwVtBits & VTBIT_I2 && ul64 <= I2_MAX)
2247 V_VT(pVarDst) = VT_I2;
2248 V_I2(pVarDst) = ul64;
2249 return S_OK;
2251 else if (dwVtBits & VTBIT_UI2 && ul64 <= UI2_MAX)
2253 V_VT(pVarDst) = VT_UI2;
2254 V_UI2(pVarDst) = ul64;
2255 return S_OK;
2257 else if (dwVtBits & VTBIT_I4 && ul64 <= I4_MAX)
2259 V_VT(pVarDst) = VT_I4;
2260 V_I4(pVarDst) = ul64;
2261 return S_OK;
2263 else if (dwVtBits & VTBIT_UI4 && ul64 <= UI4_MAX)
2265 V_VT(pVarDst) = VT_UI4;
2266 V_UI4(pVarDst) = ul64;
2267 return S_OK;
2269 else if (dwVtBits & VTBIT_I8 && ul64 <= I8_MAX)
2271 V_VT(pVarDst) = VT_I8;
2272 V_I8(pVarDst) = ul64;
2273 return S_OK;
2275 else if (dwVtBits & VTBIT_UI8)
2277 V_VT(pVarDst) = VT_UI8;
2278 V_UI8(pVarDst) = ul64;
2279 return S_OK;
2281 else if ((dwVtBits & REAL_VTBITS) == VTBIT_DECIMAL)
2283 /* Decimal is only output choice left - fast path */
2284 V_VT(pVarDst) = VT_DECIMAL;
2285 DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_POS,0);
2286 DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2287 DEC_LO64(&V_DECIMAL(pVarDst)) = ul64;
2288 return S_OK;
2293 if (dwVtBits & REAL_VTBITS)
2295 /* Try to put the number into a float or real */
2296 BOOL bOverflow = FALSE, bNegative = pNumprs->dwOutFlags & NUMPRS_NEG;
2297 double whole = 0.0;
2298 int i;
2300 /* Convert the number into a double */
2301 for (i = 0; i < pNumprs->cDig; i++)
2302 whole = whole * 10.0 + rgbDig[i];
2304 TRACE("Whole double value is %16.16g\n", whole);
2306 /* Account for the scale */
2307 while (multiplier10 > 10)
2309 if (whole > dblMaximums[10])
2311 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY);
2312 bOverflow = TRUE;
2313 break;
2315 whole = whole * dblMultipliers[10];
2316 multiplier10 -= 10;
2318 if (multiplier10)
2320 if (whole > dblMaximums[multiplier10])
2322 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY);
2323 bOverflow = TRUE;
2325 else
2326 whole = whole * dblMultipliers[multiplier10];
2329 TRACE("Scaled double value is %16.16g\n", whole);
2331 while (divisor10 > 10)
2333 if (whole < dblMinimums[10] && whole != 0)
2335 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY); /* Underflow */
2336 bOverflow = TRUE;
2337 break;
2339 whole = whole / dblMultipliers[10];
2340 divisor10 -= 10;
2342 if (divisor10)
2344 if (whole < dblMinimums[divisor10] && whole != 0)
2346 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY); /* Underflow */
2347 bOverflow = TRUE;
2349 else
2350 whole = whole / dblMultipliers[divisor10];
2352 if (!bOverflow)
2353 TRACE("Final double value is %16.16g\n", whole);
2355 if (dwVtBits & VTBIT_R4 &&
2356 ((whole <= R4_MAX && whole >= R4_MIN) || whole == 0.0))
2358 TRACE("Set R4 to final value\n");
2359 V_VT(pVarDst) = VT_R4; /* Fits into a float */
2360 V_R4(pVarDst) = pNumprs->dwOutFlags & NUMPRS_NEG ? -whole : whole;
2361 return S_OK;
2364 if (dwVtBits & VTBIT_R8)
2366 TRACE("Set R8 to final value\n");
2367 V_VT(pVarDst) = VT_R8; /* Fits into a double */
2368 V_R8(pVarDst) = pNumprs->dwOutFlags & NUMPRS_NEG ? -whole : whole;
2369 return S_OK;
2372 if (dwVtBits & VTBIT_CY)
2374 if (SUCCEEDED(VarCyFromR8(bNegative ? -whole : whole, &V_CY(pVarDst))))
2376 V_VT(pVarDst) = VT_CY; /* Fits into a currency */
2377 TRACE("Set CY to final value\n");
2378 return S_OK;
2380 TRACE("Value Overflows CY\n");
2384 if (dwVtBits & VTBIT_DECIMAL)
2386 int i;
2387 ULONG carry;
2388 ULONG64 tmp;
2389 DECIMAL* pDec = &V_DECIMAL(pVarDst);
2391 DECIMAL_SETZERO(*pDec);
2392 DEC_LO32(pDec) = 0;
2394 if (pNumprs->dwOutFlags & NUMPRS_NEG)
2395 DEC_SIGN(pDec) = DECIMAL_NEG;
2396 else
2397 DEC_SIGN(pDec) = DECIMAL_POS;
2399 /* Factor the significant digits */
2400 for (i = 0; i < pNumprs->cDig; i++)
2402 tmp = (ULONG64)DEC_LO32(pDec) * 10 + rgbDig[i];
2403 carry = (ULONG)(tmp >> 32);
2404 DEC_LO32(pDec) = (ULONG)(tmp & UI4_MAX);
2405 tmp = (ULONG64)DEC_MID32(pDec) * 10 + carry;
2406 carry = (ULONG)(tmp >> 32);
2407 DEC_MID32(pDec) = (ULONG)(tmp & UI4_MAX);
2408 tmp = (ULONG64)DEC_HI32(pDec) * 10 + carry;
2409 DEC_HI32(pDec) = (ULONG)(tmp & UI4_MAX);
2411 if (tmp >> 32 & UI4_MAX)
2413 VarNumFromParseNum_DecOverflow:
2414 TRACE("Overflow\n");
2415 DEC_LO32(pDec) = DEC_MID32(pDec) = DEC_HI32(pDec) = UI4_MAX;
2416 return DISP_E_OVERFLOW;
2420 /* Account for the scale of the number */
2421 while (multiplier10 > 0)
2423 tmp = (ULONG64)DEC_LO32(pDec) * 10;
2424 carry = (ULONG)(tmp >> 32);
2425 DEC_LO32(pDec) = (ULONG)(tmp & UI4_MAX);
2426 tmp = (ULONG64)DEC_MID32(pDec) * 10 + carry;
2427 carry = (ULONG)(tmp >> 32);
2428 DEC_MID32(pDec) = (ULONG)(tmp & UI4_MAX);
2429 tmp = (ULONG64)DEC_HI32(pDec) * 10 + carry;
2430 DEC_HI32(pDec) = (ULONG)(tmp & UI4_MAX);
2432 if (tmp >> 32 & UI4_MAX)
2433 goto VarNumFromParseNum_DecOverflow;
2434 multiplier10--;
2436 DEC_SCALE(pDec) = divisor10;
2438 V_VT(pVarDst) = VT_DECIMAL;
2439 return S_OK;
2441 return DISP_E_OVERFLOW; /* No more output choices */
2444 /**********************************************************************
2445 * VarCat [OLEAUT32.318]
2447 HRESULT WINAPI VarCat(LPVARIANT left, LPVARIANT right, LPVARIANT out)
2449 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
2450 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), out);
2452 /* Should we VariantClear out? */
2453 /* Can we handle array, vector, by ref etc. */
2454 if ((V_VT(left)&VT_TYPEMASK) == VT_NULL &&
2455 (V_VT(right)&VT_TYPEMASK) == VT_NULL)
2457 V_VT(out) = VT_NULL;
2458 return S_OK;
2461 if (V_VT(left) == VT_BSTR && V_VT(right) == VT_BSTR)
2463 V_VT(out) = VT_BSTR;
2464 VarBstrCat (V_BSTR(left), V_BSTR(right), &V_BSTR(out));
2465 return S_OK;
2467 if (V_VT(left) == VT_BSTR) {
2468 VARIANT bstrvar;
2469 HRESULT hres;
2471 V_VT(out) = VT_BSTR;
2472 VariantInit(&bstrvar);
2473 hres = VariantChangeTypeEx(&bstrvar,right,0,0,VT_BSTR);
2474 if (hres) {
2475 FIXME("Failed to convert right side from vt %d to VT_BSTR?\n",V_VT(right));
2476 return hres;
2478 VarBstrCat (V_BSTR(left), V_BSTR(&bstrvar), &V_BSTR(out));
2479 return S_OK;
2481 if (V_VT(right) == VT_BSTR) {
2482 VARIANT bstrvar;
2483 HRESULT hres;
2485 V_VT(out) = VT_BSTR;
2486 VariantInit(&bstrvar);
2487 hres = VariantChangeTypeEx(&bstrvar,left,0,0,VT_BSTR);
2488 if (hres) {
2489 FIXME("Failed to convert right side from vt %d to VT_BSTR?\n",V_VT(right));
2490 return hres;
2492 VarBstrCat (V_BSTR(&bstrvar), V_BSTR(right), &V_BSTR(out));
2493 return S_OK;
2495 FIXME ("types %d / %d not supported\n",V_VT(left)&VT_TYPEMASK, V_VT(right)&VT_TYPEMASK);
2496 return S_OK;
2499 /**********************************************************************
2500 * VarCmp [OLEAUT32.176]
2502 * flags can be:
2503 * NORM_IGNORECASE, NORM_IGNORENONSPACE, NORM_IGNORESYMBOLS
2504 * NORM_IGNOREWIDTH, NORM_IGNOREKANATYPE, NORM_IGNOREKASHIDA
2507 HRESULT WINAPI VarCmp(LPVARIANT left, LPVARIANT right, LCID lcid, DWORD flags)
2509 BOOL lOk = TRUE;
2510 BOOL rOk = TRUE;
2511 LONGLONG lVal = -1;
2512 LONGLONG rVal = -1;
2513 VARIANT rv,lv;
2514 DWORD xmask;
2515 HRESULT rc;
2517 TRACE("(%p->(%s%s),%p->(%s%s),0x%08lx,0x%08lx)\n", left, debugstr_VT(left),
2518 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), lcid, flags);
2520 VariantInit(&lv);VariantInit(&rv);
2521 V_VT(right) &= ~0x8000; /* hack since we sometime get this flag. */
2522 V_VT(left) &= ~0x8000; /* hack since we sometime get this flag. */
2524 /* If either are null, then return VARCMP_NULL */
2525 if ((V_VT(left)&VT_TYPEMASK) == VT_NULL ||
2526 (V_VT(right)&VT_TYPEMASK) == VT_NULL)
2527 return VARCMP_NULL;
2529 /* Strings - use VarBstrCmp */
2530 if ((V_VT(left)&VT_TYPEMASK) == VT_BSTR &&
2531 (V_VT(right)&VT_TYPEMASK) == VT_BSTR) {
2532 return VarBstrCmp(V_BSTR(left), V_BSTR(right), lcid, flags);
2535 xmask = (1<<(V_VT(left)&VT_TYPEMASK))|(1<<(V_VT(right)&VT_TYPEMASK));
2536 if (xmask & VTBIT_R8) {
2537 rc = VariantChangeType(&lv,left,0,VT_R8);
2538 if (FAILED(rc)) return rc;
2539 rc = VariantChangeType(&rv,right,0,VT_R8);
2540 if (FAILED(rc)) return rc;
2542 if (V_R8(&lv) == V_R8(&rv)) return VARCMP_EQ;
2543 if (V_R8(&lv) < V_R8(&rv)) return VARCMP_LT;
2544 if (V_R8(&lv) > V_R8(&rv)) return VARCMP_GT;
2545 return E_FAIL; /* can't get here */
2547 if (xmask & VTBIT_R4) {
2548 rc = VariantChangeType(&lv,left,0,VT_R4);
2549 if (FAILED(rc)) return rc;
2550 rc = VariantChangeType(&rv,right,0,VT_R4);
2551 if (FAILED(rc)) return rc;
2553 if (V_R4(&lv) == V_R4(&rv)) return VARCMP_EQ;
2554 if (V_R4(&lv) < V_R4(&rv)) return VARCMP_LT;
2555 if (V_R4(&lv) > V_R4(&rv)) return VARCMP_GT;
2556 return E_FAIL; /* can't get here */
2559 /* Integers - Ideally like to use VarDecCmp, but no Dec support yet
2560 Use LONGLONG to maximize ranges */
2561 lOk = TRUE;
2562 switch (V_VT(left)&VT_TYPEMASK) {
2563 case VT_I1 : lVal = V_I1(left); break;
2564 case VT_I2 : lVal = V_I2(left); break;
2565 case VT_I4 :
2566 case VT_INT : lVal = V_I4(left); break;
2567 case VT_UI1 : lVal = V_UI1(left); break;
2568 case VT_UI2 : lVal = V_UI2(left); break;
2569 case VT_UI4 :
2570 case VT_UINT : lVal = V_UI4(left); break;
2571 case VT_BOOL : lVal = V_BOOL(left); break;
2572 default: lOk = FALSE;
2575 rOk = TRUE;
2576 switch (V_VT(right)&VT_TYPEMASK) {
2577 case VT_I1 : rVal = V_I1(right); break;
2578 case VT_I2 : rVal = V_I2(right); break;
2579 case VT_I4 :
2580 case VT_INT : rVal = V_I4(right); break;
2581 case VT_UI1 : rVal = V_UI1(right); break;
2582 case VT_UI2 : rVal = V_UI2(right); break;
2583 case VT_UI4 :
2584 case VT_UINT : rVal = V_UI4(right); break;
2585 case VT_BOOL : rVal = V_BOOL(right); break;
2586 default: rOk = FALSE;
2589 if (lOk && rOk) {
2590 if (lVal < rVal) {
2591 return VARCMP_LT;
2592 } else if (lVal > rVal) {
2593 return VARCMP_GT;
2594 } else {
2595 return VARCMP_EQ;
2599 /* Strings - use VarBstrCmp */
2600 if ((V_VT(left)&VT_TYPEMASK) == VT_DATE &&
2601 (V_VT(right)&VT_TYPEMASK) == VT_DATE) {
2603 if (floor(V_DATE(left)) == floor(V_DATE(right))) {
2604 /* Due to floating point rounding errors, calculate varDate in whole numbers) */
2605 double wholePart = 0.0;
2606 double leftR;
2607 double rightR;
2609 /* Get the fraction * 24*60*60 to make it into whole seconds */
2610 wholePart = (double) floor( V_DATE(left) );
2611 if (wholePart == 0) wholePart = 1;
2612 leftR = floor(fmod( V_DATE(left), wholePart ) * (24*60*60));
2614 wholePart = (double) floor( V_DATE(right) );
2615 if (wholePart == 0) wholePart = 1;
2616 rightR = floor(fmod( V_DATE(right), wholePart ) * (24*60*60));
2618 if (leftR < rightR) {
2619 return VARCMP_LT;
2620 } else if (leftR > rightR) {
2621 return VARCMP_GT;
2622 } else {
2623 return VARCMP_EQ;
2626 } else if (V_DATE(left) < V_DATE(right)) {
2627 return VARCMP_LT;
2628 } else if (V_DATE(left) > V_DATE(right)) {
2629 return VARCMP_GT;
2632 FIXME("VarCmp partial implementation, doesn't support vt 0x%x / 0x%x\n",V_VT(left), V_VT(right));
2633 return E_FAIL;
2636 /**********************************************************************
2637 * VarAnd [OLEAUT32.142]
2640 HRESULT WINAPI VarAnd(LPVARIANT left, LPVARIANT right, LPVARIANT result)
2642 HRESULT rc = E_FAIL;
2644 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
2645 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
2647 if ((V_VT(left)&VT_TYPEMASK) == VT_BOOL &&
2648 (V_VT(right)&VT_TYPEMASK) == VT_BOOL) {
2650 V_VT(result) = VT_BOOL;
2651 if (V_BOOL(left) && V_BOOL(right)) {
2652 V_BOOL(result) = VARIANT_TRUE;
2653 } else {
2654 V_BOOL(result) = VARIANT_FALSE;
2656 rc = S_OK;
2658 } else {
2659 /* Integers */
2660 BOOL lOk = TRUE;
2661 BOOL rOk = TRUE;
2662 LONGLONG lVal = -1;
2663 LONGLONG rVal = -1;
2664 LONGLONG res = -1;
2665 int resT = 0; /* Testing has shown I2 & I2 == I2, all else
2666 becomes I4, even unsigned ints (incl. UI2) */
2668 lOk = TRUE;
2669 switch (V_VT(left)&VT_TYPEMASK) {
2670 case VT_I1 : lVal = V_I1(left); resT=VT_I4; break;
2671 case VT_I2 : lVal = V_I2(left); resT=VT_I2; break;
2672 case VT_I4 :
2673 case VT_INT : lVal = V_I4(left); resT=VT_I4; break;
2674 case VT_UI1 : lVal = V_UI1(left); resT=VT_I4; break;
2675 case VT_UI2 : lVal = V_UI2(left); resT=VT_I4; break;
2676 case VT_UI4 :
2677 case VT_UINT : lVal = V_UI4(left); resT=VT_I4; break;
2678 case VT_BOOL : rVal = V_BOOL(left); resT=VT_I4; break;
2679 default: lOk = FALSE;
2682 rOk = TRUE;
2683 switch (V_VT(right)&VT_TYPEMASK) {
2684 case VT_I1 : rVal = V_I1(right); resT=VT_I4; break;
2685 case VT_I2 : rVal = V_I2(right); resT=max(VT_I2, resT); break;
2686 case VT_I4 :
2687 case VT_INT : rVal = V_I4(right); resT=VT_I4; break;
2688 case VT_UI1 : rVal = V_UI1(right); resT=VT_I4; break;
2689 case VT_UI2 : rVal = V_UI2(right); resT=VT_I4; break;
2690 case VT_UI4 :
2691 case VT_UINT : rVal = V_UI4(right); resT=VT_I4; break;
2692 case VT_BOOL : rVal = V_BOOL(right); resT=VT_I4; break;
2693 default: rOk = FALSE;
2696 if (lOk && rOk) {
2697 res = (lVal & rVal);
2698 V_VT(result) = resT;
2699 switch (resT) {
2700 case VT_I2 : V_I2(result) = res; break;
2701 case VT_I4 : V_I4(result) = res; break;
2702 default:
2703 FIXME("Unexpected result variant type %x\n", resT);
2704 V_I4(result) = res;
2706 rc = S_OK;
2708 } else {
2709 FIXME("VarAnd stub\n");
2713 TRACE("returning 0x%8lx (%s%s),%ld\n", rc, debugstr_VT(result),
2714 debugstr_VF(result), V_VT(result) == VT_I4 ? V_I4(result) : V_I2(result));
2715 return rc;
2718 /**********************************************************************
2719 * VarAdd [OLEAUT32.141]
2720 * FIXME: From MSDN: If ... Then
2721 * Both expressions are of the string type Concatenated.
2722 * One expression is a string type and the other a character Addition.
2723 * One expression is numeric and the other is a string Addition.
2724 * Both expressions are numeric Addition.
2725 * Either expression is NULL NULL is returned.
2726 * Both expressions are empty Integer subtype is returned.
2729 HRESULT WINAPI VarAdd(LPVARIANT left, LPVARIANT right, LPVARIANT result)
2731 HRESULT rc = E_FAIL;
2733 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
2734 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
2736 if ((V_VT(left)&VT_TYPEMASK) == VT_EMPTY)
2737 return VariantCopy(result,right);
2739 if ((V_VT(right)&VT_TYPEMASK) == VT_EMPTY)
2740 return VariantCopy(result,left);
2742 /* check if we add doubles */
2743 if (((V_VT(left)&VT_TYPEMASK) == VT_R8) || ((V_VT(right)&VT_TYPEMASK) == VT_R8)) {
2744 BOOL lOk = TRUE;
2745 BOOL rOk = TRUE;
2746 double lVal = -1;
2747 double rVal = -1;
2748 double res = -1;
2750 lOk = TRUE;
2751 switch (V_VT(left)&VT_TYPEMASK) {
2752 case VT_I1 : lVal = V_I1(left); break;
2753 case VT_I2 : lVal = V_I2(left); break;
2754 case VT_I4 :
2755 case VT_INT : lVal = V_I4(left); break;
2756 case VT_UI1 : lVal = V_UI1(left); break;
2757 case VT_UI2 : lVal = V_UI2(left); break;
2758 case VT_UI4 :
2759 case VT_UINT : lVal = V_UI4(left); break;
2760 case VT_R4 : lVal = V_R4(left); break;
2761 case VT_R8 : lVal = V_R8(left); break;
2762 case VT_NULL : lVal = 0.0; break;
2763 default: lOk = FALSE;
2766 rOk = TRUE;
2767 switch (V_VT(right)&VT_TYPEMASK) {
2768 case VT_I1 : rVal = V_I1(right); break;
2769 case VT_I2 : rVal = V_I2(right); break;
2770 case VT_I4 :
2771 case VT_INT : rVal = V_I4(right); break;
2772 case VT_UI1 : rVal = V_UI1(right); break;
2773 case VT_UI2 : rVal = V_UI2(right); break;
2774 case VT_UI4 :
2775 case VT_UINT : rVal = V_UI4(right); break;
2776 case VT_R4 : rVal = V_R4(right);break;
2777 case VT_R8 : rVal = V_R8(right);break;
2778 case VT_NULL : rVal = 0.0; break;
2779 default: rOk = FALSE;
2782 if (lOk && rOk) {
2783 res = (lVal + rVal);
2784 V_VT(result) = VT_R8;
2785 V_R8(result) = res;
2786 rc = S_OK;
2787 } else {
2788 FIXME("Unhandled type pair %d / %d in double addition.\n",
2789 (V_VT(left)&VT_TYPEMASK),
2790 (V_VT(right)&VT_TYPEMASK)
2793 return rc;
2796 /* now check if we add floats. VT_R8 can no longer happen here! */
2797 if (((V_VT(left)&VT_TYPEMASK) == VT_R4) || ((V_VT(right)&VT_TYPEMASK) == VT_R4)) {
2798 BOOL lOk = TRUE;
2799 BOOL rOk = TRUE;
2800 float lVal = -1;
2801 float rVal = -1;
2802 float res = -1;
2804 lOk = TRUE;
2805 switch (V_VT(left)&VT_TYPEMASK) {
2806 case VT_I1 : lVal = V_I1(left); break;
2807 case VT_I2 : lVal = V_I2(left); break;
2808 case VT_I4 :
2809 case VT_INT : lVal = V_I4(left); break;
2810 case VT_UI1 : lVal = V_UI1(left); break;
2811 case VT_UI2 : lVal = V_UI2(left); break;
2812 case VT_UI4 :
2813 case VT_UINT : lVal = V_UI4(left); break;
2814 case VT_R4 : lVal = V_R4(left); break;
2815 case VT_NULL : lVal = 0.0; break;
2816 default: lOk = FALSE;
2819 rOk = TRUE;
2820 switch (V_VT(right)&VT_TYPEMASK) {
2821 case VT_I1 : rVal = V_I1(right); break;
2822 case VT_I2 : rVal = V_I2(right); break;
2823 case VT_I4 :
2824 case VT_INT : rVal = V_I4(right); break;
2825 case VT_UI1 : rVal = V_UI1(right); break;
2826 case VT_UI2 : rVal = V_UI2(right); break;
2827 case VT_UI4 :
2828 case VT_UINT : rVal = V_UI4(right); break;
2829 case VT_R4 : rVal = V_R4(right);break;
2830 case VT_NULL : rVal = 0.0; break;
2831 default: rOk = FALSE;
2834 if (lOk && rOk) {
2835 res = (lVal + rVal);
2836 V_VT(result) = VT_R4;
2837 V_R4(result) = res;
2838 rc = S_OK;
2839 } else {
2840 FIXME("Unhandled type pair %d / %d in float addition.\n",
2841 (V_VT(left)&VT_TYPEMASK),
2842 (V_VT(right)&VT_TYPEMASK)
2845 return rc;
2848 /* Handle strings as concat */
2849 if ((V_VT(left)&VT_TYPEMASK) == VT_BSTR &&
2850 (V_VT(right)&VT_TYPEMASK) == VT_BSTR) {
2851 V_VT(result) = VT_BSTR;
2852 return VarBstrCat(V_BSTR(left), V_BSTR(right), &V_BSTR(result));
2853 } else {
2855 /* Integers */
2856 BOOL lOk = TRUE;
2857 BOOL rOk = TRUE;
2858 LONGLONG lVal = -1;
2859 LONGLONG rVal = -1;
2860 LONGLONG res = -1;
2861 int resT = 0; /* Testing has shown I2 + I2 == I2, all else
2862 becomes I4 */
2864 lOk = TRUE;
2865 switch (V_VT(left)&VT_TYPEMASK) {
2866 case VT_I1 : lVal = V_I1(left); resT=VT_I4; break;
2867 case VT_I2 : lVal = V_I2(left); resT=VT_I2; break;
2868 case VT_I4 :
2869 case VT_INT : lVal = V_I4(left); resT=VT_I4; break;
2870 case VT_UI1 : lVal = V_UI1(left); resT=VT_I4; break;
2871 case VT_UI2 : lVal = V_UI2(left); resT=VT_I4; break;
2872 case VT_UI4 :
2873 case VT_UINT : lVal = V_UI4(left); resT=VT_I4; break;
2874 case VT_NULL : lVal = 0; resT = VT_I4; break;
2875 default: lOk = FALSE;
2878 rOk = TRUE;
2879 switch (V_VT(right)&VT_TYPEMASK) {
2880 case VT_I1 : rVal = V_I1(right); resT=VT_I4; break;
2881 case VT_I2 : rVal = V_I2(right); resT=max(VT_I2, resT); break;
2882 case VT_I4 :
2883 case VT_INT : rVal = V_I4(right); resT=VT_I4; break;
2884 case VT_UI1 : rVal = V_UI1(right); resT=VT_I4; break;
2885 case VT_UI2 : rVal = V_UI2(right); resT=VT_I4; break;
2886 case VT_UI4 :
2887 case VT_UINT : rVal = V_UI4(right); resT=VT_I4; break;
2888 case VT_NULL : rVal = 0; resT=VT_I4; break;
2889 default: rOk = FALSE;
2892 if (lOk && rOk) {
2893 res = (lVal + rVal);
2894 V_VT(result) = resT;
2895 switch (resT) {
2896 case VT_I2 : V_I2(result) = res; break;
2897 case VT_I4 : V_I4(result) = res; break;
2898 default:
2899 FIXME("Unexpected result variant type %x\n", resT);
2900 V_I4(result) = res;
2902 rc = S_OK;
2904 } else {
2905 FIXME("unimplemented part (0x%x + 0x%x)\n",V_VT(left), V_VT(right));
2909 TRACE("returning 0x%8lx (%s%s),%ld\n", rc, debugstr_VT(result),
2910 debugstr_VF(result), V_VT(result) == VT_I4 ? V_I4(result) : V_I2(result));
2911 return rc;
2914 /**********************************************************************
2915 * VarMul [OLEAUT32.156]
2917 * Multiply two variants.
2919 * PARAMS
2920 * left [I] First variant
2921 * right [I] Second variant
2922 * result [O] Result variant
2924 * RETURNS
2925 * Success: S_OK.
2926 * Failure: An HRESULT error code indicating the error.
2928 * NOTES
2929 * Native VarMul up to and including WinXP dosn't like as input variants
2930 * I1, UI2, UI4, UI8, INT and UINT. But it can multiply apples with oranges.
2932 * Native VarMul dosn't check for NULL in/out pointers and crashes. We do the
2933 * same here.
2935 * FIXME
2936 * Overflow checking for R8 (double) overflow. Return DISP_E_OVERFLOW in that
2937 * case.
2939 HRESULT WINAPI VarMul(LPVARIANT left, LPVARIANT right, LPVARIANT result)
2941 HRESULT hres;
2942 VARTYPE lvt, rvt, resvt, tvt;
2943 VARIANT lv, rv, tv;
2944 double r8res;
2946 /* Variant priority for coercion. Sorted from lowest to highest.
2947 VT_ERROR shows an invalid input variant type. */
2948 enum coerceprio { vt_UI1 = 0, vt_I2, vt_I4, vt_I8, vt_CY, vt_R4, vt_R8,
2949 vt_DECIMAL, vt_NULL, vt_ERROR };
2950 /* Mapping from priority to variant type. Keep in sync with coerceprio! */
2951 VARTYPE prio2vt[] = { VT_UI1, VT_I2, VT_I4, VT_I8, VT_CY, VT_R4, VT_R8,
2952 VT_DECIMAL, VT_NULL, VT_ERROR };
2954 /* Mapping for coercion from input variant to priority of result variant. */
2955 static VARTYPE coerce[] = {
2956 /* VT_EMPTY, VT_NULL, VT_I2, VT_I4, VT_R4 */
2957 vt_UI1, vt_NULL, vt_I2, vt_I4, vt_R4,
2958 /* VT_R8, VT_CY, VT_DATE, VT_BSTR, VT_DISPATCH */
2959 vt_R8, vt_CY, vt_R8, vt_R8, vt_ERROR,
2960 /* VT_ERROR, VT_BOOL, VT_VARIANT, VT_UNKNOWN, VT_DECIMAL */
2961 vt_ERROR, vt_I2, vt_ERROR, vt_ERROR, vt_DECIMAL,
2962 /* 15, VT_I1, VT_UI1, VT_UI2, VT_UI4 VT_I8 */
2963 vt_ERROR, vt_ERROR, vt_UI1, vt_ERROR, vt_ERROR, vt_I8
2966 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
2967 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right),
2968 result);
2970 VariantInit(&lv);
2971 VariantInit(&rv);
2972 VariantInit(&tv);
2973 lvt = V_VT(left)&VT_TYPEMASK;
2974 rvt = V_VT(right)&VT_TYPEMASK;
2976 /* If we have any flag set (VT_ARRAY, VT_VECTOR, etc.) bail out.
2977 Same for any input variant type > VT_I8 */
2978 if (V_VT(left) & ~VT_TYPEMASK || V_VT(right) & ~VT_TYPEMASK ||
2979 lvt > VT_I8 || rvt > VT_I8) {
2980 hres = DISP_E_BADVARTYPE;
2981 goto end;
2984 /* Determine the variant type to coerce to. */
2985 if (coerce[lvt] > coerce[rvt]) {
2986 resvt = prio2vt[coerce[lvt]];
2987 tvt = prio2vt[coerce[rvt]];
2988 } else {
2989 resvt = prio2vt[coerce[rvt]];
2990 tvt = prio2vt[coerce[lvt]];
2993 /* Special cases where the result variant type is defined by both
2994 input variants and not only that with the highest priority */
2995 if (resvt == VT_R4 && (tvt == VT_CY || tvt == VT_I8 || tvt == VT_I4))
2996 resvt = VT_R8;
2997 if (lvt == VT_EMPTY && rvt == VT_EMPTY)
2998 resvt = VT_I2;
3000 /* For overflow detection use the biggest compatible type for the
3001 multiplication */
3002 switch (resvt) {
3003 case VT_ERROR:
3004 hres = DISP_E_BADVARTYPE;
3005 goto end;
3006 case VT_NULL:
3007 hres = S_OK;
3008 V_VT(result) = VT_NULL;
3009 goto end;
3010 case VT_UI1:
3011 case VT_I2:
3012 case VT_I4:
3013 case VT_I8:
3014 tvt = VT_I8;
3015 break;
3016 case VT_R4:
3017 tvt = VT_R8;
3018 break;
3019 default:
3020 tvt = resvt;
3023 /* Now coerce the variants */
3024 hres = VariantChangeType(&lv, left, 0, tvt);
3025 if (FAILED(hres))
3026 goto end;
3027 hres = VariantChangeType(&rv, right, 0, tvt);
3028 if (FAILED(hres))
3029 goto end;
3031 /* Do the math */
3032 hres = S_OK;
3033 V_VT(&tv) = tvt;
3034 V_VT(result) = resvt;
3035 switch (tvt) {
3036 case VT_DECIMAL:
3037 hres = VarDecMul(&V_DECIMAL(&lv), &V_DECIMAL(&rv),
3038 &V_DECIMAL(result));
3039 goto end;
3040 case VT_CY:
3041 hres = VarCyMul(V_CY(&lv), V_CY(&rv), &V_CY(result));
3042 goto end;
3043 case VT_I8:
3044 /* Overflow detection */
3045 r8res = (double)V_I8(&lv) * (double)V_I8(&rv);
3046 if (r8res > (double)I8_MAX || r8res < (double)I8_MIN) {
3047 V_VT(result) = VT_R8;
3048 V_R8(result) = r8res;
3049 goto end;
3050 } else
3051 V_I8(&tv) = V_I8(&lv) * V_I8(&rv);
3052 break;
3053 case VT_R8:
3054 /* FIXME: overflow detection */
3055 V_R8(&tv) = V_R8(&lv) * V_R8(&rv);
3056 break;
3057 default:
3058 ERR("We shouldn't get here! tvt = %d!\n", tvt);
3059 break;
3061 if (rvt != tvt) {
3062 while ((hres = VariantChangeType(result, &tv, 0, resvt)) != S_OK) {
3063 /* Overflow! Change to the vartype with the next higher priority */
3064 resvt = prio2vt[coerce[resvt] + 1];
3066 } else
3067 hres = VariantCopy(result, &tv);
3069 end:
3070 if (hres != S_OK) {
3071 V_VT(result) = VT_EMPTY;
3072 V_I4(result) = 0; /* No V_EMPTY */
3074 VariantClear(&lv);
3075 VariantClear(&rv);
3076 VariantClear(&tv);
3077 TRACE("returning 0x%8lx (variant type %s)\n", hres, debugstr_VT(result));
3078 return hres;
3081 /**********************************************************************
3082 * VarDiv [OLEAUT32.143]
3085 HRESULT WINAPI VarDiv(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3087 HRESULT rc = E_FAIL;
3088 VARTYPE lvt,rvt,resvt;
3089 VARIANT lv,rv;
3090 BOOL found;
3092 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
3093 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
3095 VariantInit(&lv);VariantInit(&rv);
3096 lvt = V_VT(left)&VT_TYPEMASK;
3097 rvt = V_VT(right)&VT_TYPEMASK;
3098 found = FALSE;resvt = VT_VOID;
3099 if (((1<<lvt) | (1<<rvt)) & (VTBIT_R4|VTBIT_R8)) {
3100 found = TRUE;
3101 resvt = VT_R8;
3103 if (!found && (((1<<lvt) | (1<<rvt)) & (VTBIT_I1|VTBIT_I2|VTBIT_UI1|VTBIT_UI2|VTBIT_I4|VTBIT_UI4|(1<<VT_INT)|(1<<VT_UINT)))) {
3104 found = TRUE;
3105 resvt = VT_I4;
3107 if (!found) {
3108 FIXME("can't expand vt %d vs %d to a target type.\n",lvt,rvt);
3109 return E_FAIL;
3111 rc = VariantChangeType(&lv, left, 0, resvt);
3112 if (FAILED(rc)) {
3113 FIXME("Could not convert 0x%x to %d?\n",V_VT(left),resvt);
3114 return rc;
3116 rc = VariantChangeType(&rv, right, 0, resvt);
3117 if (FAILED(rc)) {
3118 FIXME("Could not convert 0x%x to %d?\n",V_VT(right),resvt);
3119 return rc;
3121 switch (resvt) {
3122 case VT_R8:
3123 if (V_R8(&rv) == 0) return DISP_E_DIVBYZERO;
3124 V_VT(result) = resvt;
3125 V_R8(result) = V_R8(&lv) / V_R8(&rv);
3126 rc = S_OK;
3127 break;
3128 case VT_I4:
3129 if (V_I4(&rv) == 0) return DISP_E_DIVBYZERO;
3130 V_VT(result) = resvt;
3131 V_I4(result) = V_I4(&lv) / V_I4(&rv);
3132 rc = S_OK;
3133 break;
3135 TRACE("returning 0x%8lx (%s%s),%g\n", rc, debugstr_VT(result),
3136 debugstr_VF(result), V_VT(result) == VT_R8 ? V_R8(result) : (double)V_I4(result));
3137 return rc;
3140 /**********************************************************************
3141 * VarSub [OLEAUT32.159]
3144 HRESULT WINAPI VarSub(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3146 HRESULT rc = E_FAIL;
3147 VARTYPE lvt,rvt,resvt;
3148 VARIANT lv,rv;
3149 BOOL found;
3151 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
3152 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
3154 VariantInit(&lv);VariantInit(&rv);
3155 lvt = V_VT(left)&VT_TYPEMASK;
3156 rvt = V_VT(right)&VT_TYPEMASK;
3157 found = FALSE;resvt = VT_VOID;
3158 if (((1<<lvt) | (1<<rvt)) & ((1<<VT_DATE)|(1<<VT_R4)|(1<<VT_R8))) {
3159 found = TRUE;
3160 resvt = VT_R8;
3162 if (!found && (((1<<lvt) | (1<<rvt)) & (VTBIT_I1|VTBIT_I2|VTBIT_UI1|VTBIT_UI2|VTBIT_I4|VTBIT_UI4|(1<<VT_INT)|(1<<VT_UINT)))) {
3163 found = TRUE;
3164 resvt = VT_I4;
3166 if (!found) {
3167 FIXME("can't expand vt %d vs %d to a target type.\n",lvt,rvt);
3168 return E_FAIL;
3170 rc = VariantChangeType(&lv, left, 0, resvt);
3171 if (FAILED(rc)) {
3172 FIXME("Could not convert 0x%x to %d?\n",V_VT(left),resvt);
3173 return rc;
3175 rc = VariantChangeType(&rv, right, 0, resvt);
3176 if (FAILED(rc)) {
3177 FIXME("Could not convert 0x%x to %d?\n",V_VT(right),resvt);
3178 return rc;
3180 switch (resvt) {
3181 case VT_R8:
3182 V_VT(result) = resvt;
3183 V_R8(result) = V_R8(&lv) - V_R8(&rv);
3184 rc = S_OK;
3185 break;
3186 case VT_I4:
3187 V_VT(result) = resvt;
3188 V_I4(result) = V_I4(&lv) - V_I4(&rv);
3189 rc = S_OK;
3190 break;
3192 TRACE("returning 0x%8lx (%s%s),%g\n", rc, debugstr_VT(result),
3193 debugstr_VF(result), V_VT(result) == VT_R8 ? V_R8(result) : (double)V_I4(result));
3194 return rc;
3197 /**********************************************************************
3198 * VarOr [OLEAUT32.157]
3200 * Perform a logical or (OR) operation on two variants.
3202 * PARAMS
3203 * pVarLeft [I] First variant
3204 * pVarRight [I] Variant to OR with pVarLeft
3205 * pVarOut [O] Destination for OR result
3207 * RETURNS
3208 * Success: S_OK. pVarOut contains the result of the operation with its type
3209 * taken from the table listed under VarXor().
3210 * Failure: An HRESULT error code indicating the error.
3212 * NOTES
3213 * See the Notes section of VarXor() for further information.
3215 HRESULT WINAPI VarOr(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
3217 VARTYPE vt = VT_I4;
3218 VARIANT varLeft, varRight, varStr;
3219 HRESULT hRet;
3221 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", pVarLeft, debugstr_VT(pVarLeft),
3222 debugstr_VF(pVarLeft), pVarRight, debugstr_VT(pVarRight),
3223 debugstr_VF(pVarRight), pVarOut);
3225 if (V_EXTRA_TYPE(pVarLeft) || V_EXTRA_TYPE(pVarRight) ||
3226 V_VT(pVarLeft) == VT_UNKNOWN || V_VT(pVarRight) == VT_UNKNOWN ||
3227 V_VT(pVarLeft) == VT_DISPATCH || V_VT(pVarRight) == VT_DISPATCH ||
3228 V_VT(pVarLeft) == VT_RECORD || V_VT(pVarRight) == VT_RECORD)
3229 return DISP_E_BADVARTYPE;
3231 V_VT(&varLeft) = V_VT(&varRight) = V_VT(&varStr) = VT_EMPTY;
3233 if (V_VT(pVarLeft) == VT_NULL || V_VT(pVarRight) == VT_NULL)
3235 /* NULL OR Zero is NULL, NULL OR value is value */
3236 if (V_VT(pVarLeft) == VT_NULL)
3237 pVarLeft = pVarRight; /* point to the non-NULL var */
3239 V_VT(pVarOut) = VT_NULL;
3240 V_I4(pVarOut) = 0;
3242 switch (V_VT(pVarLeft))
3244 case VT_DATE: case VT_R8:
3245 if (V_R8(pVarLeft))
3246 goto VarOr_AsEmpty;
3247 return S_OK;
3248 case VT_BOOL:
3249 if (V_BOOL(pVarLeft))
3250 *pVarOut = *pVarLeft;
3251 return S_OK;
3252 case VT_I2: case VT_UI2:
3253 if (V_I2(pVarLeft))
3254 goto VarOr_AsEmpty;
3255 return S_OK;
3256 case VT_I1:
3257 if (V_I1(pVarLeft))
3258 goto VarOr_AsEmpty;
3259 return S_OK;
3260 case VT_UI1:
3261 if (V_UI1(pVarLeft))
3262 *pVarOut = *pVarLeft;
3263 return S_OK;
3264 case VT_R4:
3265 if (V_R4(pVarLeft))
3266 goto VarOr_AsEmpty;
3267 return S_OK;
3268 case VT_I4: case VT_UI4: case VT_INT: case VT_UINT:
3269 if (V_I4(pVarLeft))
3270 goto VarOr_AsEmpty;
3271 return S_OK;
3272 case VT_CY:
3273 if (V_CY(pVarLeft).int64)
3274 goto VarOr_AsEmpty;
3275 return S_OK;
3276 case VT_I8: case VT_UI8:
3277 if (V_I8(pVarLeft))
3278 goto VarOr_AsEmpty;
3279 return S_OK;
3280 case VT_DECIMAL:
3281 if (DEC_HI32(&V_DECIMAL(pVarLeft)) || DEC_LO64(&V_DECIMAL(pVarLeft)))
3282 goto VarOr_AsEmpty;
3283 return S_OK;
3284 case VT_BSTR:
3286 VARIANT_BOOL b;
3288 if (!V_BSTR(pVarLeft))
3289 return DISP_E_BADVARTYPE;
3291 hRet = VarBoolFromStr(V_BSTR(pVarLeft), LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
3292 if (SUCCEEDED(hRet) && b)
3294 V_VT(pVarOut) = VT_BOOL;
3295 V_BOOL(pVarOut) = b;
3297 return hRet;
3299 case VT_NULL: case VT_EMPTY:
3300 V_VT(pVarOut) = VT_NULL;
3301 return S_OK;
3302 default:
3303 return DISP_E_BADVARTYPE;
3307 if (V_VT(pVarLeft) == VT_EMPTY || V_VT(pVarRight) == VT_EMPTY)
3309 if (V_VT(pVarLeft) == VT_EMPTY)
3310 pVarLeft = pVarRight; /* point to the non-EMPTY var */
3312 VarOr_AsEmpty:
3313 /* Since one argument is empty (0), OR'ing it with the other simply
3314 * gives the others value (as 0|x => x). So just convert the other
3315 * argument to the required result type.
3317 switch (V_VT(pVarLeft))
3319 case VT_BSTR:
3320 if (!V_BSTR(pVarLeft))
3321 return DISP_E_BADVARTYPE;
3323 hRet = VariantCopy(&varStr, pVarLeft);
3324 if (FAILED(hRet))
3325 goto VarOr_Exit;
3326 pVarLeft = &varStr;
3327 hRet = VariantChangeType(pVarLeft, pVarLeft, 0, VT_BOOL);
3328 if (FAILED(hRet))
3329 goto VarOr_Exit;
3330 /* Fall Through ... */
3331 case VT_EMPTY: case VT_UI1: case VT_BOOL: case VT_I2:
3332 V_VT(pVarOut) = VT_I2;
3333 break;
3334 case VT_DATE: case VT_CY: case VT_DECIMAL: case VT_R4: case VT_R8:
3335 case VT_I1: case VT_UI2: case VT_I4: case VT_UI4:
3336 case VT_INT: case VT_UINT: case VT_UI8:
3337 V_VT(pVarOut) = VT_I4;
3338 break;
3339 case VT_I8:
3340 V_VT(pVarOut) = VT_I8;
3341 break;
3342 default:
3343 return DISP_E_BADVARTYPE;
3345 hRet = VariantCopy(&varLeft, pVarLeft);
3346 if (FAILED(hRet))
3347 goto VarOr_Exit;
3348 pVarLeft = &varLeft;
3349 hRet = VariantChangeType(pVarOut, pVarLeft, 0, V_VT(pVarOut));
3350 goto VarOr_Exit;
3353 if (V_VT(pVarLeft) == VT_BOOL && V_VT(pVarRight) == VT_BOOL)
3355 V_VT(pVarOut) = VT_BOOL;
3356 V_BOOL(pVarOut) = V_BOOL(pVarLeft) | V_BOOL(pVarRight);
3357 return S_OK;
3360 if (V_VT(pVarLeft) == VT_UI1 && V_VT(pVarRight) == VT_UI1)
3362 V_VT(pVarOut) = VT_UI1;
3363 V_UI1(pVarOut) = V_UI1(pVarLeft) | V_UI1(pVarRight);
3364 return S_OK;
3367 if (V_VT(pVarLeft) == VT_BSTR)
3369 hRet = VariantCopy(&varStr, pVarLeft);
3370 if (FAILED(hRet))
3371 goto VarOr_Exit;
3372 pVarLeft = &varStr;
3373 hRet = VariantChangeType(pVarLeft, pVarLeft, 0, VT_BOOL);
3374 if (FAILED(hRet))
3375 goto VarOr_Exit;
3378 if (V_VT(pVarLeft) == VT_BOOL &&
3379 (V_VT(pVarRight) == VT_BOOL || V_VT(pVarRight) == VT_BSTR))
3381 vt = VT_BOOL;
3383 else if ((V_VT(pVarLeft) == VT_BOOL || V_VT(pVarLeft) == VT_UI1 ||
3384 V_VT(pVarLeft) == VT_I2 || V_VT(pVarLeft) == VT_BSTR) &&
3385 (V_VT(pVarRight) == VT_BOOL || V_VT(pVarRight) == VT_UI1 ||
3386 V_VT(pVarRight) == VT_I2 || V_VT(pVarRight) == VT_BSTR))
3388 vt = VT_I2;
3390 else if (V_VT(pVarLeft) == VT_I8 || V_VT(pVarRight) == VT_I8)
3392 if (V_VT(pVarLeft) == VT_INT || V_VT(pVarRight) == VT_INT)
3393 return DISP_E_TYPEMISMATCH;
3394 vt = VT_I8;
3397 hRet = VariantCopy(&varLeft, pVarLeft);
3398 if (FAILED(hRet))
3399 goto VarOr_Exit;
3401 hRet = VariantCopy(&varRight, pVarRight);
3402 if (FAILED(hRet))
3403 goto VarOr_Exit;
3405 if (vt == VT_I4 && V_VT(&varLeft) == VT_UI4)
3406 V_VT(&varLeft) = VT_I4; /* Don't overflow */
3407 else
3409 double d;
3411 if (V_VT(&varLeft) == VT_BSTR &&
3412 FAILED(VarR8FromStr(V_BSTR(&varLeft), LOCALE_USER_DEFAULT, 0, &d)))
3413 hRet = VariantChangeType(&varLeft, &varLeft, VARIANT_LOCALBOOL, VT_BOOL);
3414 if (SUCCEEDED(hRet) && V_VT(&varLeft) != vt)
3415 hRet = VariantChangeType(&varLeft, &varLeft, 0, vt);
3416 if (FAILED(hRet))
3417 goto VarOr_Exit;
3420 if (vt == VT_I4 && V_VT(&varRight) == VT_UI4)
3421 V_VT(&varRight) = VT_I4; /* Don't overflow */
3422 else
3424 double d;
3426 if (V_VT(&varRight) == VT_BSTR &&
3427 FAILED(VarR8FromStr(V_BSTR(&varRight), LOCALE_USER_DEFAULT, 0, &d)))
3428 hRet = VariantChangeType(&varRight, &varRight, VARIANT_LOCALBOOL, VT_BOOL);
3429 if (SUCCEEDED(hRet) && V_VT(&varRight) != vt)
3430 hRet = VariantChangeType(&varRight, &varRight, 0, vt);
3431 if (FAILED(hRet))
3432 goto VarOr_Exit;
3435 V_VT(pVarOut) = vt;
3436 if (vt == VT_I8)
3438 V_I8(pVarOut) = V_I8(&varLeft) | V_I8(&varRight);
3440 else if (vt == VT_I4)
3442 V_I4(pVarOut) = V_I4(&varLeft) | V_I4(&varRight);
3444 else
3446 V_I2(pVarOut) = V_I2(&varLeft) | V_I2(&varRight);
3449 VarOr_Exit:
3450 VariantClear(&varStr);
3451 VariantClear(&varLeft);
3452 VariantClear(&varRight);
3453 return hRet;
3456 /**********************************************************************
3457 * VarAbs [OLEAUT32.168]
3459 * Convert a variant to its absolute value.
3461 * PARAMS
3462 * pVarIn [I] Source variant
3463 * pVarOut [O] Destination for converted value
3465 * RETURNS
3466 * Success: S_OK. pVarOut contains the absolute value of pVarIn.
3467 * Failure: An HRESULT error code indicating the error.
3469 * NOTES
3470 * - This function does not process by-reference variants.
3471 * - The type of the value stored in pVarOut depends on the type of pVarIn,
3472 * according to the following table:
3473 *| Input Type Output Type
3474 *| ---------- -----------
3475 *| VT_BOOL VT_I2
3476 *| VT_BSTR VT_R8
3477 *| (All others) Unchanged
3479 HRESULT WINAPI VarAbs(LPVARIANT pVarIn, LPVARIANT pVarOut)
3481 VARIANT varIn;
3482 HRESULT hRet = S_OK;
3484 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
3485 debugstr_VF(pVarIn), pVarOut);
3487 if (V_ISARRAY(pVarIn) || V_VT(pVarIn) == VT_UNKNOWN ||
3488 V_VT(pVarIn) == VT_DISPATCH || V_VT(pVarIn) == VT_RECORD ||
3489 V_VT(pVarIn) == VT_ERROR)
3490 return DISP_E_TYPEMISMATCH;
3492 *pVarOut = *pVarIn; /* Shallow copy the value, and invert it if needed */
3494 #define ABS_CASE(typ,min) \
3495 case VT_##typ: if (V_##typ(pVarIn) == min) hRet = DISP_E_OVERFLOW; \
3496 else if (V_##typ(pVarIn) < 0) V_##typ(pVarOut) = -V_##typ(pVarIn); \
3497 break
3499 switch (V_VT(pVarIn))
3501 ABS_CASE(I1,I1_MIN);
3502 case VT_BOOL:
3503 V_VT(pVarOut) = VT_I2;
3504 /* BOOL->I2, Fall through ... */
3505 ABS_CASE(I2,I2_MIN);
3506 case VT_INT:
3507 ABS_CASE(I4,I4_MIN);
3508 ABS_CASE(I8,I8_MIN);
3509 ABS_CASE(R4,R4_MIN);
3510 case VT_BSTR:
3511 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn));
3512 if (FAILED(hRet))
3513 break;
3514 V_VT(pVarOut) = VT_R8;
3515 pVarIn = &varIn;
3516 /* Fall through ... */
3517 case VT_DATE:
3518 ABS_CASE(R8,R8_MIN);
3519 case VT_CY:
3520 hRet = VarCyAbs(V_CY(pVarIn), & V_CY(pVarOut));
3521 break;
3522 case VT_DECIMAL:
3523 DEC_SIGN(&V_DECIMAL(pVarOut)) &= ~DECIMAL_NEG;
3524 break;
3525 case VT_UI1:
3526 case VT_UI2:
3527 case VT_UINT:
3528 case VT_UI4:
3529 case VT_UI8:
3530 /* No-Op */
3531 break;
3532 case VT_EMPTY:
3533 V_VT(pVarOut) = VT_I2;
3534 case VT_NULL:
3535 V_I2(pVarOut) = 0;
3536 break;
3537 default:
3538 hRet = DISP_E_BADVARTYPE;
3541 return hRet;
3544 /**********************************************************************
3545 * VarFix [OLEAUT32.169]
3547 * Truncate a variants value to a whole number.
3549 * PARAMS
3550 * pVarIn [I] Source variant
3551 * pVarOut [O] Destination for converted value
3553 * RETURNS
3554 * Success: S_OK. pVarOut contains the converted value.
3555 * Failure: An HRESULT error code indicating the error.
3557 * NOTES
3558 * - The type of the value stored in pVarOut depends on the type of pVarIn,
3559 * according to the following table:
3560 *| Input Type Output Type
3561 *| ---------- -----------
3562 *| VT_BOOL VT_I2
3563 *| VT_EMPTY VT_I2
3564 *| VT_BSTR VT_R8
3565 *| All Others Unchanged
3566 * - The difference between this function and VarInt() is that VarInt() rounds
3567 * negative numbers away from 0, while this function rounds them towards zero.
3569 HRESULT WINAPI VarFix(LPVARIANT pVarIn, LPVARIANT pVarOut)
3571 HRESULT hRet = S_OK;
3573 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
3574 debugstr_VF(pVarIn), pVarOut);
3576 V_VT(pVarOut) = V_VT(pVarIn);
3578 switch (V_VT(pVarIn))
3580 case VT_UI1:
3581 V_UI1(pVarOut) = V_UI1(pVarIn);
3582 break;
3583 case VT_BOOL:
3584 V_VT(pVarOut) = VT_I2;
3585 /* Fall through */
3586 case VT_I2:
3587 V_I2(pVarOut) = V_I2(pVarIn);
3588 break;
3589 case VT_I4:
3590 V_I4(pVarOut) = V_I4(pVarIn);
3591 break;
3592 case VT_I8:
3593 V_I8(pVarOut) = V_I8(pVarIn);
3594 break;
3595 case VT_R4:
3596 if (V_R4(pVarIn) < 0.0f)
3597 V_R4(pVarOut) = (float)ceil(V_R4(pVarIn));
3598 else
3599 V_R4(pVarOut) = (float)floor(V_R4(pVarIn));
3600 break;
3601 case VT_BSTR:
3602 V_VT(pVarOut) = VT_R8;
3603 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(pVarOut));
3604 pVarIn = pVarOut;
3605 /* Fall through */
3606 case VT_DATE:
3607 case VT_R8:
3608 if (V_R8(pVarIn) < 0.0)
3609 V_R8(pVarOut) = ceil(V_R8(pVarIn));
3610 else
3611 V_R8(pVarOut) = floor(V_R8(pVarIn));
3612 break;
3613 case VT_CY:
3614 hRet = VarCyFix(V_CY(pVarIn), &V_CY(pVarOut));
3615 break;
3616 case VT_DECIMAL:
3617 hRet = VarDecFix(&V_DECIMAL(pVarIn), &V_DECIMAL(pVarOut));
3618 break;
3619 case VT_EMPTY:
3620 V_VT(pVarOut) = VT_I2;
3621 V_I2(pVarOut) = 0;
3622 break;
3623 case VT_NULL:
3624 /* No-Op */
3625 break;
3626 default:
3627 if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
3628 FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
3629 hRet = DISP_E_BADVARTYPE;
3630 else
3631 hRet = DISP_E_TYPEMISMATCH;
3633 if (FAILED(hRet))
3634 V_VT(pVarOut) = VT_EMPTY;
3636 return hRet;
3639 /**********************************************************************
3640 * VarInt [OLEAUT32.172]
3642 * Truncate a variants value to a whole number.
3644 * PARAMS
3645 * pVarIn [I] Source variant
3646 * pVarOut [O] Destination for converted value
3648 * RETURNS
3649 * Success: S_OK. pVarOut contains the converted value.
3650 * Failure: An HRESULT error code indicating the error.
3652 * NOTES
3653 * - The type of the value stored in pVarOut depends on the type of pVarIn,
3654 * according to the following table:
3655 *| Input Type Output Type
3656 *| ---------- -----------
3657 *| VT_BOOL VT_I2
3658 *| VT_EMPTY VT_I2
3659 *| VT_BSTR VT_R8
3660 *| All Others Unchanged
3661 * - The difference between this function and VarFix() is that VarFix() rounds
3662 * negative numbers towards 0, while this function rounds them away from zero.
3664 HRESULT WINAPI VarInt(LPVARIANT pVarIn, LPVARIANT pVarOut)
3666 HRESULT hRet = S_OK;
3668 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
3669 debugstr_VF(pVarIn), pVarOut);
3671 V_VT(pVarOut) = V_VT(pVarIn);
3673 switch (V_VT(pVarIn))
3675 case VT_R4:
3676 V_R4(pVarOut) = (float)floor(V_R4(pVarIn));
3677 break;
3678 case VT_BSTR:
3679 V_VT(pVarOut) = VT_R8;
3680 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(pVarOut));
3681 pVarIn = pVarOut;
3682 /* Fall through */
3683 case VT_DATE:
3684 case VT_R8:
3685 V_R8(pVarOut) = floor(V_R8(pVarIn));
3686 break;
3687 case VT_CY:
3688 hRet = VarCyInt(V_CY(pVarIn), &V_CY(pVarOut));
3689 break;
3690 case VT_DECIMAL:
3691 hRet = VarDecInt(&V_DECIMAL(pVarIn), &V_DECIMAL(pVarOut));
3692 break;
3693 default:
3694 return VarFix(pVarIn, pVarOut);
3697 return hRet;
3700 /**********************************************************************
3701 * VarXor [OLEAUT32.167]
3703 * Perform a logical exclusive-or (XOR) operation on two variants.
3705 * PARAMS
3706 * pVarLeft [I] First variant
3707 * pVarRight [I] Variant to XOR with pVarLeft
3708 * pVarOut [O] Destination for XOR result
3710 * RETURNS
3711 * Success: S_OK. pVarOut contains the result of the operation with its type
3712 * taken from the table below).
3713 * Failure: An HRESULT error code indicating the error.
3715 * NOTES
3716 * - Neither pVarLeft or pVarRight are modified by this function.
3717 * - This function does not process by-reference variants.
3718 * - Input types of VT_BSTR may be numeric strings or boolean text.
3719 * - The type of result stored in pVarOut depends on the types of pVarLeft
3720 * and pVarRight, and will be one of VT_UI1, VT_I2, VT_I4, VT_I8, VT_BOOL,
3721 * or VT_NULL if the function succeeds.
3722 * - Type promotion is inconsistent and as a result certain combinations of
3723 * values will return DISP_E_OVERFLOW even when they could be represented.
3724 * This matches the behaviour of native oleaut32.
3726 HRESULT WINAPI VarXor(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
3728 VARTYPE vt;
3729 VARIANT varLeft, varRight;
3730 double d;
3731 HRESULT hRet;
3733 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", pVarLeft, debugstr_VT(pVarLeft),
3734 debugstr_VF(pVarLeft), pVarRight, debugstr_VT(pVarRight),
3735 debugstr_VF(pVarRight), pVarOut);
3737 if (V_EXTRA_TYPE(pVarLeft) || V_EXTRA_TYPE(pVarRight) ||
3738 V_VT(pVarLeft) > VT_UINT || V_VT(pVarRight) > VT_UINT ||
3739 V_VT(pVarLeft) == VT_VARIANT || V_VT(pVarRight) == VT_VARIANT ||
3740 V_VT(pVarLeft) == VT_UNKNOWN || V_VT(pVarRight) == VT_UNKNOWN ||
3741 V_VT(pVarLeft) == (VARTYPE)15 || V_VT(pVarRight) == (VARTYPE)15 ||
3742 V_VT(pVarLeft) == VT_ERROR || V_VT(pVarRight) == VT_ERROR)
3743 return DISP_E_BADVARTYPE;
3745 if (V_VT(pVarLeft) == VT_NULL || V_VT(pVarRight) == VT_NULL)
3747 /* NULL XOR anything valid is NULL */
3748 V_VT(pVarOut) = VT_NULL;
3749 return S_OK;
3752 /* Copy our inputs so we don't disturb anything */
3753 V_VT(&varLeft) = V_VT(&varRight) = VT_EMPTY;
3755 hRet = VariantCopy(&varLeft, pVarLeft);
3756 if (FAILED(hRet))
3757 goto VarXor_Exit;
3759 hRet = VariantCopy(&varRight, pVarRight);
3760 if (FAILED(hRet))
3761 goto VarXor_Exit;
3763 /* Try any strings first as numbers, then as VT_BOOL */
3764 if (V_VT(&varLeft) == VT_BSTR)
3766 hRet = VarR8FromStr(V_BSTR(&varLeft), LOCALE_USER_DEFAULT, 0, &d);
3767 hRet = VariantChangeType(&varLeft, &varLeft, VARIANT_LOCALBOOL,
3768 FAILED(hRet) ? VT_BOOL : VT_I4);
3769 if (FAILED(hRet))
3770 goto VarXor_Exit;
3773 if (V_VT(&varRight) == VT_BSTR)
3775 hRet = VarR8FromStr(V_BSTR(&varRight), LOCALE_USER_DEFAULT, 0, &d);
3776 hRet = VariantChangeType(&varRight, &varRight, VARIANT_LOCALBOOL,
3777 FAILED(hRet) ? VT_BOOL : VT_I4);
3778 if (FAILED(hRet))
3779 goto VarXor_Exit;
3782 /* Determine the result type */
3783 if (V_VT(&varLeft) == VT_I8 || V_VT(&varRight) == VT_I8)
3785 if (V_VT(pVarLeft) == VT_INT || V_VT(pVarRight) == VT_INT)
3786 return DISP_E_TYPEMISMATCH;
3787 vt = VT_I8;
3789 else
3791 switch ((V_VT(&varLeft) << 16) | V_VT(&varRight))
3793 case (VT_BOOL << 16) | VT_BOOL:
3794 vt = VT_BOOL;
3795 break;
3796 case (VT_UI1 << 16) | VT_UI1:
3797 vt = VT_UI1;
3798 break;
3799 case (VT_EMPTY << 16) | VT_EMPTY:
3800 case (VT_EMPTY << 16) | VT_UI1:
3801 case (VT_EMPTY << 16) | VT_I2:
3802 case (VT_EMPTY << 16) | VT_BOOL:
3803 case (VT_UI1 << 16) | VT_EMPTY:
3804 case (VT_UI1 << 16) | VT_I2:
3805 case (VT_UI1 << 16) | VT_BOOL:
3806 case (VT_I2 << 16) | VT_EMPTY:
3807 case (VT_I2 << 16) | VT_UI1:
3808 case (VT_I2 << 16) | VT_I2:
3809 case (VT_I2 << 16) | VT_BOOL:
3810 case (VT_BOOL << 16) | VT_EMPTY:
3811 case (VT_BOOL << 16) | VT_UI1:
3812 case (VT_BOOL << 16) | VT_I2:
3813 vt = VT_I2;
3814 break;
3815 default:
3816 vt = VT_I4;
3817 break;
3821 /* VT_UI4 does not overflow */
3822 if (vt != VT_I8)
3824 if (V_VT(&varLeft) == VT_UI4)
3825 V_VT(&varLeft) = VT_I4;
3826 if (V_VT(&varRight) == VT_UI4)
3827 V_VT(&varRight) = VT_I4;
3830 /* Convert our input copies to the result type */
3831 if (V_VT(&varLeft) != vt)
3832 hRet = VariantChangeType(&varLeft, &varLeft, 0, vt);
3833 if (FAILED(hRet))
3834 goto VarXor_Exit;
3836 if (V_VT(&varRight) != vt)
3837 hRet = VariantChangeType(&varRight, &varRight, 0, vt);
3838 if (FAILED(hRet))
3839 goto VarXor_Exit;
3841 V_VT(pVarOut) = vt;
3843 /* Calculate the result */
3844 switch (vt)
3846 case VT_I8:
3847 V_I8(pVarOut) = V_I8(&varLeft) ^ V_I8(&varRight);
3848 break;
3849 case VT_I4:
3850 V_I4(pVarOut) = V_I4(&varLeft) ^ V_I4(&varRight);
3851 break;
3852 case VT_BOOL:
3853 case VT_I2:
3854 V_I2(pVarOut) = V_I2(&varLeft) ^ V_I2(&varRight);
3855 break;
3856 case VT_UI1:
3857 V_UI1(pVarOut) = V_UI1(&varLeft) ^ V_UI1(&varRight);
3858 break;
3861 VarXor_Exit:
3862 VariantClear(&varLeft);
3863 VariantClear(&varRight);
3864 return hRet;
3867 /**********************************************************************
3868 * VarEqv [OLEAUT32.172]
3870 * Determine if two variants contain the same value.
3872 * PARAMS
3873 * pVarLeft [I] First variant to compare
3874 * pVarRight [I] Variant to compare to pVarLeft
3875 * pVarOut [O] Destination for comparison result
3877 * RETURNS
3878 * Success: S_OK. pVarOut contains the result of the comparison (VARIANT_TRUE
3879 * if equivalent or non-zero otherwise.
3880 * Failure: An HRESULT error code indicating the error.
3882 * NOTES
3883 * - This function simply calls VarXor() on pVarLeft and pVarRight and inverts
3884 * the result.
3886 HRESULT WINAPI VarEqv(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
3888 HRESULT hRet;
3890 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", pVarLeft, debugstr_VT(pVarLeft),
3891 debugstr_VF(pVarLeft), pVarRight, debugstr_VT(pVarRight),
3892 debugstr_VF(pVarRight), pVarOut);
3894 hRet = VarXor(pVarLeft, pVarRight, pVarOut);
3895 if (SUCCEEDED(hRet))
3897 if (V_VT(pVarOut) == VT_I8)
3898 V_I8(pVarOut) = ~V_I8(pVarOut);
3899 else
3900 V_UI4(pVarOut) = ~V_UI4(pVarOut);
3902 return hRet;
3905 /**********************************************************************
3906 * VarNeg [OLEAUT32.173]
3908 * Negate the value of a variant.
3910 * PARAMS
3911 * pVarIn [I] Source variant
3912 * pVarOut [O] Destination for converted value
3914 * RETURNS
3915 * Success: S_OK. pVarOut contains the converted value.
3916 * Failure: An HRESULT error code indicating the error.
3918 * NOTES
3919 * - The type of the value stored in pVarOut depends on the type of pVarIn,
3920 * according to the following table:
3921 *| Input Type Output Type
3922 *| ---------- -----------
3923 *| VT_EMPTY VT_I2
3924 *| VT_UI1 VT_I2
3925 *| VT_BOOL VT_I2
3926 *| VT_BSTR VT_R8
3927 *| All Others Unchanged (unless promoted)
3928 * - Where the negated value of a variant does not fit in its base type, the type
3929 * is promoted according to the following table:
3930 *| Input Type Promoted To
3931 *| ---------- -----------
3932 *| VT_I2 VT_I4
3933 *| VT_I4 VT_R8
3934 *| VT_I8 VT_R8
3935 * - The native version of this function returns DISP_E_BADVARTYPE for valid
3936 * variant types that cannot be negated, and returns DISP_E_TYPEMISMATCH
3937 * for types which are not valid. Since this is in contravention of the
3938 * meaning of those error codes and unlikely to be relied on by applications,
3939 * this implementation returns errors consistent with the other high level
3940 * variant math functions.
3942 HRESULT WINAPI VarNeg(LPVARIANT pVarIn, LPVARIANT pVarOut)
3944 HRESULT hRet = S_OK;
3946 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
3947 debugstr_VF(pVarIn), pVarOut);
3949 V_VT(pVarOut) = V_VT(pVarIn);
3951 switch (V_VT(pVarIn))
3953 case VT_UI1:
3954 V_VT(pVarOut) = VT_I2;
3955 V_I2(pVarOut) = -V_UI1(pVarIn);
3956 break;
3957 case VT_BOOL:
3958 V_VT(pVarOut) = VT_I2;
3959 /* Fall through */
3960 case VT_I2:
3961 if (V_I2(pVarIn) == I2_MIN)
3963 V_VT(pVarOut) = VT_I4;
3964 V_I4(pVarOut) = -(int)V_I2(pVarIn);
3966 else
3967 V_I2(pVarOut) = -V_I2(pVarIn);
3968 break;
3969 case VT_I4:
3970 if (V_I4(pVarIn) == I4_MIN)
3972 V_VT(pVarOut) = VT_R8;
3973 V_R8(pVarOut) = -(double)V_I4(pVarIn);
3975 else
3976 V_I4(pVarOut) = -V_I4(pVarIn);
3977 break;
3978 case VT_I8:
3979 if (V_I8(pVarIn) == I8_MIN)
3981 V_VT(pVarOut) = VT_R8;
3982 hRet = VarR8FromI8(V_I8(pVarIn), &V_R8(pVarOut));
3983 V_R8(pVarOut) *= -1.0;
3985 else
3986 V_I8(pVarOut) = -V_I8(pVarIn);
3987 break;
3988 case VT_R4:
3989 V_R4(pVarOut) = -V_R4(pVarIn);
3990 break;
3991 case VT_DATE:
3992 case VT_R8:
3993 V_R8(pVarOut) = -V_R8(pVarIn);
3994 break;
3995 case VT_CY:
3996 hRet = VarCyNeg(V_CY(pVarIn), &V_CY(pVarOut));
3997 break;
3998 case VT_DECIMAL:
3999 hRet = VarDecNeg(&V_DECIMAL(pVarIn), &V_DECIMAL(pVarOut));
4000 break;
4001 case VT_BSTR:
4002 V_VT(pVarOut) = VT_R8;
4003 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(pVarOut));
4004 V_R8(pVarOut) = -V_R8(pVarOut);
4005 break;
4006 case VT_EMPTY:
4007 V_VT(pVarOut) = VT_I2;
4008 V_I2(pVarOut) = 0;
4009 break;
4010 case VT_NULL:
4011 /* No-Op */
4012 break;
4013 default:
4014 if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
4015 FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
4016 hRet = DISP_E_BADVARTYPE;
4017 else
4018 hRet = DISP_E_TYPEMISMATCH;
4020 if (FAILED(hRet))
4021 V_VT(pVarOut) = VT_EMPTY;
4023 return hRet;
4026 /**********************************************************************
4027 * VarNot [OLEAUT32.174]
4029 * Perform a not operation on a variant.
4031 * PARAMS
4032 * pVarIn [I] Source variant
4033 * pVarOut [O] Destination for converted value
4035 * RETURNS
4036 * Success: S_OK. pVarOut contains the converted value.
4037 * Failure: An HRESULT error code indicating the error.
4039 * NOTES
4040 * - Strictly speaking, this function performs a bitwise ones complement
4041 * on the variants value (after possibly converting to VT_I4, see below).
4042 * This only behaves like a boolean not operation if the value in
4043 * pVarIn is either VARIANT_TRUE or VARIANT_FALSE and the type is signed.
4044 * - To perform a genuine not operation, convert the variant to a VT_BOOL
4045 * before calling this function.
4046 * - This function does not process by-reference variants.
4047 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4048 * according to the following table:
4049 *| Input Type Output Type
4050 *| ---------- -----------
4051 *| VT_EMPTY VT_I2
4052 *| VT_R4 VT_I4
4053 *| VT_R8 VT_I4
4054 *| VT_BSTR VT_I4
4055 *| VT_DECIMAL VT_I4
4056 *| VT_CY VT_I4
4057 *| (All others) Unchanged
4059 HRESULT WINAPI VarNot(LPVARIANT pVarIn, LPVARIANT pVarOut)
4061 VARIANT varIn;
4062 HRESULT hRet = S_OK;
4064 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
4065 debugstr_VF(pVarIn), pVarOut);
4067 V_VT(pVarOut) = V_VT(pVarIn);
4069 switch (V_VT(pVarIn))
4071 case VT_I1:
4072 V_I4(pVarOut) = ~V_I1(pVarIn);
4073 V_VT(pVarOut) = VT_I4;
4074 break;
4075 case VT_UI1: V_UI1(pVarOut) = ~V_UI1(pVarIn); break;
4076 case VT_BOOL:
4077 case VT_I2: V_I2(pVarOut) = ~V_I2(pVarIn); break;
4078 case VT_UI2:
4079 V_I4(pVarOut) = ~V_UI2(pVarIn);
4080 V_VT(pVarOut) = VT_I4;
4081 break;
4082 case VT_DECIMAL:
4083 hRet = VarI4FromDec(&V_DECIMAL(pVarIn), &V_I4(&varIn));
4084 if (FAILED(hRet))
4085 break;
4086 pVarIn = &varIn;
4087 /* Fall through ... */
4088 case VT_INT:
4089 V_VT(pVarOut) = VT_I4;
4090 /* Fall through ... */
4091 case VT_I4: V_I4(pVarOut) = ~V_I4(pVarIn); break;
4092 case VT_UINT:
4093 case VT_UI4:
4094 V_I4(pVarOut) = ~V_UI4(pVarIn);
4095 V_VT(pVarOut) = VT_I4;
4096 break;
4097 case VT_I8: V_I8(pVarOut) = ~V_I8(pVarIn); break;
4098 case VT_UI8:
4099 V_I4(pVarOut) = ~V_UI8(pVarIn);
4100 V_VT(pVarOut) = VT_I4;
4101 break;
4102 case VT_R4:
4103 hRet = VarI4FromR4(V_R4(pVarIn), &V_I4(pVarOut));
4104 V_I4(pVarOut) = ~V_I4(pVarOut);
4105 V_VT(pVarOut) = VT_I4;
4106 break;
4107 case VT_BSTR:
4108 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn));
4109 if (FAILED(hRet))
4110 break;
4111 pVarIn = &varIn;
4112 /* Fall through ... */
4113 case VT_DATE:
4114 case VT_R8:
4115 hRet = VarI4FromR8(V_R8(pVarIn), &V_I4(pVarOut));
4116 V_I4(pVarOut) = ~V_I4(pVarOut);
4117 V_VT(pVarOut) = VT_I4;
4118 break;
4119 case VT_CY:
4120 hRet = VarI4FromCy(V_CY(pVarIn), &V_I4(pVarOut));
4121 V_I4(pVarOut) = ~V_I4(pVarOut);
4122 V_VT(pVarOut) = VT_I4;
4123 break;
4124 case VT_EMPTY:
4125 V_I2(pVarOut) = ~0;
4126 V_VT(pVarOut) = VT_I2;
4127 break;
4128 case VT_NULL:
4129 /* No-Op */
4130 break;
4131 default:
4132 if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
4133 FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
4134 hRet = DISP_E_BADVARTYPE;
4135 else
4136 hRet = DISP_E_TYPEMISMATCH;
4138 if (FAILED(hRet))
4139 V_VT(pVarOut) = VT_EMPTY;
4141 return hRet;
4144 /**********************************************************************
4145 * VarRound [OLEAUT32.175]
4147 * Perform a round operation on a variant.
4149 * PARAMS
4150 * pVarIn [I] Source variant
4151 * deci [I] Number of decimals to round to
4152 * pVarOut [O] Destination for converted value
4154 * RETURNS
4155 * Success: S_OK. pVarOut contains the converted value.
4156 * Failure: An HRESULT error code indicating the error.
4158 * NOTES
4159 * - Floating point values are rounded to the desired number of decimals.
4160 * - Some integer types are just copied to the return variable.
4161 * - Some other integer types are not handled and fail.
4163 HRESULT WINAPI VarRound(LPVARIANT pVarIn, int deci, LPVARIANT pVarOut)
4165 VARIANT varIn;
4166 HRESULT hRet = S_OK;
4167 float factor;
4169 TRACE("(%p->(%s%s),%d)\n", pVarIn, debugstr_VT(pVarIn), debugstr_VF(pVarIn), deci);
4171 switch (V_VT(pVarIn))
4173 /* cases that fail on windows */
4174 case VT_I1:
4175 case VT_I8:
4176 case VT_UI2:
4177 case VT_UI4:
4178 hRet = DISP_E_BADVARTYPE;
4179 break;
4181 /* cases just copying in to out */
4182 case VT_UI1:
4183 V_VT(pVarOut) = V_VT(pVarIn);
4184 V_UI1(pVarOut) = V_UI1(pVarIn);
4185 break;
4186 case VT_I2:
4187 V_VT(pVarOut) = V_VT(pVarIn);
4188 V_I2(pVarOut) = V_I2(pVarIn);
4189 break;
4190 case VT_I4:
4191 V_VT(pVarOut) = V_VT(pVarIn);
4192 V_I4(pVarOut) = V_I4(pVarIn);
4193 break;
4194 case VT_NULL:
4195 V_VT(pVarOut) = V_VT(pVarIn);
4196 /* value unchanged */
4197 break;
4199 /* cases that change type */
4200 case VT_EMPTY:
4201 V_VT(pVarOut) = VT_I2;
4202 V_I2(pVarOut) = 0;
4203 break;
4204 case VT_BOOL:
4205 V_VT(pVarOut) = VT_I2;
4206 V_I2(pVarOut) = V_BOOL(pVarIn);
4207 break;
4208 case VT_BSTR:
4209 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn));
4210 if (FAILED(hRet))
4211 break;
4212 V_VT(&varIn)=VT_R8;
4213 pVarIn = &varIn;
4214 /* Fall through ... */
4216 /* cases we need to do math */
4217 case VT_R8:
4218 if (V_R8(pVarIn)>0) {
4219 V_R8(pVarOut)=floor(V_R8(pVarIn)*pow(10, deci)+0.5)/pow(10, deci);
4220 } else {
4221 V_R8(pVarOut)=ceil(V_R8(pVarIn)*pow(10, deci)-0.5)/pow(10, deci);
4223 V_VT(pVarOut) = V_VT(pVarIn);
4224 break;
4225 case VT_R4:
4226 if (V_R4(pVarIn)>0) {
4227 V_R4(pVarOut)=floor(V_R4(pVarIn)*pow(10, deci)+0.5)/pow(10, deci);
4228 } else {
4229 V_R4(pVarOut)=ceil(V_R4(pVarIn)*pow(10, deci)-0.5)/pow(10, deci);
4231 V_VT(pVarOut) = V_VT(pVarIn);
4232 break;
4233 case VT_DATE:
4234 if (V_DATE(pVarIn)>0) {
4235 V_DATE(pVarOut)=floor(V_DATE(pVarIn)*pow(10, deci)+0.5)/pow(10, deci);
4236 } else {
4237 V_DATE(pVarOut)=ceil(V_DATE(pVarIn)*pow(10, deci)-0.5)/pow(10, deci);
4239 V_VT(pVarOut) = V_VT(pVarIn);
4240 break;
4241 case VT_CY:
4242 if (deci>3)
4243 factor=1;
4244 else
4245 factor=pow(10, 4-deci);
4247 if (V_CY(pVarIn).int64>0) {
4248 V_CY(pVarOut).int64=floor(V_CY(pVarIn).int64/factor)*factor;
4249 } else {
4250 V_CY(pVarOut).int64=ceil(V_CY(pVarIn).int64/factor)*factor;
4252 V_VT(pVarOut) = V_VT(pVarIn);
4253 break;
4255 /* cases we don't know yet */
4256 default:
4257 FIXME("unimplemented part, V_VT(pVarIn) == 0x%X, deci == %d\n",
4258 V_VT(pVarIn) & VT_TYPEMASK, deci);
4259 hRet = DISP_E_BADVARTYPE;
4262 if (FAILED(hRet))
4263 V_VT(pVarOut) = VT_EMPTY;
4265 TRACE("returning 0x%08lx (%s%s),%f\n", hRet, debugstr_VT(pVarOut),
4266 debugstr_VF(pVarOut), (V_VT(pVarOut) == VT_R4) ? V_R4(pVarOut) :
4267 (V_VT(pVarOut) == VT_R8) ? V_R8(pVarOut) : 0);
4269 return hRet;
4272 /**********************************************************************
4273 * VarIdiv [OLEAUT32.153]
4275 * Converts input variants to integers and divides them.
4277 * PARAMS
4278 * left [I] Left hand variant
4279 * right [I] Right hand variant
4280 * result [O] Destination for quotient
4282 * RETURNS
4283 * Success: S_OK. result contains the quotient.
4284 * Failure: An HRESULT error code indicating the error.
4286 * NOTES
4287 * If either expression is null, null is returned, as per MSDN
4289 HRESULT WINAPI VarIdiv(LPVARIANT left, LPVARIANT right, LPVARIANT result)
4291 VARIANT lv, rv;
4292 HRESULT hr;
4294 VariantInit(&lv);
4295 VariantInit(&rv);
4297 if ((V_VT(left) == VT_NULL) || (V_VT(right) == VT_NULL)) {
4298 hr = VariantChangeType(result, result, 0, VT_NULL);
4299 if (FAILED(hr)) {
4300 /* This should never happen */
4301 FIXME("Failed to convert return value to VT_NULL.\n");
4302 return hr;
4304 return S_OK;
4307 hr = VariantChangeType(&lv, left, 0, VT_I4);
4308 if (FAILED(hr)) {
4309 return hr;
4311 hr = VariantChangeType(&rv, right, 0, VT_I4);
4312 if (FAILED(hr)) {
4313 return hr;
4316 hr = VarDiv(&lv, &rv, result);
4317 return hr;
4321 /**********************************************************************
4322 * VarMod [OLEAUT32.155]
4324 * Perform the modulus operation of the right hand variant on the left
4326 * PARAMS
4327 * left [I] Left hand variant
4328 * right [I] Right hand variant
4329 * result [O] Destination for converted value
4331 * RETURNS
4332 * Success: S_OK. result contains the remainder.
4333 * Failure: An HRESULT error code indicating the error.
4335 * NOTE:
4336 * If an error occurs the type of result will be modified but the value will not be.
4337 * Doesn't support arrays or any special flags yet.
4339 HRESULT WINAPI VarMod(LPVARIANT left, LPVARIANT right, LPVARIANT result)
4341 BOOL lOk = TRUE;
4342 BOOL rOk = TRUE;
4343 HRESULT rc = E_FAIL;
4344 int resT = 0;
4345 VARIANT lv,rv;
4347 VariantInit(&lv);
4348 VariantInit(&rv);
4350 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
4351 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
4353 /* check for invalid inputs */
4354 lOk = TRUE;
4355 switch (V_VT(left) & VT_TYPEMASK) {
4356 case VT_BOOL :
4357 case VT_I1 :
4358 case VT_I2 :
4359 case VT_I4 :
4360 case VT_I8 :
4361 case VT_INT :
4362 case VT_UI1 :
4363 case VT_UI2 :
4364 case VT_UI4 :
4365 case VT_UI8 :
4366 case VT_UINT :
4367 case VT_R4 :
4368 case VT_R8 :
4369 case VT_CY :
4370 case VT_EMPTY:
4371 case VT_DATE :
4372 case VT_BSTR :
4373 break;
4374 case VT_VARIANT:
4375 case VT_UNKNOWN:
4376 V_VT(result) = VT_EMPTY;
4377 return DISP_E_TYPEMISMATCH;
4378 case VT_DECIMAL:
4379 V_VT(result) = VT_EMPTY;
4380 return DISP_E_OVERFLOW;
4381 case VT_ERROR:
4382 return DISP_E_TYPEMISMATCH;
4383 case VT_RECORD:
4384 V_VT(result) = VT_EMPTY;
4385 return DISP_E_TYPEMISMATCH;
4386 case VT_NULL:
4387 break;
4388 default:
4389 V_VT(result) = VT_EMPTY;
4390 return DISP_E_BADVARTYPE;
4394 rOk = TRUE;
4395 switch (V_VT(right) & VT_TYPEMASK) {
4396 case VT_BOOL :
4397 case VT_I1 :
4398 case VT_I2 :
4399 case VT_I4 :
4400 case VT_I8 :
4401 if((V_VT(left) == VT_INT) && (V_VT(right) == VT_I8))
4403 V_VT(result) = VT_EMPTY;
4404 return DISP_E_TYPEMISMATCH;
4406 case VT_INT :
4407 if((V_VT(right) == VT_INT) && (V_VT(left) == VT_I8))
4409 V_VT(result) = VT_EMPTY;
4410 return DISP_E_TYPEMISMATCH;
4412 case VT_UI1 :
4413 case VT_UI2 :
4414 case VT_UI4 :
4415 case VT_UI8 :
4416 case VT_UINT :
4417 case VT_R4 :
4418 case VT_R8 :
4419 case VT_CY :
4420 if(V_VT(left) == VT_EMPTY)
4422 V_VT(result) = VT_I4;
4423 return S_OK;
4425 case VT_EMPTY:
4426 case VT_DATE :
4427 case VT_BSTR:
4428 if(V_VT(left) == VT_NULL)
4430 V_VT(result) = VT_NULL;
4431 return S_OK;
4433 break;
4435 case VT_VOID:
4436 V_VT(result) = VT_EMPTY;
4437 return DISP_E_BADVARTYPE;
4438 case VT_NULL:
4439 if(V_VT(left) == VT_VOID)
4441 V_VT(result) = VT_EMPTY;
4442 return DISP_E_BADVARTYPE;
4443 } else if((V_VT(left) == VT_NULL) || (V_VT(left) == VT_EMPTY) || (V_VT(left) == VT_ERROR) ||
4444 lOk)
4446 V_VT(result) = VT_NULL;
4447 return S_OK;
4448 } else
4450 V_VT(result) = VT_NULL;
4451 return DISP_E_BADVARTYPE;
4453 case VT_VARIANT:
4454 case VT_UNKNOWN:
4455 V_VT(result) = VT_EMPTY;
4456 return DISP_E_TYPEMISMATCH;
4457 case VT_DECIMAL:
4458 if(V_VT(left) == VT_ERROR)
4460 V_VT(result) = VT_EMPTY;
4461 return DISP_E_TYPEMISMATCH;
4462 } else
4464 V_VT(result) = VT_EMPTY;
4465 return DISP_E_OVERFLOW;
4467 case VT_ERROR:
4468 return DISP_E_TYPEMISMATCH;
4469 case VT_RECORD:
4470 if((V_VT(left) == 15) || ((V_VT(left) >= 24) && (V_VT(left) <= 35)) || !lOk)
4472 V_VT(result) = VT_EMPTY;
4473 return DISP_E_BADVARTYPE;
4474 } else
4476 V_VT(result) = VT_EMPTY;
4477 return DISP_E_TYPEMISMATCH;
4479 default:
4480 V_VT(result) = VT_EMPTY;
4481 return DISP_E_BADVARTYPE;
4484 /* determine the result type */
4485 if((V_VT(left) == VT_I8) || (V_VT(right) == VT_I8)) resT = VT_I8;
4486 else if((V_VT(left) == VT_UI1) && (V_VT(right) == VT_BOOL)) resT = VT_I2;
4487 else if((V_VT(left) == VT_UI1) && (V_VT(right) == VT_UI1)) resT = VT_UI1;
4488 else if((V_VT(left) == VT_UI1) && (V_VT(right) == VT_I2)) resT = VT_I2;
4489 else if((V_VT(left) == VT_I2) && (V_VT(right) == VT_BOOL)) resT = VT_I2;
4490 else if((V_VT(left) == VT_I2) && (V_VT(right) == VT_UI1)) resT = VT_I2;
4491 else if((V_VT(left) == VT_I2) && (V_VT(right) == VT_I2)) resT = VT_I2;
4492 else if((V_VT(left) == VT_BOOL) && (V_VT(right) == VT_BOOL)) resT = VT_I2;
4493 else if((V_VT(left) == VT_BOOL) && (V_VT(right) == VT_UI1)) resT = VT_I2;
4494 else if((V_VT(left) == VT_BOOL) && (V_VT(right) == VT_I2)) resT = VT_I2;
4495 else resT = VT_I4; /* most outputs are I4 */
4497 /* convert to I8 for the modulo */
4498 rc = VariantChangeType(&lv, left, 0, VT_I8);
4499 if(FAILED(rc))
4501 FIXME("Could not convert left type %d to %d? rc == 0x%lX\n", V_VT(left), VT_I8, rc);
4502 return rc;
4505 rc = VariantChangeType(&rv, right, 0, VT_I8);
4506 if(FAILED(rc))
4508 FIXME("Could not convert right type %d to %d? rc == 0x%lX\n", V_VT(right), VT_I8, rc);
4509 return rc;
4512 /* if right is zero set VT_EMPTY and return divide by zero */
4513 if(V_I8(&rv) == 0)
4515 V_VT(result) = VT_EMPTY;
4516 return DISP_E_DIVBYZERO;
4519 /* perform the modulo operation */
4520 V_VT(result) = VT_I8;
4521 V_I8(result) = V_I8(&lv) % V_I8(&rv);
4523 TRACE("V_I8(left) == %ld, V_I8(right) == %ld, V_I8(result) == %ld\n", (long)V_I8(&lv), (long)V_I8(&rv), (long)V_I8(result));
4525 /* convert left and right to the destination type */
4526 rc = VariantChangeType(result, result, 0, resT);
4527 if(FAILED(rc))
4529 FIXME("Could not convert 0x%x to %d?\n", V_VT(result), resT);
4530 return rc;
4533 return S_OK;
4536 /**********************************************************************
4537 * VarPow [OLEAUT32.158]
4540 HRESULT WINAPI VarPow(LPVARIANT left, LPVARIANT right, LPVARIANT result)
4542 HRESULT hr;
4543 VARIANT dl,dr;
4545 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left), debugstr_VF(left),
4546 right, debugstr_VT(right), debugstr_VF(right), result);
4548 hr = VariantChangeType(&dl,left,0,VT_R8);
4549 if (!SUCCEEDED(hr)) {
4550 ERR("Could not change passed left argument to VT_R8, handle it differently.\n");
4551 return E_FAIL;
4553 hr = VariantChangeType(&dr,right,0,VT_R8);
4554 if (!SUCCEEDED(hr)) {
4555 ERR("Could not change passed right argument to VT_R8, handle it differently.\n");
4556 return E_FAIL;
4558 V_VT(result) = VT_R8;
4559 V_R8(result) = pow(V_R8(&dl),V_R8(&dr));
4560 return S_OK;