2 * Low level variant functions
4 * Copyright 2003 Jon Griffiths
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 WINE_DEFAULT_DEBUG_CHANNEL(variant
);
25 extern HMODULE hProxyDll DECLSPEC_HIDDEN
;
27 #define CY_MULTIPLIER 10000 /* 4 dp of precision */
28 #define CY_MULTIPLIER_F 10000.0
29 #define CY_HALF (CY_MULTIPLIER/2) /* 0.5 */
30 #define CY_HALF_F (CY_MULTIPLIER_F/2.0)
32 static const WCHAR szFloatFormatW
[] = { '%','.','7','G','\0' };
33 static const WCHAR szDoubleFormatW
[] = { '%','.','1','5','G','\0' };
35 /* Copy data from one variant to another. */
36 static inline void VARIANT_CopyData(const VARIANT
*srcVar
, VARTYPE vt
, void *pOut
)
41 case VT_UI1
: memcpy(pOut
, &V_UI1(srcVar
), sizeof(BYTE
)); break;
44 case VT_UI2
: memcpy(pOut
, &V_UI2(srcVar
), sizeof(SHORT
)); break;
49 case VT_UI4
: memcpy(pOut
, &V_UI4(srcVar
), sizeof (LONG
)); break;
54 case VT_UI8
: memcpy(pOut
, &V_UI8(srcVar
), sizeof (LONG64
)); break;
55 case VT_INT_PTR
: memcpy(pOut
, &V_INT_PTR(srcVar
), sizeof (INT_PTR
)); break;
56 case VT_DECIMAL
: memcpy(pOut
, &V_DECIMAL(srcVar
), sizeof (DECIMAL
)); break;
57 case VT_BSTR
: memcpy(pOut
, &V_BSTR(srcVar
), sizeof(BSTR
)); break;
59 FIXME("VT_ type %d unhandled, please report!\n", vt
);
63 /* Macro to inline conversion from a float or double to any integer type,
64 * rounding according to the 'dutch' convention.
66 #define VARIANT_DutchRound(typ, value, res) do { \
67 double whole = value < 0 ? ceil(value) : floor(value); \
68 double fract = value - whole; \
69 if (fract > 0.5) res = (typ)whole + (typ)1; \
70 else if (fract == 0.5) { typ is_odd = (typ)whole & 1; res = whole + is_odd; } \
71 else if (fract >= 0.0) res = (typ)whole; \
72 else if (fract == -0.5) { typ is_odd = (typ)whole & 1; res = whole - is_odd; } \
73 else if (fract > -0.5) res = (typ)whole; \
74 else res = (typ)whole - (typ)1; \
78 /* Coerce VT_BSTR to a numeric type */
79 static HRESULT
VARIANT_NumberFromBstr(OLECHAR
* pStrIn
, LCID lcid
, ULONG ulFlags
,
80 void* pOut
, VARTYPE vt
)
87 /* Use VarParseNumFromStr/VarNumFromParseNum as MSDN indicates */
88 np
.cDig
= sizeof(rgb
) / sizeof(BYTE
);
89 np
.dwInFlags
= NUMPRS_STD
;
91 hRet
= VarParseNumFromStr(pStrIn
, lcid
, ulFlags
, &np
, rgb
);
95 /* 1 << vt gives us the VTBIT constant for the destination number type */
96 hRet
= VarNumFromParseNum(&np
, rgb
, 1 << vt
, &dstVar
);
98 VARIANT_CopyData(&dstVar
, vt
, pOut
);
103 /* Coerce VT_DISPATCH to another type */
104 static HRESULT
VARIANT_FromDisp(IDispatch
* pdispIn
, LCID lcid
, void* pOut
,
105 VARTYPE vt
, DWORD dwFlags
)
107 static DISPPARAMS emptyParams
= { NULL
, NULL
, 0, 0 };
108 VARIANTARG srcVar
, dstVar
;
112 return DISP_E_BADVARTYPE
;
114 /* Get the default 'value' property from the IDispatch */
115 VariantInit(&srcVar
);
116 hRet
= IDispatch_Invoke(pdispIn
, DISPID_VALUE
, &IID_NULL
, lcid
, DISPATCH_PROPERTYGET
,
117 &emptyParams
, &srcVar
, NULL
, NULL
);
121 /* Convert the property to the requested type */
122 V_VT(&dstVar
) = VT_EMPTY
;
123 hRet
= VariantChangeTypeEx(&dstVar
, &srcVar
, lcid
, dwFlags
, vt
);
124 VariantClear(&srcVar
);
128 VARIANT_CopyData(&dstVar
, vt
, pOut
);
129 VariantClear(&srcVar
);
133 hRet
= DISP_E_TYPEMISMATCH
;
137 /* Inline return type */
138 #define RETTYP static inline HRESULT
141 /* Simple compiler cast from one type to another */
142 #define SIMPLE(dest, src, func) RETTYP _##func(src in, dest* out) { \
143 *out = in; return S_OK; }
145 /* Compiler cast where input cannot be negative */
146 #define NEGTST(dest, src, func) RETTYP _##func(src in, dest* out) { \
147 if (in < 0) return DISP_E_OVERFLOW; *out = in; return S_OK; }
149 /* Compiler cast where input cannot be > some number */
150 #define POSTST(dest, src, func, tst) RETTYP _##func(src in, dest* out) { \
151 if (in > (dest)tst) return DISP_E_OVERFLOW; *out = in; return S_OK; }
153 /* Compiler cast where input cannot be < some number or >= some other number */
154 #define BOTHTST(dest, src, func, lo, hi) RETTYP _##func(src in, dest* out) { \
155 if (in < (dest)lo || in > hi) return DISP_E_OVERFLOW; *out = in; return S_OK; }
158 POSTST(signed char, BYTE
, VarI1FromUI1
, I1_MAX
)
159 BOTHTST(signed char, SHORT
, VarI1FromI2
, I1_MIN
, I1_MAX
)
160 BOTHTST(signed char, LONG
, VarI1FromI4
, I1_MIN
, I1_MAX
)
161 SIMPLE(signed char, VARIANT_BOOL
, VarI1FromBool
)
162 POSTST(signed char, USHORT
, VarI1FromUI2
, I1_MAX
)
163 POSTST(signed char, ULONG
, VarI1FromUI4
, I1_MAX
)
164 BOTHTST(signed char, LONG64
, VarI1FromI8
, I1_MIN
, I1_MAX
)
165 POSTST(signed char, ULONG64
, VarI1FromUI8
, I1_MAX
)
168 BOTHTST(BYTE
, SHORT
, VarUI1FromI2
, UI1_MIN
, UI1_MAX
)
169 SIMPLE(BYTE
, VARIANT_BOOL
, VarUI1FromBool
)
170 NEGTST(BYTE
, signed char, VarUI1FromI1
)
171 POSTST(BYTE
, USHORT
, VarUI1FromUI2
, UI1_MAX
)
172 BOTHTST(BYTE
, LONG
, VarUI1FromI4
, UI1_MIN
, UI1_MAX
)
173 POSTST(BYTE
, ULONG
, VarUI1FromUI4
, UI1_MAX
)
174 BOTHTST(BYTE
, LONG64
, VarUI1FromI8
, UI1_MIN
, UI1_MAX
)
175 POSTST(BYTE
, ULONG64
, VarUI1FromUI8
, UI1_MAX
)
178 SIMPLE(SHORT
, BYTE
, VarI2FromUI1
)
179 BOTHTST(SHORT
, LONG
, VarI2FromI4
, I2_MIN
, I2_MAX
)
180 SIMPLE(SHORT
, VARIANT_BOOL
, VarI2FromBool
)
181 SIMPLE(SHORT
, signed char, VarI2FromI1
)
182 POSTST(SHORT
, USHORT
, VarI2FromUI2
, I2_MAX
)
183 POSTST(SHORT
, ULONG
, VarI2FromUI4
, I2_MAX
)
184 BOTHTST(SHORT
, LONG64
, VarI2FromI8
, I2_MIN
, I2_MAX
)
185 POSTST(SHORT
, ULONG64
, VarI2FromUI8
, I2_MAX
)
188 SIMPLE(USHORT
, BYTE
, VarUI2FromUI1
)
189 NEGTST(USHORT
, SHORT
, VarUI2FromI2
)
190 BOTHTST(USHORT
, LONG
, VarUI2FromI4
, UI2_MIN
, UI2_MAX
)
191 SIMPLE(USHORT
, VARIANT_BOOL
, VarUI2FromBool
)
192 NEGTST(USHORT
, signed char, VarUI2FromI1
)
193 POSTST(USHORT
, ULONG
, VarUI2FromUI4
, UI2_MAX
)
194 BOTHTST(USHORT
, LONG64
, VarUI2FromI8
, UI2_MIN
, UI2_MAX
)
195 POSTST(USHORT
, ULONG64
, VarUI2FromUI8
, UI2_MAX
)
198 SIMPLE(LONG
, BYTE
, VarI4FromUI1
)
199 SIMPLE(LONG
, SHORT
, VarI4FromI2
)
200 SIMPLE(LONG
, VARIANT_BOOL
, VarI4FromBool
)
201 SIMPLE(LONG
, signed char, VarI4FromI1
)
202 SIMPLE(LONG
, USHORT
, VarI4FromUI2
)
203 POSTST(LONG
, ULONG
, VarI4FromUI4
, I4_MAX
)
204 BOTHTST(LONG
, LONG64
, VarI4FromI8
, I4_MIN
, I4_MAX
)
205 POSTST(LONG
, ULONG64
, VarI4FromUI8
, I4_MAX
)
208 SIMPLE(ULONG
, BYTE
, VarUI4FromUI1
)
209 NEGTST(ULONG
, SHORT
, VarUI4FromI2
)
210 NEGTST(ULONG
, LONG
, VarUI4FromI4
)
211 SIMPLE(ULONG
, VARIANT_BOOL
, VarUI4FromBool
)
212 NEGTST(ULONG
, signed char, VarUI4FromI1
)
213 SIMPLE(ULONG
, USHORT
, VarUI4FromUI2
)
214 BOTHTST(ULONG
, LONG64
, VarUI4FromI8
, UI4_MIN
, UI4_MAX
)
215 POSTST(ULONG
, ULONG64
, VarUI4FromUI8
, UI4_MAX
)
218 SIMPLE(LONG64
, BYTE
, VarI8FromUI1
)
219 SIMPLE(LONG64
, SHORT
, VarI8FromI2
)
220 SIMPLE(LONG64
, signed char, VarI8FromI1
)
221 SIMPLE(LONG64
, USHORT
, VarI8FromUI2
)
222 SIMPLE(LONG64
, ULONG
, VarI8FromUI4
)
223 POSTST(LONG64
, ULONG64
, VarI8FromUI8
, I8_MAX
)
226 SIMPLE(ULONG64
, BYTE
, VarUI8FromUI1
)
227 NEGTST(ULONG64
, SHORT
, VarUI8FromI2
)
228 NEGTST(ULONG64
, signed char, VarUI8FromI1
)
229 SIMPLE(ULONG64
, USHORT
, VarUI8FromUI2
)
230 SIMPLE(ULONG64
, ULONG
, VarUI8FromUI4
)
231 NEGTST(ULONG64
, LONG64
, VarUI8FromI8
)
234 SIMPLE(float, BYTE
, VarR4FromUI1
)
235 SIMPLE(float, SHORT
, VarR4FromI2
)
236 SIMPLE(float, signed char, VarR4FromI1
)
237 SIMPLE(float, USHORT
, VarR4FromUI2
)
238 SIMPLE(float, LONG
, VarR4FromI4
)
239 SIMPLE(float, ULONG
, VarR4FromUI4
)
240 SIMPLE(float, LONG64
, VarR4FromI8
)
241 SIMPLE(float, ULONG64
, VarR4FromUI8
)
244 SIMPLE(double, BYTE
, VarR8FromUI1
)
245 SIMPLE(double, SHORT
, VarR8FromI2
)
246 SIMPLE(double, float, VarR8FromR4
)
247 RETTYP
_VarR8FromCy(CY i
, double* o
) { *o
= (double)i
.int64
/ CY_MULTIPLIER_F
; return S_OK
; }
248 SIMPLE(double, DATE
, VarR8FromDate
)
249 SIMPLE(double, signed char, VarR8FromI1
)
250 SIMPLE(double, USHORT
, VarR8FromUI2
)
251 SIMPLE(double, LONG
, VarR8FromI4
)
252 SIMPLE(double, ULONG
, VarR8FromUI4
)
253 SIMPLE(double, LONG64
, VarR8FromI8
)
254 SIMPLE(double, ULONG64
, VarR8FromUI8
)
260 /************************************************************************
261 * VarI1FromUI1 (OLEAUT32.244)
263 * Convert a VT_UI1 to a VT_I1.
267 * pcOut [O] Destination
271 * Failure: E_INVALIDARG, if the source value is invalid
272 * DISP_E_OVERFLOW, if the value will not fit in the destination
274 HRESULT WINAPI
VarI1FromUI1(BYTE bIn
, signed char* pcOut
)
276 return _VarI1FromUI1(bIn
, pcOut
);
279 /************************************************************************
280 * VarI1FromI2 (OLEAUT32.245)
282 * Convert a VT_I2 to a VT_I1.
286 * pcOut [O] Destination
290 * Failure: E_INVALIDARG, if the source value is invalid
291 * DISP_E_OVERFLOW, if the value will not fit in the destination
293 HRESULT WINAPI
VarI1FromI2(SHORT sIn
, signed char* pcOut
)
295 return _VarI1FromI2(sIn
, pcOut
);
298 /************************************************************************
299 * VarI1FromI4 (OLEAUT32.246)
301 * Convert a VT_I4 to a VT_I1.
305 * pcOut [O] Destination
309 * Failure: E_INVALIDARG, if the source value is invalid
310 * DISP_E_OVERFLOW, if the value will not fit in the destination
312 HRESULT WINAPI
VarI1FromI4(LONG iIn
, signed char* pcOut
)
314 return _VarI1FromI4(iIn
, pcOut
);
317 /************************************************************************
318 * VarI1FromR4 (OLEAUT32.247)
320 * Convert a VT_R4 to a VT_I1.
324 * pcOut [O] Destination
328 * Failure: E_INVALIDARG, if the source value is invalid
329 * DISP_E_OVERFLOW, if the value will not fit in the destination
331 HRESULT WINAPI
VarI1FromR4(FLOAT fltIn
, signed char* pcOut
)
333 return VarI1FromR8(fltIn
, pcOut
);
336 /************************************************************************
337 * VarI1FromR8 (OLEAUT32.248)
339 * Convert a VT_R8 to a VT_I1.
343 * pcOut [O] Destination
347 * Failure: E_INVALIDARG, if the source value is invalid
348 * DISP_E_OVERFLOW, if the value will not fit in the destination
351 * See VarI8FromR8() for details concerning rounding.
353 HRESULT WINAPI
VarI1FromR8(double dblIn
, signed char* pcOut
)
355 if (dblIn
< I1_MIN
- 0.5 || dblIn
>= I1_MAX
+ 0.5)
356 return DISP_E_OVERFLOW
;
357 VARIANT_DutchRound(CHAR
, dblIn
, *pcOut
);
361 /************************************************************************
362 * VarI1FromDate (OLEAUT32.249)
364 * Convert a VT_DATE to a VT_I1.
368 * pcOut [O] Destination
372 * Failure: E_INVALIDARG, if the source value is invalid
373 * DISP_E_OVERFLOW, if the value will not fit in the destination
375 HRESULT WINAPI
VarI1FromDate(DATE dateIn
, signed char* pcOut
)
377 return VarI1FromR8(dateIn
, pcOut
);
380 /************************************************************************
381 * VarI1FromCy (OLEAUT32.250)
383 * Convert a VT_CY to a VT_I1.
387 * pcOut [O] Destination
391 * Failure: E_INVALIDARG, if the source value is invalid
392 * DISP_E_OVERFLOW, if the value will not fit in the destination
394 HRESULT WINAPI
VarI1FromCy(CY cyIn
, signed char* pcOut
)
398 VarI4FromCy(cyIn
, &i
);
399 return _VarI1FromI4(i
, pcOut
);
402 /************************************************************************
403 * VarI1FromStr (OLEAUT32.251)
405 * Convert a VT_BSTR to a VT_I1.
409 * lcid [I] LCID for the conversion
410 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
411 * pcOut [O] Destination
415 * Failure: E_INVALIDARG, if the source value is invalid
416 * DISP_E_OVERFLOW, if the value will not fit in the destination
417 * DISP_E_TYPEMISMATCH, if the type cannot be converted
419 HRESULT WINAPI
VarI1FromStr(OLECHAR
* strIn
, LCID lcid
, ULONG dwFlags
, signed char* pcOut
)
421 return VARIANT_NumberFromBstr(strIn
, lcid
, dwFlags
, pcOut
, VT_I1
);
424 /************************************************************************
425 * VarI1FromDisp (OLEAUT32.252)
427 * Convert a VT_DISPATCH to a VT_I1.
431 * lcid [I] LCID for conversion
432 * pcOut [O] Destination
436 * Failure: E_INVALIDARG, if the source value is invalid
437 * DISP_E_OVERFLOW, if the value will not fit in the destination
438 * DISP_E_TYPEMISMATCH, if the type cannot be converted
440 HRESULT WINAPI
VarI1FromDisp(IDispatch
* pdispIn
, LCID lcid
, signed char* pcOut
)
442 return VARIANT_FromDisp(pdispIn
, lcid
, pcOut
, VT_I1
, 0);
445 /************************************************************************
446 * VarI1FromBool (OLEAUT32.253)
448 * Convert a VT_BOOL to a VT_I1.
452 * pcOut [O] Destination
457 HRESULT WINAPI
VarI1FromBool(VARIANT_BOOL boolIn
, signed char* pcOut
)
459 return _VarI1FromBool(boolIn
, pcOut
);
462 /************************************************************************
463 * VarI1FromUI2 (OLEAUT32.254)
465 * Convert a VT_UI2 to a VT_I1.
469 * pcOut [O] Destination
473 * Failure: E_INVALIDARG, if the source value is invalid
474 * DISP_E_OVERFLOW, if the value will not fit in the destination
476 HRESULT WINAPI
VarI1FromUI2(USHORT usIn
, signed char* pcOut
)
478 return _VarI1FromUI2(usIn
, pcOut
);
481 /************************************************************************
482 * VarI1FromUI4 (OLEAUT32.255)
484 * Convert a VT_UI4 to a VT_I1.
488 * pcOut [O] Destination
492 * Failure: E_INVALIDARG, if the source value is invalid
493 * DISP_E_OVERFLOW, if the value will not fit in the destination
494 * DISP_E_TYPEMISMATCH, if the type cannot be converted
496 HRESULT WINAPI
VarI1FromUI4(ULONG ulIn
, signed char* pcOut
)
498 return _VarI1FromUI4(ulIn
, pcOut
);
501 /************************************************************************
502 * VarI1FromDec (OLEAUT32.256)
504 * Convert a VT_DECIMAL to a VT_I1.
508 * pcOut [O] Destination
512 * Failure: E_INVALIDARG, if the source value is invalid
513 * DISP_E_OVERFLOW, if the value will not fit in the destination
515 HRESULT WINAPI
VarI1FromDec(DECIMAL
*pdecIn
, signed char* pcOut
)
520 hRet
= VarI8FromDec(pdecIn
, &i64
);
523 hRet
= _VarI1FromI8(i64
, pcOut
);
527 /************************************************************************
528 * VarI1FromI8 (OLEAUT32.376)
530 * Convert a VT_I8 to a VT_I1.
534 * pcOut [O] Destination
538 * Failure: E_INVALIDARG, if the source value is invalid
539 * DISP_E_OVERFLOW, if the value will not fit in the destination
541 HRESULT WINAPI
VarI1FromI8(LONG64 llIn
, signed char* pcOut
)
543 return _VarI1FromI8(llIn
, pcOut
);
546 /************************************************************************
547 * VarI1FromUI8 (OLEAUT32.377)
549 * Convert a VT_UI8 to a VT_I1.
553 * pcOut [O] Destination
557 * Failure: E_INVALIDARG, if the source value is invalid
558 * DISP_E_OVERFLOW, if the value will not fit in the destination
560 HRESULT WINAPI
VarI1FromUI8(ULONG64 ullIn
, signed char* pcOut
)
562 return _VarI1FromUI8(ullIn
, pcOut
);
568 /************************************************************************
569 * VarUI1FromI2 (OLEAUT32.130)
571 * Convert a VT_I2 to a VT_UI1.
575 * pbOut [O] Destination
579 * Failure: E_INVALIDARG, if the source value is invalid
580 * DISP_E_OVERFLOW, if the value will not fit in the destination
582 HRESULT WINAPI
VarUI1FromI2(SHORT sIn
, BYTE
* pbOut
)
584 return _VarUI1FromI2(sIn
, pbOut
);
587 /************************************************************************
588 * VarUI1FromI4 (OLEAUT32.131)
590 * Convert a VT_I4 to a VT_UI1.
594 * pbOut [O] Destination
598 * Failure: E_INVALIDARG, if the source value is invalid
599 * DISP_E_OVERFLOW, if the value will not fit in the destination
601 HRESULT WINAPI
VarUI1FromI4(LONG iIn
, BYTE
* pbOut
)
603 return _VarUI1FromI4(iIn
, pbOut
);
606 /************************************************************************
607 * VarUI1FromR4 (OLEAUT32.132)
609 * Convert a VT_R4 to a VT_UI1.
613 * pbOut [O] Destination
617 * Failure: E_INVALIDARG, if the source value is invalid
618 * DISP_E_OVERFLOW, if the value will not fit in the destination
619 * DISP_E_TYPEMISMATCH, if the type cannot be converted
621 HRESULT WINAPI
VarUI1FromR4(FLOAT fltIn
, BYTE
* pbOut
)
623 return VarUI1FromR8(fltIn
, pbOut
);
626 /************************************************************************
627 * VarUI1FromR8 (OLEAUT32.133)
629 * Convert a VT_R8 to a VT_UI1.
633 * pbOut [O] Destination
637 * Failure: E_INVALIDARG, if the source value is invalid
638 * DISP_E_OVERFLOW, if the value will not fit in the destination
641 * See VarI8FromR8() for details concerning rounding.
643 HRESULT WINAPI
VarUI1FromR8(double dblIn
, BYTE
* pbOut
)
645 if (dblIn
< -0.5 || dblIn
>= UI1_MAX
+ 0.5)
646 return DISP_E_OVERFLOW
;
647 VARIANT_DutchRound(BYTE
, dblIn
, *pbOut
);
651 /************************************************************************
652 * VarUI1FromCy (OLEAUT32.134)
654 * Convert a VT_CY to a VT_UI1.
658 * pbOut [O] Destination
662 * Failure: E_INVALIDARG, if the source value is invalid
663 * DISP_E_OVERFLOW, if the value will not fit in the destination
666 * Negative values >= -5000 will be converted to 0.
668 HRESULT WINAPI
VarUI1FromCy(CY cyIn
, BYTE
* pbOut
)
670 ULONG i
= UI1_MAX
+ 1;
672 VarUI4FromCy(cyIn
, &i
);
673 return _VarUI1FromUI4(i
, pbOut
);
676 /************************************************************************
677 * VarUI1FromDate (OLEAUT32.135)
679 * Convert a VT_DATE to a VT_UI1.
683 * pbOut [O] Destination
687 * Failure: E_INVALIDARG, if the source value is invalid
688 * DISP_E_OVERFLOW, if the value will not fit in the destination
690 HRESULT WINAPI
VarUI1FromDate(DATE dateIn
, BYTE
* pbOut
)
692 return VarUI1FromR8(dateIn
, pbOut
);
695 /************************************************************************
696 * VarUI1FromStr (OLEAUT32.136)
698 * Convert a VT_BSTR to a VT_UI1.
702 * lcid [I] LCID for the conversion
703 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
704 * pbOut [O] Destination
708 * Failure: E_INVALIDARG, if the source value is invalid
709 * DISP_E_OVERFLOW, if the value will not fit in the destination
710 * DISP_E_TYPEMISMATCH, if the type cannot be converted
712 HRESULT WINAPI
VarUI1FromStr(OLECHAR
* strIn
, LCID lcid
, ULONG dwFlags
, BYTE
* pbOut
)
714 return VARIANT_NumberFromBstr(strIn
, lcid
, dwFlags
, pbOut
, VT_UI1
);
717 /************************************************************************
718 * VarUI1FromDisp (OLEAUT32.137)
720 * Convert a VT_DISPATCH to a VT_UI1.
724 * lcid [I] LCID for conversion
725 * pbOut [O] Destination
729 * Failure: E_INVALIDARG, if the source value is invalid
730 * DISP_E_OVERFLOW, if the value will not fit in the destination
731 * DISP_E_TYPEMISMATCH, if the type cannot be converted
733 HRESULT WINAPI
VarUI1FromDisp(IDispatch
* pdispIn
, LCID lcid
, BYTE
* pbOut
)
735 return VARIANT_FromDisp(pdispIn
, lcid
, pbOut
, VT_UI1
, 0);
738 /************************************************************************
739 * VarUI1FromBool (OLEAUT32.138)
741 * Convert a VT_BOOL to a VT_UI1.
745 * pbOut [O] Destination
750 HRESULT WINAPI
VarUI1FromBool(VARIANT_BOOL boolIn
, BYTE
* pbOut
)
752 return _VarUI1FromBool(boolIn
, pbOut
);
755 /************************************************************************
756 * VarUI1FromI1 (OLEAUT32.237)
758 * Convert a VT_I1 to a VT_UI1.
762 * pbOut [O] Destination
766 * Failure: E_INVALIDARG, if the source value is invalid
767 * DISP_E_OVERFLOW, if the value will not fit in the destination
769 HRESULT WINAPI
VarUI1FromI1(signed char cIn
, BYTE
* pbOut
)
771 return _VarUI1FromI1(cIn
, pbOut
);
774 /************************************************************************
775 * VarUI1FromUI2 (OLEAUT32.238)
777 * Convert a VT_UI2 to a VT_UI1.
781 * pbOut [O] Destination
785 * Failure: E_INVALIDARG, if the source value is invalid
786 * DISP_E_OVERFLOW, if the value will not fit in the destination
788 HRESULT WINAPI
VarUI1FromUI2(USHORT usIn
, BYTE
* pbOut
)
790 return _VarUI1FromUI2(usIn
, pbOut
);
793 /************************************************************************
794 * VarUI1FromUI4 (OLEAUT32.239)
796 * Convert a VT_UI4 to a VT_UI1.
800 * pbOut [O] Destination
804 * Failure: E_INVALIDARG, if the source value is invalid
805 * DISP_E_OVERFLOW, if the value will not fit in the destination
807 HRESULT WINAPI
VarUI1FromUI4(ULONG ulIn
, BYTE
* pbOut
)
809 return _VarUI1FromUI4(ulIn
, pbOut
);
812 /************************************************************************
813 * VarUI1FromDec (OLEAUT32.240)
815 * Convert a VT_DECIMAL to a VT_UI1.
819 * pbOut [O] Destination
823 * Failure: E_INVALIDARG, if the source value is invalid
824 * DISP_E_OVERFLOW, if the value will not fit in the destination
826 HRESULT WINAPI
VarUI1FromDec(DECIMAL
*pdecIn
, BYTE
* pbOut
)
831 hRet
= VarI8FromDec(pdecIn
, &i64
);
834 hRet
= _VarUI1FromI8(i64
, pbOut
);
838 /************************************************************************
839 * VarUI1FromI8 (OLEAUT32.372)
841 * Convert a VT_I8 to a VT_UI1.
845 * pbOut [O] Destination
849 * Failure: E_INVALIDARG, if the source value is invalid
850 * DISP_E_OVERFLOW, if the value will not fit in the destination
852 HRESULT WINAPI
VarUI1FromI8(LONG64 llIn
, BYTE
* pbOut
)
854 return _VarUI1FromI8(llIn
, pbOut
);
857 /************************************************************************
858 * VarUI1FromUI8 (OLEAUT32.373)
860 * Convert a VT_UI8 to a VT_UI1.
864 * pbOut [O] Destination
868 * Failure: E_INVALIDARG, if the source value is invalid
869 * DISP_E_OVERFLOW, if the value will not fit in the destination
871 HRESULT WINAPI
VarUI1FromUI8(ULONG64 ullIn
, BYTE
* pbOut
)
873 return _VarUI1FromUI8(ullIn
, pbOut
);
880 /************************************************************************
881 * VarI2FromUI1 (OLEAUT32.48)
883 * Convert a VT_UI2 to a VT_I2.
887 * psOut [O] Destination
892 HRESULT WINAPI
VarI2FromUI1(BYTE bIn
, SHORT
* psOut
)
894 return _VarI2FromUI1(bIn
, psOut
);
897 /************************************************************************
898 * VarI2FromI4 (OLEAUT32.49)
900 * Convert a VT_I4 to a VT_I2.
904 * psOut [O] Destination
908 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
910 HRESULT WINAPI
VarI2FromI4(LONG iIn
, SHORT
* psOut
)
912 return _VarI2FromI4(iIn
, psOut
);
915 /************************************************************************
916 * VarI2FromR4 (OLEAUT32.50)
918 * Convert a VT_R4 to a VT_I2.
922 * psOut [O] Destination
926 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
928 HRESULT WINAPI
VarI2FromR4(FLOAT fltIn
, SHORT
* psOut
)
930 return VarI2FromR8(fltIn
, psOut
);
933 /************************************************************************
934 * VarI2FromR8 (OLEAUT32.51)
936 * Convert a VT_R8 to a VT_I2.
940 * psOut [O] Destination
944 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
947 * See VarI8FromR8() for details concerning rounding.
949 HRESULT WINAPI
VarI2FromR8(double dblIn
, SHORT
* psOut
)
951 if (dblIn
< I2_MIN
- 0.5 || dblIn
>= I2_MAX
+ 0.5)
952 return DISP_E_OVERFLOW
;
953 VARIANT_DutchRound(SHORT
, dblIn
, *psOut
);
957 /************************************************************************
958 * VarI2FromCy (OLEAUT32.52)
960 * Convert a VT_CY to a VT_I2.
964 * psOut [O] Destination
968 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
970 HRESULT WINAPI
VarI2FromCy(CY cyIn
, SHORT
* psOut
)
974 VarI4FromCy(cyIn
, &i
);
975 return _VarI2FromI4(i
, psOut
);
978 /************************************************************************
979 * VarI2FromDate (OLEAUT32.53)
981 * Convert a VT_DATE to a VT_I2.
985 * psOut [O] Destination
989 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
991 HRESULT WINAPI
VarI2FromDate(DATE dateIn
, SHORT
* psOut
)
993 return VarI2FromR8(dateIn
, psOut
);
996 /************************************************************************
997 * VarI2FromStr (OLEAUT32.54)
999 * Convert a VT_BSTR to a VT_I2.
1003 * lcid [I] LCID for the conversion
1004 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1005 * psOut [O] Destination
1009 * Failure: E_INVALIDARG, if any parameter is invalid
1010 * DISP_E_OVERFLOW, if the value will not fit in the destination
1011 * DISP_E_TYPEMISMATCH, if the type cannot be converted
1013 HRESULT WINAPI
VarI2FromStr(OLECHAR
* strIn
, LCID lcid
, ULONG dwFlags
, SHORT
* psOut
)
1015 return VARIANT_NumberFromBstr(strIn
, lcid
, dwFlags
, psOut
, VT_I2
);
1018 /************************************************************************
1019 * VarI2FromDisp (OLEAUT32.55)
1021 * Convert a VT_DISPATCH to a VT_I2.
1024 * pdispIn [I] Source
1025 * lcid [I] LCID for conversion
1026 * psOut [O] Destination
1030 * Failure: E_INVALIDARG, if pdispIn is invalid,
1031 * DISP_E_OVERFLOW, if the value will not fit in the destination,
1032 * DISP_E_TYPEMISMATCH, if the type cannot be converted
1034 HRESULT WINAPI
VarI2FromDisp(IDispatch
* pdispIn
, LCID lcid
, SHORT
* psOut
)
1036 return VARIANT_FromDisp(pdispIn
, lcid
, psOut
, VT_I2
, 0);
1039 /************************************************************************
1040 * VarI2FromBool (OLEAUT32.56)
1042 * Convert a VT_BOOL to a VT_I2.
1046 * psOut [O] Destination
1051 HRESULT WINAPI
VarI2FromBool(VARIANT_BOOL boolIn
, SHORT
* psOut
)
1053 return _VarI2FromBool(boolIn
, psOut
);
1056 /************************************************************************
1057 * VarI2FromI1 (OLEAUT32.205)
1059 * Convert a VT_I1 to a VT_I2.
1063 * psOut [O] Destination
1068 HRESULT WINAPI
VarI2FromI1(signed char cIn
, SHORT
* psOut
)
1070 return _VarI2FromI1(cIn
, psOut
);
1073 /************************************************************************
1074 * VarI2FromUI2 (OLEAUT32.206)
1076 * Convert a VT_UI2 to a VT_I2.
1080 * psOut [O] Destination
1084 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1086 HRESULT WINAPI
VarI2FromUI2(USHORT usIn
, SHORT
* psOut
)
1088 return _VarI2FromUI2(usIn
, psOut
);
1091 /************************************************************************
1092 * VarI2FromUI4 (OLEAUT32.207)
1094 * Convert a VT_UI4 to a VT_I2.
1098 * psOut [O] Destination
1102 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1104 HRESULT WINAPI
VarI2FromUI4(ULONG ulIn
, SHORT
* psOut
)
1106 return _VarI2FromUI4(ulIn
, psOut
);
1109 /************************************************************************
1110 * VarI2FromDec (OLEAUT32.208)
1112 * Convert a VT_DECIMAL to a VT_I2.
1116 * psOut [O] Destination
1120 * Failure: E_INVALIDARG, if the source value is invalid
1121 * DISP_E_OVERFLOW, if the value will not fit in the destination
1123 HRESULT WINAPI
VarI2FromDec(DECIMAL
*pdecIn
, SHORT
* psOut
)
1128 hRet
= VarI8FromDec(pdecIn
, &i64
);
1130 if (SUCCEEDED(hRet
))
1131 hRet
= _VarI2FromI8(i64
, psOut
);
1135 /************************************************************************
1136 * VarI2FromI8 (OLEAUT32.346)
1138 * Convert a VT_I8 to a VT_I2.
1142 * psOut [O] Destination
1146 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1148 HRESULT WINAPI
VarI2FromI8(LONG64 llIn
, SHORT
* psOut
)
1150 return _VarI2FromI8(llIn
, psOut
);
1153 /************************************************************************
1154 * VarI2FromUI8 (OLEAUT32.347)
1156 * Convert a VT_UI8 to a VT_I2.
1160 * psOut [O] Destination
1164 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1166 HRESULT WINAPI
VarI2FromUI8(ULONG64 ullIn
, SHORT
* psOut
)
1168 return _VarI2FromUI8(ullIn
, psOut
);
1174 /************************************************************************
1175 * VarUI2FromUI1 (OLEAUT32.257)
1177 * Convert a VT_UI1 to a VT_UI2.
1181 * pusOut [O] Destination
1186 HRESULT WINAPI
VarUI2FromUI1(BYTE bIn
, USHORT
* pusOut
)
1188 return _VarUI2FromUI1(bIn
, pusOut
);
1191 /************************************************************************
1192 * VarUI2FromI2 (OLEAUT32.258)
1194 * Convert a VT_I2 to a VT_UI2.
1198 * pusOut [O] Destination
1202 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1204 HRESULT WINAPI
VarUI2FromI2(SHORT sIn
, USHORT
* pusOut
)
1206 return _VarUI2FromI2(sIn
, pusOut
);
1209 /************************************************************************
1210 * VarUI2FromI4 (OLEAUT32.259)
1212 * Convert a VT_I4 to a VT_UI2.
1216 * pusOut [O] Destination
1220 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1222 HRESULT WINAPI
VarUI2FromI4(LONG iIn
, USHORT
* pusOut
)
1224 return _VarUI2FromI4(iIn
, pusOut
);
1227 /************************************************************************
1228 * VarUI2FromR4 (OLEAUT32.260)
1230 * Convert a VT_R4 to a VT_UI2.
1234 * pusOut [O] Destination
1238 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1240 HRESULT WINAPI
VarUI2FromR4(FLOAT fltIn
, USHORT
* pusOut
)
1242 return VarUI2FromR8(fltIn
, pusOut
);
1245 /************************************************************************
1246 * VarUI2FromR8 (OLEAUT32.261)
1248 * Convert a VT_R8 to a VT_UI2.
1252 * pusOut [O] Destination
1256 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1259 * See VarI8FromR8() for details concerning rounding.
1261 HRESULT WINAPI
VarUI2FromR8(double dblIn
, USHORT
* pusOut
)
1263 if (dblIn
< -0.5 || dblIn
>= UI2_MAX
+ 0.5)
1264 return DISP_E_OVERFLOW
;
1265 VARIANT_DutchRound(USHORT
, dblIn
, *pusOut
);
1269 /************************************************************************
1270 * VarUI2FromDate (OLEAUT32.262)
1272 * Convert a VT_DATE to a VT_UI2.
1276 * pusOut [O] Destination
1280 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1282 HRESULT WINAPI
VarUI2FromDate(DATE dateIn
, USHORT
* pusOut
)
1284 return VarUI2FromR8(dateIn
, pusOut
);
1287 /************************************************************************
1288 * VarUI2FromCy (OLEAUT32.263)
1290 * Convert a VT_CY to a VT_UI2.
1294 * pusOut [O] Destination
1298 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1301 * Negative values >= -5000 will be converted to 0.
1303 HRESULT WINAPI
VarUI2FromCy(CY cyIn
, USHORT
* pusOut
)
1305 ULONG i
= UI2_MAX
+ 1;
1307 VarUI4FromCy(cyIn
, &i
);
1308 return _VarUI2FromUI4(i
, pusOut
);
1311 /************************************************************************
1312 * VarUI2FromStr (OLEAUT32.264)
1314 * Convert a VT_BSTR to a VT_UI2.
1318 * lcid [I] LCID for the conversion
1319 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1320 * pusOut [O] Destination
1324 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1325 * DISP_E_TYPEMISMATCH, if the type cannot be converted
1327 HRESULT WINAPI
VarUI2FromStr(OLECHAR
* strIn
, LCID lcid
, ULONG dwFlags
, USHORT
* pusOut
)
1329 return VARIANT_NumberFromBstr(strIn
, lcid
, dwFlags
, pusOut
, VT_UI2
);
1332 /************************************************************************
1333 * VarUI2FromDisp (OLEAUT32.265)
1335 * Convert a VT_DISPATCH to a VT_UI2.
1338 * pdispIn [I] Source
1339 * lcid [I] LCID for conversion
1340 * pusOut [O] Destination
1344 * Failure: E_INVALIDARG, if the source value is invalid
1345 * DISP_E_OVERFLOW, if the value will not fit in the destination
1346 * DISP_E_TYPEMISMATCH, if the type cannot be converted
1348 HRESULT WINAPI
VarUI2FromDisp(IDispatch
* pdispIn
, LCID lcid
, USHORT
* pusOut
)
1350 return VARIANT_FromDisp(pdispIn
, lcid
, pusOut
, VT_UI2
, 0);
1353 /************************************************************************
1354 * VarUI2FromBool (OLEAUT32.266)
1356 * Convert a VT_BOOL to a VT_UI2.
1360 * pusOut [O] Destination
1365 HRESULT WINAPI
VarUI2FromBool(VARIANT_BOOL boolIn
, USHORT
* pusOut
)
1367 return _VarUI2FromBool(boolIn
, pusOut
);
1370 /************************************************************************
1371 * VarUI2FromI1 (OLEAUT32.267)
1373 * Convert a VT_I1 to a VT_UI2.
1377 * pusOut [O] Destination
1381 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1383 HRESULT WINAPI
VarUI2FromI1(signed char cIn
, USHORT
* pusOut
)
1385 return _VarUI2FromI1(cIn
, pusOut
);
1388 /************************************************************************
1389 * VarUI2FromUI4 (OLEAUT32.268)
1391 * Convert a VT_UI4 to a VT_UI2.
1395 * pusOut [O] Destination
1399 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1401 HRESULT WINAPI
VarUI2FromUI4(ULONG ulIn
, USHORT
* pusOut
)
1403 return _VarUI2FromUI4(ulIn
, pusOut
);
1406 /************************************************************************
1407 * VarUI2FromDec (OLEAUT32.269)
1409 * Convert a VT_DECIMAL to a VT_UI2.
1413 * pusOut [O] Destination
1417 * Failure: E_INVALIDARG, if the source value is invalid
1418 * DISP_E_OVERFLOW, if the value will not fit in the destination
1420 HRESULT WINAPI
VarUI2FromDec(DECIMAL
*pdecIn
, USHORT
* pusOut
)
1425 hRet
= VarI8FromDec(pdecIn
, &i64
);
1427 if (SUCCEEDED(hRet
))
1428 hRet
= _VarUI2FromI8(i64
, pusOut
);
1432 /************************************************************************
1433 * VarUI2FromI8 (OLEAUT32.378)
1435 * Convert a VT_I8 to a VT_UI2.
1439 * pusOut [O] Destination
1443 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1445 HRESULT WINAPI
VarUI2FromI8(LONG64 llIn
, USHORT
* pusOut
)
1447 return _VarUI2FromI8(llIn
, pusOut
);
1450 /************************************************************************
1451 * VarUI2FromUI8 (OLEAUT32.379)
1453 * Convert a VT_UI8 to a VT_UI2.
1457 * pusOut [O] Destination
1461 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1463 HRESULT WINAPI
VarUI2FromUI8(ULONG64 ullIn
, USHORT
* pusOut
)
1465 return _VarUI2FromUI8(ullIn
, pusOut
);
1471 /************************************************************************
1472 * VarI4FromUI1 (OLEAUT32.58)
1474 * Convert a VT_UI1 to a VT_I4.
1478 * piOut [O] Destination
1483 HRESULT WINAPI
VarI4FromUI1(BYTE bIn
, LONG
*piOut
)
1485 return _VarI4FromUI1(bIn
, piOut
);
1488 /************************************************************************
1489 * VarI4FromI2 (OLEAUT32.59)
1491 * Convert a VT_I2 to a VT_I4.
1495 * piOut [O] Destination
1499 * Failure: E_INVALIDARG, if the source value is invalid
1500 * DISP_E_OVERFLOW, if the value will not fit in the destination
1502 HRESULT WINAPI
VarI4FromI2(SHORT sIn
, LONG
*piOut
)
1504 return _VarI4FromI2(sIn
, piOut
);
1507 /************************************************************************
1508 * VarI4FromR4 (OLEAUT32.60)
1510 * Convert a VT_R4 to a VT_I4.
1514 * piOut [O] Destination
1518 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1520 HRESULT WINAPI
VarI4FromR4(FLOAT fltIn
, LONG
*piOut
)
1522 return VarI4FromR8(fltIn
, piOut
);
1525 /************************************************************************
1526 * VarI4FromR8 (OLEAUT32.61)
1528 * Convert a VT_R8 to a VT_I4.
1532 * piOut [O] Destination
1536 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1539 * See VarI8FromR8() for details concerning rounding.
1541 HRESULT WINAPI
VarI4FromR8(double dblIn
, LONG
*piOut
)
1543 if (dblIn
< I4_MIN
- 0.5 || dblIn
>= I4_MAX
+ 0.5)
1544 return DISP_E_OVERFLOW
;
1545 VARIANT_DutchRound(LONG
, dblIn
, *piOut
);
1549 /************************************************************************
1550 * VarI4FromCy (OLEAUT32.62)
1552 * Convert a VT_CY to a VT_I4.
1556 * piOut [O] Destination
1560 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1562 HRESULT WINAPI
VarI4FromCy(CY cyIn
, LONG
*piOut
)
1564 double d
= cyIn
.int64
/ CY_MULTIPLIER_F
;
1565 return VarI4FromR8(d
, piOut
);
1568 /************************************************************************
1569 * VarI4FromDate (OLEAUT32.63)
1571 * Convert a VT_DATE to a VT_I4.
1575 * piOut [O] Destination
1579 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1581 HRESULT WINAPI
VarI4FromDate(DATE dateIn
, LONG
*piOut
)
1583 return VarI4FromR8(dateIn
, piOut
);
1586 /************************************************************************
1587 * VarI4FromStr (OLEAUT32.64)
1589 * Convert a VT_BSTR to a VT_I4.
1593 * lcid [I] LCID for the conversion
1594 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1595 * piOut [O] Destination
1599 * Failure: E_INVALIDARG, if any parameter is invalid
1600 * DISP_E_OVERFLOW, if the value will not fit in the destination
1601 * DISP_E_TYPEMISMATCH, if strIn cannot be converted
1603 HRESULT WINAPI
VarI4FromStr(OLECHAR
* strIn
, LCID lcid
, ULONG dwFlags
, LONG
*piOut
)
1605 return VARIANT_NumberFromBstr(strIn
, lcid
, dwFlags
, piOut
, VT_I4
);
1608 /************************************************************************
1609 * VarI4FromDisp (OLEAUT32.65)
1611 * Convert a VT_DISPATCH to a VT_I4.
1614 * pdispIn [I] Source
1615 * lcid [I] LCID for conversion
1616 * piOut [O] Destination
1620 * Failure: E_INVALIDARG, if the source value is invalid
1621 * DISP_E_OVERFLOW, if the value will not fit in the destination
1622 * DISP_E_TYPEMISMATCH, if the type cannot be converted
1624 HRESULT WINAPI
VarI4FromDisp(IDispatch
* pdispIn
, LCID lcid
, LONG
*piOut
)
1626 return VARIANT_FromDisp(pdispIn
, lcid
, piOut
, VT_I4
, 0);
1629 /************************************************************************
1630 * VarI4FromBool (OLEAUT32.66)
1632 * Convert a VT_BOOL to a VT_I4.
1636 * piOut [O] Destination
1641 HRESULT WINAPI
VarI4FromBool(VARIANT_BOOL boolIn
, LONG
*piOut
)
1643 return _VarI4FromBool(boolIn
, piOut
);
1646 /************************************************************************
1647 * VarI4FromI1 (OLEAUT32.209)
1649 * Convert a VT_I1 to a VT_I4.
1653 * piOut [O] Destination
1658 HRESULT WINAPI
VarI4FromI1(signed char cIn
, LONG
*piOut
)
1660 return _VarI4FromI1(cIn
, piOut
);
1663 /************************************************************************
1664 * VarI4FromUI2 (OLEAUT32.210)
1666 * Convert a VT_UI2 to a VT_I4.
1670 * piOut [O] Destination
1675 HRESULT WINAPI
VarI4FromUI2(USHORT usIn
, LONG
*piOut
)
1677 return _VarI4FromUI2(usIn
, piOut
);
1680 /************************************************************************
1681 * VarI4FromUI4 (OLEAUT32.211)
1683 * Convert a VT_UI4 to a VT_I4.
1687 * piOut [O] Destination
1691 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1693 HRESULT WINAPI
VarI4FromUI4(ULONG ulIn
, LONG
*piOut
)
1695 return _VarI4FromUI4(ulIn
, piOut
);
1698 /************************************************************************
1699 * VarI4FromDec (OLEAUT32.212)
1701 * Convert a VT_DECIMAL to a VT_I4.
1705 * piOut [O] Destination
1709 * Failure: E_INVALIDARG, if pdecIn is invalid
1710 * DISP_E_OVERFLOW, if the value will not fit in the destination
1712 HRESULT WINAPI
VarI4FromDec(DECIMAL
*pdecIn
, LONG
*piOut
)
1717 hRet
= VarI8FromDec(pdecIn
, &i64
);
1719 if (SUCCEEDED(hRet
))
1720 hRet
= _VarI4FromI8(i64
, piOut
);
1724 /************************************************************************
1725 * VarI4FromI8 (OLEAUT32.348)
1727 * Convert a VT_I8 to a VT_I4.
1731 * piOut [O] Destination
1735 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1737 HRESULT WINAPI
VarI4FromI8(LONG64 llIn
, LONG
*piOut
)
1739 return _VarI4FromI8(llIn
, piOut
);
1742 /************************************************************************
1743 * VarI4FromUI8 (OLEAUT32.349)
1745 * Convert a VT_UI8 to a VT_I4.
1749 * piOut [O] Destination
1753 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1755 HRESULT WINAPI
VarI4FromUI8(ULONG64 ullIn
, LONG
*piOut
)
1757 return _VarI4FromUI8(ullIn
, piOut
);
1763 /************************************************************************
1764 * VarUI4FromUI1 (OLEAUT32.270)
1766 * Convert a VT_UI1 to a VT_UI4.
1770 * pulOut [O] Destination
1775 HRESULT WINAPI
VarUI4FromUI1(BYTE bIn
, ULONG
*pulOut
)
1777 return _VarUI4FromUI1(bIn
, pulOut
);
1780 /************************************************************************
1781 * VarUI4FromI2 (OLEAUT32.271)
1783 * Convert a VT_I2 to a VT_UI4.
1787 * pulOut [O] Destination
1791 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1793 HRESULT WINAPI
VarUI4FromI2(SHORT sIn
, ULONG
*pulOut
)
1795 return _VarUI4FromI2(sIn
, pulOut
);
1798 /************************************************************************
1799 * VarUI4FromI4 (OLEAUT32.272)
1801 * Convert a VT_I4 to a VT_UI4.
1805 * pulOut [O] Destination
1809 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1811 HRESULT WINAPI
VarUI4FromI4(LONG iIn
, ULONG
*pulOut
)
1813 return _VarUI4FromI4(iIn
, pulOut
);
1816 /************************************************************************
1817 * VarUI4FromR4 (OLEAUT32.273)
1819 * Convert a VT_R4 to a VT_UI4.
1823 * pulOut [O] Destination
1827 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1829 HRESULT WINAPI
VarUI4FromR4(FLOAT fltIn
, ULONG
*pulOut
)
1831 return VarUI4FromR8(fltIn
, pulOut
);
1834 /************************************************************************
1835 * VarUI4FromR8 (OLEAUT32.274)
1837 * Convert a VT_R8 to a VT_UI4.
1841 * pulOut [O] Destination
1845 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1848 * See VarI8FromR8() for details concerning rounding.
1850 HRESULT WINAPI
VarUI4FromR8(double dblIn
, ULONG
*pulOut
)
1852 if (dblIn
< -0.5 || dblIn
>= UI4_MAX
+ 0.5)
1853 return DISP_E_OVERFLOW
;
1854 VARIANT_DutchRound(ULONG
, dblIn
, *pulOut
);
1858 /************************************************************************
1859 * VarUI4FromDate (OLEAUT32.275)
1861 * Convert a VT_DATE to a VT_UI4.
1865 * pulOut [O] Destination
1869 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1871 HRESULT WINAPI
VarUI4FromDate(DATE dateIn
, ULONG
*pulOut
)
1873 return VarUI4FromR8(dateIn
, pulOut
);
1876 /************************************************************************
1877 * VarUI4FromCy (OLEAUT32.276)
1879 * Convert a VT_CY to a VT_UI4.
1883 * pulOut [O] Destination
1887 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1889 HRESULT WINAPI
VarUI4FromCy(CY cyIn
, ULONG
*pulOut
)
1891 double d
= cyIn
.int64
/ CY_MULTIPLIER_F
;
1892 return VarUI4FromR8(d
, pulOut
);
1895 /************************************************************************
1896 * VarUI4FromStr (OLEAUT32.277)
1898 * Convert a VT_BSTR to a VT_UI4.
1902 * lcid [I] LCID for the conversion
1903 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1904 * pulOut [O] Destination
1908 * Failure: E_INVALIDARG, if any parameter is invalid
1909 * DISP_E_OVERFLOW, if the value will not fit in the destination
1910 * DISP_E_TYPEMISMATCH, if strIn cannot be converted
1912 HRESULT WINAPI
VarUI4FromStr(OLECHAR
* strIn
, LCID lcid
, ULONG dwFlags
, ULONG
*pulOut
)
1914 return VARIANT_NumberFromBstr(strIn
, lcid
, dwFlags
, pulOut
, VT_UI4
);
1917 /************************************************************************
1918 * VarUI4FromDisp (OLEAUT32.278)
1920 * Convert a VT_DISPATCH to a VT_UI4.
1923 * pdispIn [I] Source
1924 * lcid [I] LCID for conversion
1925 * pulOut [O] Destination
1929 * Failure: E_INVALIDARG, if the source value is invalid
1930 * DISP_E_OVERFLOW, if the value will not fit in the destination
1931 * DISP_E_TYPEMISMATCH, if the type cannot be converted
1933 HRESULT WINAPI
VarUI4FromDisp(IDispatch
* pdispIn
, LCID lcid
, ULONG
*pulOut
)
1935 return VARIANT_FromDisp(pdispIn
, lcid
, pulOut
, VT_UI4
, 0);
1938 /************************************************************************
1939 * VarUI4FromBool (OLEAUT32.279)
1941 * Convert a VT_BOOL to a VT_UI4.
1945 * pulOut [O] Destination
1950 HRESULT WINAPI
VarUI4FromBool(VARIANT_BOOL boolIn
, ULONG
*pulOut
)
1952 return _VarUI4FromBool(boolIn
, pulOut
);
1955 /************************************************************************
1956 * VarUI4FromI1 (OLEAUT32.280)
1958 * Convert a VT_I1 to a VT_UI4.
1962 * pulOut [O] Destination
1966 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1968 HRESULT WINAPI
VarUI4FromI1(signed char cIn
, ULONG
*pulOut
)
1970 return _VarUI4FromI1(cIn
, pulOut
);
1973 /************************************************************************
1974 * VarUI4FromUI2 (OLEAUT32.281)
1976 * Convert a VT_UI2 to a VT_UI4.
1980 * pulOut [O] Destination
1985 HRESULT WINAPI
VarUI4FromUI2(USHORT usIn
, ULONG
*pulOut
)
1987 return _VarUI4FromUI2(usIn
, pulOut
);
1990 /************************************************************************
1991 * VarUI4FromDec (OLEAUT32.282)
1993 * Convert a VT_DECIMAL to a VT_UI4.
1997 * pulOut [O] Destination
2001 * Failure: E_INVALIDARG, if pdecIn is invalid
2002 * DISP_E_OVERFLOW, if the value will not fit in the destination
2004 HRESULT WINAPI
VarUI4FromDec(DECIMAL
*pdecIn
, ULONG
*pulOut
)
2009 hRet
= VarI8FromDec(pdecIn
, &i64
);
2011 if (SUCCEEDED(hRet
))
2012 hRet
= _VarUI4FromI8(i64
, pulOut
);
2016 /************************************************************************
2017 * VarUI4FromI8 (OLEAUT32.425)
2019 * Convert a VT_I8 to a VT_UI4.
2023 * pulOut [O] Destination
2027 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
2029 HRESULT WINAPI
VarUI4FromI8(LONG64 llIn
, ULONG
*pulOut
)
2031 return _VarUI4FromI8(llIn
, pulOut
);
2034 /************************************************************************
2035 * VarUI4FromUI8 (OLEAUT32.426)
2037 * Convert a VT_UI8 to a VT_UI4.
2041 * pulOut [O] Destination
2045 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
2047 HRESULT WINAPI
VarUI4FromUI8(ULONG64 ullIn
, ULONG
*pulOut
)
2049 return _VarUI4FromUI8(ullIn
, pulOut
);
2055 /************************************************************************
2056 * VarI8FromUI1 (OLEAUT32.333)
2058 * Convert a VT_UI1 to a VT_I8.
2062 * pi64Out [O] Destination
2067 HRESULT WINAPI
VarI8FromUI1(BYTE bIn
, LONG64
* pi64Out
)
2069 return _VarI8FromUI1(bIn
, pi64Out
);
2073 /************************************************************************
2074 * VarI8FromI2 (OLEAUT32.334)
2076 * Convert a VT_I2 to a VT_I8.
2080 * pi64Out [O] Destination
2085 HRESULT WINAPI
VarI8FromI2(SHORT sIn
, LONG64
* pi64Out
)
2087 return _VarI8FromI2(sIn
, pi64Out
);
2090 /************************************************************************
2091 * VarI8FromR4 (OLEAUT32.335)
2093 * Convert a VT_R4 to a VT_I8.
2097 * pi64Out [O] Destination
2101 * Failure: E_INVALIDARG, if the source value is invalid
2102 * DISP_E_OVERFLOW, if the value will not fit in the destination
2104 HRESULT WINAPI
VarI8FromR4(FLOAT fltIn
, LONG64
* pi64Out
)
2106 return VarI8FromR8(fltIn
, pi64Out
);
2109 /************************************************************************
2110 * VarI8FromR8 (OLEAUT32.336)
2112 * Convert a VT_R8 to a VT_I8.
2116 * pi64Out [O] Destination
2120 * Failure: E_INVALIDARG, if the source value is invalid
2121 * DISP_E_OVERFLOW, if the value will not fit in the destination
2124 * Only values that fit into 63 bits are accepted. Due to rounding issues,
2125 * very high or low values will not be accurately converted.
2127 * Numbers are rounded using Dutch rounding, as follows:
2129 *| Fractional Part Sign Direction Example
2130 *| --------------- ---- --------- -------
2131 *| < 0.5 + Down 0.4 -> 0.0
2132 *| < 0.5 - Up -0.4 -> 0.0
2133 *| > 0.5 + Up 0.6 -> 1.0
2134 *| < 0.5 - Up -0.6 -> -1.0
2135 *| = 0.5 + Up/Down Down if even, Up if odd
2136 *| = 0.5 - Up/Down Up if even, Down if odd
2138 * This system is often used in supermarkets.
2140 HRESULT WINAPI
VarI8FromR8(double dblIn
, LONG64
* pi64Out
)
2142 if ( dblIn
< -4611686018427387904.0 || dblIn
>= 4611686018427387904.0)
2143 return DISP_E_OVERFLOW
;
2144 VARIANT_DutchRound(LONG64
, dblIn
, *pi64Out
);
2148 /************************************************************************
2149 * VarI8FromCy (OLEAUT32.337)
2151 * Convert a VT_CY to a VT_I8.
2155 * pi64Out [O] Destination
2161 * All negative numbers are rounded down by 1, including those that are
2162 * evenly divisible by 10000 (this is a Win32 bug that Wine mimics).
2163 * Positive numbers are rounded using Dutch rounding: See VarI8FromR8()
2166 HRESULT WINAPI
VarI8FromCy(CY cyIn
, LONG64
* pi64Out
)
2168 *pi64Out
= cyIn
.int64
/ CY_MULTIPLIER
;
2171 (*pi64Out
)--; /* Mimic Win32 bug */
2174 cyIn
.int64
-= *pi64Out
* CY_MULTIPLIER
; /* cyIn.s.Lo now holds fractional remainder */
2176 if (cyIn
.s
.Lo
> CY_HALF
|| (cyIn
.s
.Lo
== CY_HALF
&& (*pi64Out
& 0x1)))
2182 /************************************************************************
2183 * VarI8FromDate (OLEAUT32.338)
2185 * Convert a VT_DATE to a VT_I8.
2189 * pi64Out [O] Destination
2193 * Failure: E_INVALIDARG, if the source value is invalid
2194 * DISP_E_OVERFLOW, if the value will not fit in the destination
2195 * DISP_E_TYPEMISMATCH, if the type cannot be converted
2197 HRESULT WINAPI
VarI8FromDate(DATE dateIn
, LONG64
* pi64Out
)
2199 return VarI8FromR8(dateIn
, pi64Out
);
2202 /************************************************************************
2203 * VarI8FromStr (OLEAUT32.339)
2205 * Convert a VT_BSTR to a VT_I8.
2209 * lcid [I] LCID for the conversion
2210 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
2211 * pi64Out [O] Destination
2215 * Failure: E_INVALIDARG, if the source value is invalid
2216 * DISP_E_OVERFLOW, if the value will not fit in the destination
2217 * DISP_E_TYPEMISMATCH, if the type cannot be converted
2219 HRESULT WINAPI
VarI8FromStr(OLECHAR
* strIn
, LCID lcid
, ULONG dwFlags
, LONG64
* pi64Out
)
2221 return VARIANT_NumberFromBstr(strIn
, lcid
, dwFlags
, pi64Out
, VT_I8
);
2224 /************************************************************************
2225 * VarI8FromDisp (OLEAUT32.340)
2227 * Convert a VT_DISPATCH to a VT_I8.
2230 * pdispIn [I] Source
2231 * lcid [I] LCID for conversion
2232 * pi64Out [O] Destination
2236 * Failure: E_INVALIDARG, if the source value is invalid
2237 * DISP_E_OVERFLOW, if the value will not fit in the destination
2238 * DISP_E_TYPEMISMATCH, if the type cannot be converted
2240 HRESULT WINAPI
VarI8FromDisp(IDispatch
* pdispIn
, LCID lcid
, LONG64
* pi64Out
)
2242 return VARIANT_FromDisp(pdispIn
, lcid
, pi64Out
, VT_I8
, 0);
2245 /************************************************************************
2246 * VarI8FromBool (OLEAUT32.341)
2248 * Convert a VT_BOOL to a VT_I8.
2252 * pi64Out [O] Destination
2257 HRESULT WINAPI
VarI8FromBool(VARIANT_BOOL boolIn
, LONG64
* pi64Out
)
2259 return VarI8FromI2(boolIn
, pi64Out
);
2262 /************************************************************************
2263 * VarI8FromI1 (OLEAUT32.342)
2265 * Convert a VT_I1 to a VT_I8.
2269 * pi64Out [O] Destination
2274 HRESULT WINAPI
VarI8FromI1(signed char cIn
, LONG64
* pi64Out
)
2276 return _VarI8FromI1(cIn
, pi64Out
);
2279 /************************************************************************
2280 * VarI8FromUI2 (OLEAUT32.343)
2282 * Convert a VT_UI2 to a VT_I8.
2286 * pi64Out [O] Destination
2291 HRESULT WINAPI
VarI8FromUI2(USHORT usIn
, LONG64
* pi64Out
)
2293 return _VarI8FromUI2(usIn
, pi64Out
);
2296 /************************************************************************
2297 * VarI8FromUI4 (OLEAUT32.344)
2299 * Convert a VT_UI4 to a VT_I8.
2303 * pi64Out [O] Destination
2308 HRESULT WINAPI
VarI8FromUI4(ULONG ulIn
, LONG64
* pi64Out
)
2310 return _VarI8FromUI4(ulIn
, pi64Out
);
2313 /************************************************************************
2314 * VarI8FromDec (OLEAUT32.345)
2316 * Convert a VT_DECIMAL to a VT_I8.
2320 * pi64Out [O] Destination
2324 * Failure: E_INVALIDARG, if the source value is invalid
2325 * DISP_E_OVERFLOW, if the value will not fit in the destination
2327 HRESULT WINAPI
VarI8FromDec(DECIMAL
*pdecIn
, LONG64
* pi64Out
)
2329 if (!DEC_SCALE(pdecIn
))
2331 /* This decimal is just a 96 bit integer */
2332 if (DEC_SIGN(pdecIn
) & ~DECIMAL_NEG
)
2333 return E_INVALIDARG
;
2335 if (DEC_HI32(pdecIn
) || DEC_MID32(pdecIn
) & 0x80000000)
2336 return DISP_E_OVERFLOW
;
2338 if (DEC_SIGN(pdecIn
))
2339 *pi64Out
= -DEC_LO64(pdecIn
);
2341 *pi64Out
= DEC_LO64(pdecIn
);
2346 /* Decimal contains a floating point number */
2350 hRet
= VarR8FromDec(pdecIn
, &dbl
);
2351 if (SUCCEEDED(hRet
))
2352 hRet
= VarI8FromR8(dbl
, pi64Out
);
2357 /************************************************************************
2358 * VarI8FromUI8 (OLEAUT32.427)
2360 * Convert a VT_UI8 to a VT_I8.
2364 * pi64Out [O] Destination
2368 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
2370 HRESULT WINAPI
VarI8FromUI8(ULONG64 ullIn
, LONG64
* pi64Out
)
2372 return _VarI8FromUI8(ullIn
, pi64Out
);
2378 /************************************************************************
2379 * VarUI8FromI8 (OLEAUT32.428)
2381 * Convert a VT_I8 to a VT_UI8.
2385 * pui64Out [O] Destination
2389 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
2391 HRESULT WINAPI
VarUI8FromI8(LONG64 llIn
, ULONG64
* pui64Out
)
2393 return _VarUI8FromI8(llIn
, pui64Out
);
2396 /************************************************************************
2397 * VarUI8FromUI1 (OLEAUT32.429)
2399 * Convert a VT_UI1 to a VT_UI8.
2403 * pui64Out [O] Destination
2408 HRESULT WINAPI
VarUI8FromUI1(BYTE bIn
, ULONG64
* pui64Out
)
2410 return _VarUI8FromUI1(bIn
, pui64Out
);
2413 /************************************************************************
2414 * VarUI8FromI2 (OLEAUT32.430)
2416 * Convert a VT_I2 to a VT_UI8.
2420 * pui64Out [O] Destination
2425 HRESULT WINAPI
VarUI8FromI2(SHORT sIn
, ULONG64
* pui64Out
)
2427 return _VarUI8FromI2(sIn
, pui64Out
);
2430 /************************************************************************
2431 * VarUI8FromR4 (OLEAUT32.431)
2433 * Convert a VT_R4 to a VT_UI8.
2437 * pui64Out [O] Destination
2441 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
2443 HRESULT WINAPI
VarUI8FromR4(FLOAT fltIn
, ULONG64
* pui64Out
)
2445 return VarUI8FromR8(fltIn
, pui64Out
);
2448 /************************************************************************
2449 * VarUI8FromR8 (OLEAUT32.432)
2451 * Convert a VT_R8 to a VT_UI8.
2455 * pui64Out [O] Destination
2459 * Failure: E_INVALIDARG, if the source value is invalid
2460 * DISP_E_OVERFLOW, if the value will not fit in the destination
2463 * See VarI8FromR8() for details concerning rounding.
2465 HRESULT WINAPI
VarUI8FromR8(double dblIn
, ULONG64
* pui64Out
)
2467 if (dblIn
< -0.5 || dblIn
> 1.844674407370955e19
)
2468 return DISP_E_OVERFLOW
;
2469 VARIANT_DutchRound(ULONG64
, dblIn
, *pui64Out
);
2473 /************************************************************************
2474 * VarUI8FromCy (OLEAUT32.433)
2476 * Convert a VT_CY to a VT_UI8.
2480 * pui64Out [O] Destination
2484 * Failure: E_INVALIDARG, if the source value is invalid
2485 * DISP_E_OVERFLOW, if the value will not fit in the destination
2488 * Negative values >= -5000 will be converted to 0.
2490 HRESULT WINAPI
VarUI8FromCy(CY cyIn
, ULONG64
* pui64Out
)
2494 if (cyIn
.int64
< -CY_HALF
)
2495 return DISP_E_OVERFLOW
;
2500 *pui64Out
= cyIn
.int64
/ CY_MULTIPLIER
;
2502 cyIn
.int64
-= *pui64Out
* CY_MULTIPLIER
; /* cyIn.s.Lo now holds fractional remainder */
2504 if (cyIn
.s
.Lo
> CY_HALF
|| (cyIn
.s
.Lo
== CY_HALF
&& (*pui64Out
& 0x1)))
2510 /************************************************************************
2511 * VarUI8FromDate (OLEAUT32.434)
2513 * Convert a VT_DATE to a VT_UI8.
2517 * pui64Out [O] Destination
2521 * Failure: E_INVALIDARG, if the source value is invalid
2522 * DISP_E_OVERFLOW, if the value will not fit in the destination
2523 * DISP_E_TYPEMISMATCH, if the type cannot be converted
2525 HRESULT WINAPI
VarUI8FromDate(DATE dateIn
, ULONG64
* pui64Out
)
2527 return VarUI8FromR8(dateIn
, pui64Out
);
2530 /************************************************************************
2531 * VarUI8FromStr (OLEAUT32.435)
2533 * Convert a VT_BSTR to a VT_UI8.
2537 * lcid [I] LCID for the conversion
2538 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
2539 * pui64Out [O] Destination
2543 * Failure: E_INVALIDARG, if the source value is invalid
2544 * DISP_E_OVERFLOW, if the value will not fit in the destination
2545 * DISP_E_TYPEMISMATCH, if the type cannot be converted
2547 HRESULT WINAPI
VarUI8FromStr(OLECHAR
* strIn
, LCID lcid
, ULONG dwFlags
, ULONG64
* pui64Out
)
2549 return VARIANT_NumberFromBstr(strIn
, lcid
, dwFlags
, pui64Out
, VT_UI8
);
2552 /************************************************************************
2553 * VarUI8FromDisp (OLEAUT32.436)
2555 * Convert a VT_DISPATCH to a VT_UI8.
2558 * pdispIn [I] Source
2559 * lcid [I] LCID for conversion
2560 * pui64Out [O] Destination
2564 * Failure: E_INVALIDARG, if the source value is invalid
2565 * DISP_E_OVERFLOW, if the value will not fit in the destination
2566 * DISP_E_TYPEMISMATCH, if the type cannot be converted
2568 HRESULT WINAPI
VarUI8FromDisp(IDispatch
* pdispIn
, LCID lcid
, ULONG64
* pui64Out
)
2570 return VARIANT_FromDisp(pdispIn
, lcid
, pui64Out
, VT_UI8
, 0);
2573 /************************************************************************
2574 * VarUI8FromBool (OLEAUT32.437)
2576 * Convert a VT_BOOL to a VT_UI8.
2580 * pui64Out [O] Destination
2584 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
2586 HRESULT WINAPI
VarUI8FromBool(VARIANT_BOOL boolIn
, ULONG64
* pui64Out
)
2588 return VarI8FromI2(boolIn
, (LONG64
*)pui64Out
);
2590 /************************************************************************
2591 * VarUI8FromI1 (OLEAUT32.438)
2593 * Convert a VT_I1 to a VT_UI8.
2597 * pui64Out [O] Destination
2601 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
2603 HRESULT WINAPI
VarUI8FromI1(signed char cIn
, ULONG64
* pui64Out
)
2605 return _VarUI8FromI1(cIn
, pui64Out
);
2608 /************************************************************************
2609 * VarUI8FromUI2 (OLEAUT32.439)
2611 * Convert a VT_UI2 to a VT_UI8.
2615 * pui64Out [O] Destination
2620 HRESULT WINAPI
VarUI8FromUI2(USHORT usIn
, ULONG64
* pui64Out
)
2622 return _VarUI8FromUI2(usIn
, pui64Out
);
2625 /************************************************************************
2626 * VarUI8FromUI4 (OLEAUT32.440)
2628 * Convert a VT_UI4 to a VT_UI8.
2632 * pui64Out [O] Destination
2637 HRESULT WINAPI
VarUI8FromUI4(ULONG ulIn
, ULONG64
* pui64Out
)
2639 return _VarUI8FromUI4(ulIn
, pui64Out
);
2642 /************************************************************************
2643 * VarUI8FromDec (OLEAUT32.441)
2645 * Convert a VT_DECIMAL to a VT_UI8.
2649 * pui64Out [O] Destination
2653 * Failure: E_INVALIDARG, if the source value is invalid
2654 * DISP_E_OVERFLOW, if the value will not fit in the destination
2657 * Under native Win32, if the source value has a scale of 0, its sign is
2658 * ignored, i.e. this function takes the absolute value rather than fail
2659 * with DISP_E_OVERFLOW. This bug has been fixed in Wine's implementation
2660 * (use VarAbs() on pDecIn first if you really want this behaviour).
2662 HRESULT WINAPI
VarUI8FromDec(DECIMAL
*pdecIn
, ULONG64
* pui64Out
)
2664 if (!DEC_SCALE(pdecIn
))
2666 /* This decimal is just a 96 bit integer */
2667 if (DEC_SIGN(pdecIn
) & ~DECIMAL_NEG
)
2668 return E_INVALIDARG
;
2670 if (DEC_HI32(pdecIn
))
2671 return DISP_E_OVERFLOW
;
2673 if (DEC_SIGN(pdecIn
))
2675 WARN("Sign would be ignored under Win32!\n");
2676 return DISP_E_OVERFLOW
;
2679 *pui64Out
= DEC_LO64(pdecIn
);
2684 /* Decimal contains a floating point number */
2688 hRet
= VarR8FromDec(pdecIn
, &dbl
);
2689 if (SUCCEEDED(hRet
))
2690 hRet
= VarUI8FromR8(dbl
, pui64Out
);
2698 /************************************************************************
2699 * VarR4FromUI1 (OLEAUT32.68)
2701 * Convert a VT_UI1 to a VT_R4.
2705 * pFltOut [O] Destination
2710 HRESULT WINAPI
VarR4FromUI1(BYTE bIn
, float *pFltOut
)
2712 return _VarR4FromUI1(bIn
, pFltOut
);
2715 /************************************************************************
2716 * VarR4FromI2 (OLEAUT32.69)
2718 * Convert a VT_I2 to a VT_R4.
2722 * pFltOut [O] Destination
2727 HRESULT WINAPI
VarR4FromI2(SHORT sIn
, float *pFltOut
)
2729 return _VarR4FromI2(sIn
, pFltOut
);
2732 /************************************************************************
2733 * VarR4FromI4 (OLEAUT32.70)
2735 * Convert a VT_I4 to a VT_R4.
2739 * pFltOut [O] Destination
2744 HRESULT WINAPI
VarR4FromI4(LONG lIn
, float *pFltOut
)
2746 return _VarR4FromI4(lIn
, pFltOut
);
2749 /************************************************************************
2750 * VarR4FromR8 (OLEAUT32.71)
2752 * Convert a VT_R8 to a VT_R4.
2756 * pFltOut [O] Destination
2760 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination.
2762 HRESULT WINAPI
VarR4FromR8(double dblIn
, float *pFltOut
)
2764 double d
= dblIn
< 0.0 ? -dblIn
: dblIn
;
2765 if (d
> R4_MAX
) return DISP_E_OVERFLOW
;
2770 /************************************************************************
2771 * VarR4FromCy (OLEAUT32.72)
2773 * Convert a VT_CY to a VT_R4.
2777 * pFltOut [O] Destination
2782 HRESULT WINAPI
VarR4FromCy(CY cyIn
, float *pFltOut
)
2784 *pFltOut
= (double)cyIn
.int64
/ CY_MULTIPLIER_F
;
2788 /************************************************************************
2789 * VarR4FromDate (OLEAUT32.73)
2791 * Convert a VT_DATE to a VT_R4.
2795 * pFltOut [O] Destination
2799 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination.
2801 HRESULT WINAPI
VarR4FromDate(DATE dateIn
, float *pFltOut
)
2803 return VarR4FromR8(dateIn
, pFltOut
);
2806 /************************************************************************
2807 * VarR4FromStr (OLEAUT32.74)
2809 * Convert a VT_BSTR to a VT_R4.
2813 * lcid [I] LCID for the conversion
2814 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
2815 * pFltOut [O] Destination
2819 * Failure: E_INVALIDARG, if strIn or pFltOut is invalid.
2820 * DISP_E_TYPEMISMATCH, if the type cannot be converted
2822 HRESULT WINAPI
VarR4FromStr(OLECHAR
* strIn
, LCID lcid
, ULONG dwFlags
, float *pFltOut
)
2824 return VARIANT_NumberFromBstr(strIn
, lcid
, dwFlags
, pFltOut
, VT_R4
);
2827 /************************************************************************
2828 * VarR4FromDisp (OLEAUT32.75)
2830 * Convert a VT_DISPATCH to a VT_R4.
2833 * pdispIn [I] Source
2834 * lcid [I] LCID for conversion
2835 * pFltOut [O] Destination
2839 * Failure: E_INVALIDARG, if the source value is invalid
2840 * DISP_E_OVERFLOW, if the value will not fit in the destination
2841 * DISP_E_TYPEMISMATCH, if the type cannot be converted
2843 HRESULT WINAPI
VarR4FromDisp(IDispatch
* pdispIn
, LCID lcid
, float *pFltOut
)
2845 return VARIANT_FromDisp(pdispIn
, lcid
, pFltOut
, VT_R4
, 0);
2848 /************************************************************************
2849 * VarR4FromBool (OLEAUT32.76)
2851 * Convert a VT_BOOL to a VT_R4.
2855 * pFltOut [O] Destination
2860 HRESULT WINAPI
VarR4FromBool(VARIANT_BOOL boolIn
, float *pFltOut
)
2862 return VarR4FromI2(boolIn
, pFltOut
);
2865 /************************************************************************
2866 * VarR4FromI1 (OLEAUT32.213)
2868 * Convert a VT_I1 to a VT_R4.
2872 * pFltOut [O] Destination
2876 * Failure: E_INVALIDARG, if the source value is invalid
2877 * DISP_E_OVERFLOW, if the value will not fit in the destination
2878 * DISP_E_TYPEMISMATCH, if the type cannot be converted
2880 HRESULT WINAPI
VarR4FromI1(signed char cIn
, float *pFltOut
)
2882 return _VarR4FromI1(cIn
, pFltOut
);
2885 /************************************************************************
2886 * VarR4FromUI2 (OLEAUT32.214)
2888 * Convert a VT_UI2 to a VT_R4.
2892 * pFltOut [O] Destination
2896 * Failure: E_INVALIDARG, if the source value is invalid
2897 * DISP_E_OVERFLOW, if the value will not fit in the destination
2898 * DISP_E_TYPEMISMATCH, if the type cannot be converted
2900 HRESULT WINAPI
VarR4FromUI2(USHORT usIn
, float *pFltOut
)
2902 return _VarR4FromUI2(usIn
, pFltOut
);
2905 /************************************************************************
2906 * VarR4FromUI4 (OLEAUT32.215)
2908 * Convert a VT_UI4 to a VT_R4.
2912 * pFltOut [O] Destination
2916 * Failure: E_INVALIDARG, if the source value is invalid
2917 * DISP_E_OVERFLOW, if the value will not fit in the destination
2918 * DISP_E_TYPEMISMATCH, if the type cannot be converted
2920 HRESULT WINAPI
VarR4FromUI4(ULONG ulIn
, float *pFltOut
)
2922 return _VarR4FromUI4(ulIn
, pFltOut
);
2925 /************************************************************************
2926 * VarR4FromDec (OLEAUT32.216)
2928 * Convert a VT_DECIMAL to a VT_R4.
2932 * pFltOut [O] Destination
2936 * Failure: E_INVALIDARG, if the source value is invalid.
2938 HRESULT WINAPI
VarR4FromDec(DECIMAL
* pDecIn
, float *pFltOut
)
2940 BYTE scale
= DEC_SCALE(pDecIn
);
2944 if (scale
> DEC_MAX_SCALE
|| DEC_SIGN(pDecIn
) & ~DECIMAL_NEG
)
2945 return E_INVALIDARG
;
2950 if (DEC_SIGN(pDecIn
))
2953 if (DEC_HI32(pDecIn
))
2955 highPart
= (double)DEC_HI32(pDecIn
) / (double)divisor
;
2956 highPart
*= 4294967296.0F
;
2957 highPart
*= 4294967296.0F
;
2962 *pFltOut
= (double)DEC_LO64(pDecIn
) / (double)divisor
+ highPart
;
2966 /************************************************************************
2967 * VarR4FromI8 (OLEAUT32.360)
2969 * Convert a VT_I8 to a VT_R4.
2973 * pFltOut [O] Destination
2978 HRESULT WINAPI
VarR4FromI8(LONG64 llIn
, float *pFltOut
)
2980 return _VarR4FromI8(llIn
, pFltOut
);
2983 /************************************************************************
2984 * VarR4FromUI8 (OLEAUT32.361)
2986 * Convert a VT_UI8 to a VT_R4.
2990 * pFltOut [O] Destination
2995 HRESULT WINAPI
VarR4FromUI8(ULONG64 ullIn
, float *pFltOut
)
2997 return _VarR4FromUI8(ullIn
, pFltOut
);
3000 /************************************************************************
3001 * VarR4CmpR8 (OLEAUT32.316)
3003 * Compare a VT_R4 to a VT_R8.
3006 * fltLeft [I] Source
3007 * dblRight [I] Value to compare
3010 * VARCMP_LT, VARCMP_EQ or VARCMP_GT indicating that fltLeft is less than,
3011 * equal to or greater than dblRight respectively.
3013 HRESULT WINAPI
VarR4CmpR8(float fltLeft
, double dblRight
)
3015 if (fltLeft
< dblRight
)
3017 else if (fltLeft
> dblRight
)
3025 /************************************************************************
3026 * VarR8FromUI1 (OLEAUT32.78)
3028 * Convert a VT_UI1 to a VT_R8.
3032 * pDblOut [O] Destination
3037 HRESULT WINAPI
VarR8FromUI1(BYTE bIn
, double *pDblOut
)
3039 return _VarR8FromUI1(bIn
, pDblOut
);
3042 /************************************************************************
3043 * VarR8FromI2 (OLEAUT32.79)
3045 * Convert a VT_I2 to a VT_R8.
3049 * pDblOut [O] Destination
3054 HRESULT WINAPI
VarR8FromI2(SHORT sIn
, double *pDblOut
)
3056 return _VarR8FromI2(sIn
, pDblOut
);
3059 /************************************************************************
3060 * VarR8FromI4 (OLEAUT32.80)
3062 * Convert a VT_I4 to a VT_R8.
3066 * pDblOut [O] Destination
3071 HRESULT WINAPI
VarR8FromI4(LONG lIn
, double *pDblOut
)
3073 return _VarR8FromI4(lIn
, pDblOut
);
3076 /************************************************************************
3077 * VarR8FromR4 (OLEAUT32.81)
3079 * Convert a VT_R4 to a VT_R8.
3083 * pDblOut [O] Destination
3088 HRESULT WINAPI
VarR8FromR4(FLOAT fltIn
, double *pDblOut
)
3090 return _VarR8FromR4(fltIn
, pDblOut
);
3093 /************************************************************************
3094 * VarR8FromCy (OLEAUT32.82)
3096 * Convert a VT_CY to a VT_R8.
3100 * pDblOut [O] Destination
3105 HRESULT WINAPI
VarR8FromCy(CY cyIn
, double *pDblOut
)
3107 return _VarR8FromCy(cyIn
, pDblOut
);
3110 /************************************************************************
3111 * VarR8FromDate (OLEAUT32.83)
3113 * Convert a VT_DATE to a VT_R8.
3117 * pDblOut [O] Destination
3122 HRESULT WINAPI
VarR8FromDate(DATE dateIn
, double *pDblOut
)
3124 return _VarR8FromDate(dateIn
, pDblOut
);
3127 /************************************************************************
3128 * VarR8FromStr (OLEAUT32.84)
3130 * Convert a VT_BSTR to a VT_R8.
3134 * lcid [I] LCID for the conversion
3135 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
3136 * pDblOut [O] Destination
3140 * Failure: E_INVALIDARG, if strIn or pDblOut is invalid.
3141 * DISP_E_TYPEMISMATCH, if the type cannot be converted
3143 HRESULT WINAPI
VarR8FromStr(OLECHAR
* strIn
, LCID lcid
, ULONG dwFlags
, double *pDblOut
)
3145 return VARIANT_NumberFromBstr(strIn
, lcid
, dwFlags
, pDblOut
, VT_R8
);
3148 /************************************************************************
3149 * VarR8FromDisp (OLEAUT32.85)
3151 * Convert a VT_DISPATCH to a VT_R8.
3154 * pdispIn [I] Source
3155 * lcid [I] LCID for conversion
3156 * pDblOut [O] Destination
3160 * Failure: E_INVALIDARG, if the source value is invalid
3161 * DISP_E_OVERFLOW, if the value will not fit in the destination
3162 * DISP_E_TYPEMISMATCH, if the type cannot be converted
3164 HRESULT WINAPI
VarR8FromDisp(IDispatch
* pdispIn
, LCID lcid
, double *pDblOut
)
3166 return VARIANT_FromDisp(pdispIn
, lcid
, pDblOut
, VT_R8
, 0);
3169 /************************************************************************
3170 * VarR8FromBool (OLEAUT32.86)
3172 * Convert a VT_BOOL to a VT_R8.
3176 * pDblOut [O] Destination
3181 HRESULT WINAPI
VarR8FromBool(VARIANT_BOOL boolIn
, double *pDblOut
)
3183 return VarR8FromI2(boolIn
, pDblOut
);
3186 /************************************************************************
3187 * VarR8FromI1 (OLEAUT32.217)
3189 * Convert a VT_I1 to a VT_R8.
3193 * pDblOut [O] Destination
3197 * Failure: E_INVALIDARG, if the source value is invalid
3198 * DISP_E_OVERFLOW, if the value will not fit in the destination
3199 * DISP_E_TYPEMISMATCH, if the type cannot be converted
3201 HRESULT WINAPI
VarR8FromI1(signed char cIn
, double *pDblOut
)
3203 return _VarR8FromI1(cIn
, pDblOut
);
3206 /************************************************************************
3207 * VarR8FromUI2 (OLEAUT32.218)
3209 * Convert a VT_UI2 to a VT_R8.
3213 * pDblOut [O] Destination
3217 * Failure: E_INVALIDARG, if the source value is invalid
3218 * DISP_E_OVERFLOW, if the value will not fit in the destination
3219 * DISP_E_TYPEMISMATCH, if the type cannot be converted
3221 HRESULT WINAPI
VarR8FromUI2(USHORT usIn
, double *pDblOut
)
3223 return _VarR8FromUI2(usIn
, pDblOut
);
3226 /************************************************************************
3227 * VarR8FromUI4 (OLEAUT32.219)
3229 * Convert a VT_UI4 to a VT_R8.
3233 * pDblOut [O] Destination
3237 * Failure: E_INVALIDARG, if the source value is invalid
3238 * DISP_E_OVERFLOW, if the value will not fit in the destination
3239 * DISP_E_TYPEMISMATCH, if the type cannot be converted
3241 HRESULT WINAPI
VarR8FromUI4(ULONG ulIn
, double *pDblOut
)
3243 return _VarR8FromUI4(ulIn
, pDblOut
);
3246 /************************************************************************
3247 * VarR8FromDec (OLEAUT32.220)
3249 * Convert a VT_DECIMAL to a VT_R8.
3253 * pDblOut [O] Destination
3257 * Failure: E_INVALIDARG, if the source value is invalid.
3259 HRESULT WINAPI
VarR8FromDec(const DECIMAL
* pDecIn
, double *pDblOut
)
3261 BYTE scale
= DEC_SCALE(pDecIn
);
3262 double divisor
= 1.0, highPart
;
3264 if (scale
> DEC_MAX_SCALE
|| DEC_SIGN(pDecIn
) & ~DECIMAL_NEG
)
3265 return E_INVALIDARG
;
3270 if (DEC_SIGN(pDecIn
))
3273 if (DEC_HI32(pDecIn
))
3275 highPart
= (double)DEC_HI32(pDecIn
) / divisor
;
3276 highPart
*= 4294967296.0F
;
3277 highPart
*= 4294967296.0F
;
3282 *pDblOut
= (double)DEC_LO64(pDecIn
) / divisor
+ highPart
;
3286 /************************************************************************
3287 * VarR8FromI8 (OLEAUT32.362)
3289 * Convert a VT_I8 to a VT_R8.
3293 * pDblOut [O] Destination
3298 HRESULT WINAPI
VarR8FromI8(LONG64 llIn
, double *pDblOut
)
3300 return _VarR8FromI8(llIn
, pDblOut
);
3303 /************************************************************************
3304 * VarR8FromUI8 (OLEAUT32.363)
3306 * Convert a VT_UI8 to a VT_R8.
3310 * pDblOut [O] Destination
3315 HRESULT WINAPI
VarR8FromUI8(ULONG64 ullIn
, double *pDblOut
)
3317 return _VarR8FromUI8(ullIn
, pDblOut
);
3320 /************************************************************************
3321 * VarR8Pow (OLEAUT32.315)
3323 * Raise a VT_R8 to a power.
3326 * dblLeft [I] Source
3327 * dblPow [I] Power to raise dblLeft by
3328 * pDblOut [O] Destination
3331 * S_OK. pDblOut contains dblLeft to the power of dblRight.
3333 HRESULT WINAPI
VarR8Pow(double dblLeft
, double dblPow
, double *pDblOut
)
3335 *pDblOut
= pow(dblLeft
, dblPow
);
3339 /************************************************************************
3340 * VarR8Round (OLEAUT32.317)
3342 * Round a VT_R8 to a given number of decimal points.
3346 * nDig [I] Number of decimal points to round to
3347 * pDblOut [O] Destination for rounded number
3350 * Success: S_OK. pDblOut is rounded to nDig digits.
3351 * Failure: E_INVALIDARG, if cDecimals is less than 0.
3354 * The native version of this function rounds using the internal
3355 * binary representation of the number. Wine uses the dutch rounding
3356 * convention, so therefore small differences can occur in the value returned.
3357 * MSDN says that you should use your own rounding function if you want
3358 * rounding to be predictable in your application.
3360 HRESULT WINAPI
VarR8Round(double dblIn
, int nDig
, double *pDblOut
)
3362 double scale
, whole
, fract
;
3365 return E_INVALIDARG
;
3367 scale
= pow(10.0, nDig
);
3370 whole
= dblIn
< 0 ? ceil(dblIn
) : floor(dblIn
);
3371 fract
= dblIn
- whole
;
3374 dblIn
= whole
+ 1.0;
3375 else if (fract
== 0.5)
3376 dblIn
= whole
+ fmod(whole
, 2.0);
3377 else if (fract
>= 0.0)
3379 else if (fract
== -0.5)
3380 dblIn
= whole
- fmod(whole
, 2.0);
3381 else if (fract
> -0.5)
3384 dblIn
= whole
- 1.0;
3386 *pDblOut
= dblIn
/ scale
;
3393 /* Powers of 10 from 0..4 D.P. */
3394 static const int CY_Divisors
[5] = { CY_MULTIPLIER
/10000, CY_MULTIPLIER
/1000,
3395 CY_MULTIPLIER
/100, CY_MULTIPLIER
/10, CY_MULTIPLIER
};
3397 /************************************************************************
3398 * VarCyFromUI1 (OLEAUT32.98)
3400 * Convert a VT_UI1 to a VT_CY.
3404 * pCyOut [O] Destination
3408 * Failure: E_INVALIDARG, if the source value is invalid
3409 * DISP_E_OVERFLOW, if the value will not fit in the destination
3410 * DISP_E_TYPEMISMATCH, if the type cannot be converted
3412 HRESULT WINAPI
VarCyFromUI1(BYTE bIn
, CY
* pCyOut
)
3414 pCyOut
->int64
= (ULONG64
)bIn
* CY_MULTIPLIER
;
3418 /************************************************************************
3419 * VarCyFromI2 (OLEAUT32.99)
3421 * Convert a VT_I2 to a VT_CY.
3425 * pCyOut [O] Destination
3429 * Failure: E_INVALIDARG, if the source value is invalid
3430 * DISP_E_OVERFLOW, if the value will not fit in the destination
3431 * DISP_E_TYPEMISMATCH, if the type cannot be converted
3433 HRESULT WINAPI
VarCyFromI2(SHORT sIn
, CY
* pCyOut
)
3435 pCyOut
->int64
= (LONG64
)sIn
* CY_MULTIPLIER
;
3439 /************************************************************************
3440 * VarCyFromI4 (OLEAUT32.100)
3442 * Convert a VT_I4 to a VT_CY.
3446 * pCyOut [O] Destination
3450 * Failure: E_INVALIDARG, if the source value is invalid
3451 * DISP_E_OVERFLOW, if the value will not fit in the destination
3452 * DISP_E_TYPEMISMATCH, if the type cannot be converted
3454 HRESULT WINAPI
VarCyFromI4(LONG lIn
, CY
* pCyOut
)
3456 pCyOut
->int64
= (LONG64
)lIn
* CY_MULTIPLIER
;
3460 /************************************************************************
3461 * VarCyFromR4 (OLEAUT32.101)
3463 * Convert a VT_R4 to a VT_CY.
3467 * pCyOut [O] Destination
3471 * Failure: E_INVALIDARG, if the source value is invalid
3472 * DISP_E_OVERFLOW, if the value will not fit in the destination
3473 * DISP_E_TYPEMISMATCH, if the type cannot be converted
3475 HRESULT WINAPI
VarCyFromR4(FLOAT fltIn
, CY
* pCyOut
)
3477 return VarCyFromR8(fltIn
, pCyOut
);
3480 /************************************************************************
3481 * VarCyFromR8 (OLEAUT32.102)
3483 * Convert a VT_R8 to a VT_CY.
3487 * pCyOut [O] Destination
3491 * Failure: E_INVALIDARG, if the source value is invalid
3492 * DISP_E_OVERFLOW, if the value will not fit in the destination
3493 * DISP_E_TYPEMISMATCH, if the type cannot be converted
3495 HRESULT WINAPI
VarCyFromR8(double dblIn
, CY
* pCyOut
)
3497 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3498 /* This code gives identical results to Win32 on Intel.
3499 * Here we use fp exceptions to catch overflows when storing the value.
3501 static const unsigned short r8_fpcontrol
= 0x137f;
3502 static const double r8_multiplier
= CY_MULTIPLIER_F
;
3503 unsigned short old_fpcontrol
, result_fpstatus
;
3505 /* Clear exceptions, save the old fp state and load the new state */
3506 __asm__
__volatile__( "fnclex" );
3507 __asm__
__volatile__( "fstcw %0" : "=m" (old_fpcontrol
) : );
3508 __asm__
__volatile__( "fldcw %0" : : "m" (r8_fpcontrol
) );
3509 /* Perform the conversion. */
3510 __asm__
__volatile__( "fldl %0" : : "m" (dblIn
) );
3511 __asm__
__volatile__( "fmull %0" : : "m" (r8_multiplier
) );
3512 __asm__
__volatile__( "fistpll %0" : : "m" (*pCyOut
) );
3513 /* Save the resulting fp state, load the old state and clear exceptions */
3514 __asm__
__volatile__( "fstsw %0" : "=m" (result_fpstatus
) : );
3515 __asm__
__volatile__( "fnclex" );
3516 __asm__
__volatile__( "fldcw %0" : : "m" (old_fpcontrol
) );
3518 if (result_fpstatus
& 0x9) /* Overflow | Invalid */
3519 return DISP_E_OVERFLOW
;
3521 /* This version produces slightly different results for boundary cases */
3522 if (dblIn
< -922337203685477.5807 || dblIn
>= 922337203685477.5807)
3523 return DISP_E_OVERFLOW
;
3524 dblIn
*= CY_MULTIPLIER_F
;
3525 VARIANT_DutchRound(LONG64
, dblIn
, pCyOut
->int64
);
3530 /************************************************************************
3531 * VarCyFromDate (OLEAUT32.103)
3533 * Convert a VT_DATE to a VT_CY.
3537 * pCyOut [O] Destination
3541 * Failure: E_INVALIDARG, if the source value is invalid
3542 * DISP_E_OVERFLOW, if the value will not fit in the destination
3543 * DISP_E_TYPEMISMATCH, if the type cannot be converted
3545 HRESULT WINAPI
VarCyFromDate(DATE dateIn
, CY
* pCyOut
)
3547 return VarCyFromR8(dateIn
, pCyOut
);
3550 /************************************************************************
3551 * VarCyFromStr (OLEAUT32.104)
3553 * Convert a VT_BSTR to a VT_CY.
3557 * lcid [I] LCID for the conversion
3558 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
3559 * pCyOut [O] Destination
3563 * Failure: E_INVALIDARG, if the source value is invalid
3564 * DISP_E_OVERFLOW, if the value will not fit in the destination
3565 * DISP_E_TYPEMISMATCH, if the type cannot be converted
3567 HRESULT WINAPI
VarCyFromStr(OLECHAR
* strIn
, LCID lcid
, ULONG dwFlags
, CY
* pCyOut
)
3569 return VARIANT_NumberFromBstr(strIn
, lcid
, dwFlags
, pCyOut
, VT_CY
);
3572 /************************************************************************
3573 * VarCyFromDisp (OLEAUT32.105)
3575 * Convert a VT_DISPATCH to a VT_CY.
3578 * pdispIn [I] Source
3579 * lcid [I] LCID for conversion
3580 * pCyOut [O] Destination
3584 * Failure: E_INVALIDARG, if the source value is invalid
3585 * DISP_E_OVERFLOW, if the value will not fit in the destination
3586 * DISP_E_TYPEMISMATCH, if the type cannot be converted
3588 HRESULT WINAPI
VarCyFromDisp(IDispatch
* pdispIn
, LCID lcid
, CY
* pCyOut
)
3590 return VARIANT_FromDisp(pdispIn
, lcid
, pCyOut
, VT_CY
, 0);
3593 /************************************************************************
3594 * VarCyFromBool (OLEAUT32.106)
3596 * Convert a VT_BOOL to a VT_CY.
3600 * pCyOut [O] Destination
3604 * Failure: E_INVALIDARG, if the source value is invalid
3605 * DISP_E_OVERFLOW, if the value will not fit in the destination
3606 * DISP_E_TYPEMISMATCH, if the type cannot be converted
3609 * While the sign of the boolean is stored in the currency, the value is
3610 * converted to either 0 or 1.
3612 HRESULT WINAPI
VarCyFromBool(VARIANT_BOOL boolIn
, CY
* pCyOut
)
3614 pCyOut
->int64
= (LONG64
)boolIn
* CY_MULTIPLIER
;
3618 /************************************************************************
3619 * VarCyFromI1 (OLEAUT32.225)
3621 * Convert a VT_I1 to a VT_CY.
3625 * pCyOut [O] Destination
3629 * Failure: E_INVALIDARG, if the source value is invalid
3630 * DISP_E_OVERFLOW, if the value will not fit in the destination
3631 * DISP_E_TYPEMISMATCH, if the type cannot be converted
3633 HRESULT WINAPI
VarCyFromI1(signed char cIn
, CY
* pCyOut
)
3635 pCyOut
->int64
= (LONG64
)cIn
* CY_MULTIPLIER
;
3639 /************************************************************************
3640 * VarCyFromUI2 (OLEAUT32.226)
3642 * Convert a VT_UI2 to a VT_CY.
3646 * pCyOut [O] Destination
3650 * Failure: E_INVALIDARG, if the source value is invalid
3651 * DISP_E_OVERFLOW, if the value will not fit in the destination
3652 * DISP_E_TYPEMISMATCH, if the type cannot be converted
3654 HRESULT WINAPI
VarCyFromUI2(USHORT usIn
, CY
* pCyOut
)
3656 pCyOut
->int64
= (ULONG64
)usIn
* CY_MULTIPLIER
;
3660 /************************************************************************
3661 * VarCyFromUI4 (OLEAUT32.227)
3663 * Convert a VT_UI4 to a VT_CY.
3667 * pCyOut [O] Destination
3671 * Failure: E_INVALIDARG, if the source value is invalid
3672 * DISP_E_OVERFLOW, if the value will not fit in the destination
3673 * DISP_E_TYPEMISMATCH, if the type cannot be converted
3675 HRESULT WINAPI
VarCyFromUI4(ULONG ulIn
, CY
* pCyOut
)
3677 pCyOut
->int64
= (ULONG64
)ulIn
* CY_MULTIPLIER
;
3681 /************************************************************************
3682 * VarCyFromDec (OLEAUT32.228)
3684 * Convert a VT_DECIMAL to a VT_CY.
3688 * pCyOut [O] Destination
3692 * Failure: E_INVALIDARG, if the source value is invalid
3693 * DISP_E_OVERFLOW, if the value will not fit in the destination
3694 * DISP_E_TYPEMISMATCH, if the type cannot be converted
3696 HRESULT WINAPI
VarCyFromDec(DECIMAL
* pdecIn
, CY
* pCyOut
)
3701 hRet
= VarDecRound(pdecIn
, 4, &rounded
);
3703 if (SUCCEEDED(hRet
))
3707 if (DEC_HI32(&rounded
))
3708 return DISP_E_OVERFLOW
;
3710 /* Note: Without the casts this promotes to int64 which loses precision */
3711 d
= (double)DEC_LO64(&rounded
) / (double)CY_Divisors
[DEC_SCALE(&rounded
)];
3712 if (DEC_SIGN(&rounded
))
3714 return VarCyFromR8(d
, pCyOut
);
3719 /************************************************************************
3720 * VarCyFromI8 (OLEAUT32.366)
3722 * Convert a VT_I8 to a VT_CY.
3726 * pCyOut [O] Destination
3730 * Failure: E_INVALIDARG, if the source value is invalid
3731 * DISP_E_OVERFLOW, if the value will not fit in the destination
3732 * DISP_E_TYPEMISMATCH, if the type cannot be converted
3734 HRESULT WINAPI
VarCyFromI8(LONG64 llIn
, CY
* pCyOut
)
3736 if (llIn
<= (I8_MIN
/CY_MULTIPLIER
) || llIn
>= (I8_MAX
/CY_MULTIPLIER
)) return DISP_E_OVERFLOW
;
3737 pCyOut
->int64
= llIn
* CY_MULTIPLIER
;
3741 /************************************************************************
3742 * VarCyFromUI8 (OLEAUT32.375)
3744 * Convert a VT_UI8 to a VT_CY.
3748 * pCyOut [O] Destination
3752 * Failure: E_INVALIDARG, if the source value is invalid
3753 * DISP_E_OVERFLOW, if the value will not fit in the destination
3754 * DISP_E_TYPEMISMATCH, if the type cannot be converted
3756 HRESULT WINAPI
VarCyFromUI8(ULONG64 ullIn
, CY
* pCyOut
)
3758 if (ullIn
> (I8_MAX
/CY_MULTIPLIER
)) return DISP_E_OVERFLOW
;
3759 pCyOut
->int64
= ullIn
* CY_MULTIPLIER
;
3763 /************************************************************************
3764 * VarCyAdd (OLEAUT32.299)
3766 * Add one CY to another.
3770 * cyRight [I] Value to add
3771 * pCyOut [O] Destination
3775 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
3777 HRESULT WINAPI
VarCyAdd(const CY cyLeft
, const CY cyRight
, CY
* pCyOut
)
3780 _VarR8FromCy(cyLeft
, &l
);
3781 _VarR8FromCy(cyRight
, &r
);
3783 return VarCyFromR8(l
, pCyOut
);
3786 /************************************************************************
3787 * VarCyMul (OLEAUT32.303)
3789 * Multiply one CY by another.
3793 * cyRight [I] Value to multiply by
3794 * pCyOut [O] Destination
3798 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
3800 HRESULT WINAPI
VarCyMul(const CY cyLeft
, const CY cyRight
, CY
* pCyOut
)
3803 _VarR8FromCy(cyLeft
, &l
);
3804 _VarR8FromCy(cyRight
, &r
);
3806 return VarCyFromR8(l
, pCyOut
);
3809 /************************************************************************
3810 * VarCyMulI4 (OLEAUT32.304)
3812 * Multiply one CY by a VT_I4.
3816 * lRight [I] Value to multiply by
3817 * pCyOut [O] Destination
3821 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
3823 HRESULT WINAPI
VarCyMulI4(const CY cyLeft
, LONG lRight
, CY
* pCyOut
)
3827 _VarR8FromCy(cyLeft
, &d
);
3829 return VarCyFromR8(d
, pCyOut
);
3832 /************************************************************************
3833 * VarCySub (OLEAUT32.305)
3835 * Subtract one CY from another.
3839 * cyRight [I] Value to subtract
3840 * pCyOut [O] Destination
3844 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
3846 HRESULT WINAPI
VarCySub(const CY cyLeft
, const CY cyRight
, CY
* pCyOut
)
3849 _VarR8FromCy(cyLeft
, &l
);
3850 _VarR8FromCy(cyRight
, &r
);
3852 return VarCyFromR8(l
, pCyOut
);
3855 /************************************************************************
3856 * VarCyAbs (OLEAUT32.306)
3858 * Convert a VT_CY into its absolute value.
3862 * pCyOut [O] Destination
3865 * Success: S_OK. pCyOut contains the absolute value.
3866 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
3868 HRESULT WINAPI
VarCyAbs(const CY cyIn
, CY
* pCyOut
)
3870 if (cyIn
.s
.Hi
== (int)0x80000000 && !cyIn
.s
.Lo
)
3871 return DISP_E_OVERFLOW
;
3873 pCyOut
->int64
= cyIn
.int64
< 0 ? -cyIn
.int64
: cyIn
.int64
;
3877 /************************************************************************
3878 * VarCyFix (OLEAUT32.307)
3880 * Return the integer part of a VT_CY.
3884 * pCyOut [O] Destination
3888 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
3891 * - The difference between this function and VarCyInt() is that VarCyInt() rounds
3892 * negative numbers away from 0, while this function rounds them towards zero.
3894 HRESULT WINAPI
VarCyFix(const CY cyIn
, CY
* pCyOut
)
3896 pCyOut
->int64
= cyIn
.int64
/ CY_MULTIPLIER
;
3897 pCyOut
->int64
*= CY_MULTIPLIER
;
3901 /************************************************************************
3902 * VarCyInt (OLEAUT32.308)
3904 * Return the integer part of a VT_CY.
3908 * pCyOut [O] Destination
3912 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
3915 * - The difference between this function and VarCyFix() is that VarCyFix() rounds
3916 * negative numbers towards 0, while this function rounds them away from zero.
3918 HRESULT WINAPI
VarCyInt(const CY cyIn
, CY
* pCyOut
)
3920 pCyOut
->int64
= cyIn
.int64
/ CY_MULTIPLIER
;
3921 pCyOut
->int64
*= CY_MULTIPLIER
;
3923 if (cyIn
.int64
< 0 && cyIn
.int64
% CY_MULTIPLIER
!= 0)
3925 pCyOut
->int64
-= CY_MULTIPLIER
;
3930 /************************************************************************
3931 * VarCyNeg (OLEAUT32.309)
3933 * Change the sign of a VT_CY.
3937 * pCyOut [O] Destination
3941 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
3943 HRESULT WINAPI
VarCyNeg(const CY cyIn
, CY
* pCyOut
)
3945 if (cyIn
.s
.Hi
== (int)0x80000000 && !cyIn
.s
.Lo
)
3946 return DISP_E_OVERFLOW
;
3948 pCyOut
->int64
= -cyIn
.int64
;
3952 /************************************************************************
3953 * VarCyRound (OLEAUT32.310)
3955 * Change the precision of a VT_CY.
3959 * cDecimals [I] New number of decimals to keep
3960 * pCyOut [O] Destination
3964 * Failure: E_INVALIDARG, if cDecimals is less than 0.
3966 HRESULT WINAPI
VarCyRound(const CY cyIn
, int cDecimals
, CY
* pCyOut
)
3969 return E_INVALIDARG
;
3973 /* Rounding to more precision than we have */
3979 double d
, div
= CY_Divisors
[cDecimals
];
3981 _VarR8FromCy(cyIn
, &d
);
3983 VARIANT_DutchRound(LONGLONG
, d
, pCyOut
->int64
);
3984 d
= (double)pCyOut
->int64
/ div
* CY_MULTIPLIER_F
;
3985 VARIANT_DutchRound(LONGLONG
, d
, pCyOut
->int64
);
3990 /************************************************************************
3991 * VarCyCmp (OLEAUT32.311)
3993 * Compare two VT_CY values.
3997 * cyRight [I] Value to compare
4000 * Success: VARCMP_LT, VARCMP_EQ or VARCMP_GT indicating that the value to
4001 * compare is less, equal or greater than source respectively.
4002 * Failure: DISP_E_OVERFLOW, if overflow occurs during the comparison
4004 HRESULT WINAPI
VarCyCmp(const CY cyLeft
, const CY cyRight
)
4009 /* Subtract right from left, and compare the result to 0 */
4010 hRet
= VarCySub(cyLeft
, cyRight
, &result
);
4012 if (SUCCEEDED(hRet
))
4014 if (result
.int64
< 0)
4015 hRet
= (HRESULT
)VARCMP_LT
;
4016 else if (result
.int64
> 0)
4017 hRet
= (HRESULT
)VARCMP_GT
;
4019 hRet
= (HRESULT
)VARCMP_EQ
;
4024 /************************************************************************
4025 * VarCyCmpR8 (OLEAUT32.312)
4027 * Compare a VT_CY to a double
4030 * cyLeft [I] Currency Source
4031 * dblRight [I] double to compare to cyLeft
4034 * Success: VARCMP_LT, VARCMP_EQ or VARCMP_GT indicating that dblRight is
4035 * less than, equal to or greater than cyLeft respectively.
4036 * Failure: DISP_E_OVERFLOW, if overflow occurs during the comparison
4038 HRESULT WINAPI
VarCyCmpR8(const CY cyLeft
, double dblRight
)
4043 hRet
= VarCyFromR8(dblRight
, &cyRight
);
4045 if (SUCCEEDED(hRet
))
4046 hRet
= VarCyCmp(cyLeft
, cyRight
);
4051 /************************************************************************
4052 * VarCyMulI8 (OLEAUT32.329)
4054 * Multiply a VT_CY by a VT_I8.
4058 * llRight [I] Value to multiply by
4059 * pCyOut [O] Destination
4063 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
4065 HRESULT WINAPI
VarCyMulI8(const CY cyLeft
, LONG64 llRight
, CY
* pCyOut
)
4069 _VarR8FromCy(cyLeft
, &d
);
4070 d
= d
* (double)llRight
;
4071 return VarCyFromR8(d
, pCyOut
);
4077 /************************************************************************
4078 * VarDecFromUI1 (OLEAUT32.190)
4080 * Convert a VT_UI1 to a DECIMAL.
4084 * pDecOut [O] Destination
4089 HRESULT WINAPI
VarDecFromUI1(BYTE bIn
, DECIMAL
* pDecOut
)
4091 return VarDecFromUI4(bIn
, pDecOut
);
4094 /************************************************************************
4095 * VarDecFromI2 (OLEAUT32.191)
4097 * Convert a VT_I2 to a DECIMAL.
4101 * pDecOut [O] Destination
4106 HRESULT WINAPI
VarDecFromI2(SHORT sIn
, DECIMAL
* pDecOut
)
4108 return VarDecFromI4(sIn
, pDecOut
);
4111 /************************************************************************
4112 * VarDecFromI4 (OLEAUT32.192)
4114 * Convert a VT_I4 to a DECIMAL.
4118 * pDecOut [O] Destination
4123 HRESULT WINAPI
VarDecFromI4(LONG lIn
, DECIMAL
* pDecOut
)
4125 DEC_HI32(pDecOut
) = 0;
4126 DEC_MID32(pDecOut
) = 0;
4130 DEC_SIGNSCALE(pDecOut
) = SIGNSCALE(DECIMAL_NEG
,0);
4131 DEC_LO32(pDecOut
) = -lIn
;
4135 DEC_SIGNSCALE(pDecOut
) = SIGNSCALE(DECIMAL_POS
,0);
4136 DEC_LO32(pDecOut
) = lIn
;
4141 #define LOCALE_EN_US (MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT))
4143 /* internal representation of the value stored in a DECIMAL. The bytes are
4144 stored from LSB at index 0 to MSB at index 11
4146 typedef struct DECIMAL_internal
4148 DWORD bitsnum
[3]; /* 96 significant bits, unsigned */
4149 unsigned char scale
; /* number scaled * 10 ^ -(scale) */
4150 unsigned int sign
: 1; /* 0 - positive, 1 - negative */
4153 static HRESULT
VARIANT_DI_FromR4(float source
, VARIANT_DI
* dest
);
4154 static HRESULT
VARIANT_DI_FromR8(double source
, VARIANT_DI
* dest
);
4155 static void VARIANT_DIFromDec(const DECIMAL
* from
, VARIANT_DI
* to
);
4156 static void VARIANT_DecFromDI(const VARIANT_DI
* from
, DECIMAL
* to
);
4158 /************************************************************************
4159 * VarDecFromR4 (OLEAUT32.193)
4161 * Convert a VT_R4 to a DECIMAL.
4165 * pDecOut [O] Destination
4170 HRESULT WINAPI
VarDecFromR4(FLOAT fltIn
, DECIMAL
* pDecOut
)
4175 hres
= VARIANT_DI_FromR4(fltIn
, &di
);
4176 if (hres
== S_OK
) VARIANT_DecFromDI(&di
, pDecOut
);
4180 /************************************************************************
4181 * VarDecFromR8 (OLEAUT32.194)
4183 * Convert a VT_R8 to a DECIMAL.
4187 * pDecOut [O] Destination
4192 HRESULT WINAPI
VarDecFromR8(double dblIn
, DECIMAL
* pDecOut
)
4197 hres
= VARIANT_DI_FromR8(dblIn
, &di
);
4198 if (hres
== S_OK
) VARIANT_DecFromDI(&di
, pDecOut
);
4202 /************************************************************************
4203 * VarDecFromDate (OLEAUT32.195)
4205 * Convert a VT_DATE to a DECIMAL.
4209 * pDecOut [O] Destination
4214 HRESULT WINAPI
VarDecFromDate(DATE dateIn
, DECIMAL
* pDecOut
)
4216 return VarDecFromR8(dateIn
, pDecOut
);
4219 /************************************************************************
4220 * VarDecFromCy (OLEAUT32.196)
4222 * Convert a VT_CY to a DECIMAL.
4226 * pDecOut [O] Destination
4231 HRESULT WINAPI
VarDecFromCy(CY cyIn
, DECIMAL
* pDecOut
)
4233 DEC_HI32(pDecOut
) = 0;
4235 /* Note: This assumes 2s complement integer representation */
4236 if (cyIn
.s
.Hi
& 0x80000000)
4238 DEC_SIGNSCALE(pDecOut
) = SIGNSCALE(DECIMAL_NEG
,4);
4239 DEC_LO64(pDecOut
) = -cyIn
.int64
;
4243 DEC_SIGNSCALE(pDecOut
) = SIGNSCALE(DECIMAL_POS
,4);
4244 DEC_MID32(pDecOut
) = cyIn
.s
.Hi
;
4245 DEC_LO32(pDecOut
) = cyIn
.s
.Lo
;
4250 /************************************************************************
4251 * VarDecFromStr (OLEAUT32.197)
4253 * Convert a VT_BSTR to a DECIMAL.
4257 * lcid [I] LCID for the conversion
4258 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
4259 * pDecOut [O] Destination
4263 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
4265 HRESULT WINAPI
VarDecFromStr(OLECHAR
* strIn
, LCID lcid
, ULONG dwFlags
, DECIMAL
* pDecOut
)
4267 return VARIANT_NumberFromBstr(strIn
, lcid
, dwFlags
, pDecOut
, VT_DECIMAL
);
4270 /************************************************************************
4271 * VarDecFromDisp (OLEAUT32.198)
4273 * Convert a VT_DISPATCH to a DECIMAL.
4276 * pdispIn [I] Source
4277 * lcid [I] LCID for conversion
4278 * pDecOut [O] Destination
4282 * Failure: DISP_E_TYPEMISMATCH, if the type cannot be converted
4284 HRESULT WINAPI
VarDecFromDisp(IDispatch
* pdispIn
, LCID lcid
, DECIMAL
* pDecOut
)
4286 return VARIANT_FromDisp(pdispIn
, lcid
, pDecOut
, VT_DECIMAL
, 0);
4289 /************************************************************************
4290 * VarDecFromBool (OLEAUT32.199)
4292 * Convert a VT_BOOL to a DECIMAL.
4296 * pDecOut [O] Destination
4302 * The value is converted to either 0 (if bIn is FALSE) or -1 (TRUE).
4304 HRESULT WINAPI
VarDecFromBool(VARIANT_BOOL bIn
, DECIMAL
* pDecOut
)
4306 DEC_HI32(pDecOut
) = 0;
4307 DEC_MID32(pDecOut
) = 0;
4310 DEC_SIGNSCALE(pDecOut
) = SIGNSCALE(DECIMAL_NEG
,0);
4311 DEC_LO32(pDecOut
) = 1;
4315 DEC_SIGNSCALE(pDecOut
) = SIGNSCALE(DECIMAL_POS
,0);
4316 DEC_LO32(pDecOut
) = 0;
4321 /************************************************************************
4322 * VarDecFromI1 (OLEAUT32.241)
4324 * Convert a VT_I1 to a DECIMAL.
4328 * pDecOut [O] Destination
4333 HRESULT WINAPI
VarDecFromI1(signed char cIn
, DECIMAL
* pDecOut
)
4335 return VarDecFromI4(cIn
, pDecOut
);
4338 /************************************************************************
4339 * VarDecFromUI2 (OLEAUT32.242)
4341 * Convert a VT_UI2 to a DECIMAL.
4345 * pDecOut [O] Destination
4350 HRESULT WINAPI
VarDecFromUI2(USHORT usIn
, DECIMAL
* pDecOut
)
4352 return VarDecFromUI4(usIn
, pDecOut
);
4355 /************************************************************************
4356 * VarDecFromUI4 (OLEAUT32.243)
4358 * Convert a VT_UI4 to a DECIMAL.
4362 * pDecOut [O] Destination
4367 HRESULT WINAPI
VarDecFromUI4(ULONG ulIn
, DECIMAL
* pDecOut
)
4369 DEC_SIGNSCALE(pDecOut
) = SIGNSCALE(DECIMAL_POS
,0);
4370 DEC_HI32(pDecOut
) = 0;
4371 DEC_MID32(pDecOut
) = 0;
4372 DEC_LO32(pDecOut
) = ulIn
;
4376 /************************************************************************
4377 * VarDecFromI8 (OLEAUT32.374)
4379 * Convert a VT_I8 to a DECIMAL.
4383 * pDecOut [O] Destination
4388 HRESULT WINAPI
VarDecFromI8(LONG64 llIn
, DECIMAL
* pDecOut
)
4390 PULARGE_INTEGER pLi
= (PULARGE_INTEGER
)&llIn
;
4392 DEC_HI32(pDecOut
) = 0;
4394 /* Note: This assumes 2s complement integer representation */
4395 if (pLi
->u
.HighPart
& 0x80000000)
4397 DEC_SIGNSCALE(pDecOut
) = SIGNSCALE(DECIMAL_NEG
,0);
4398 DEC_LO64(pDecOut
) = -pLi
->QuadPart
;
4402 DEC_SIGNSCALE(pDecOut
) = SIGNSCALE(DECIMAL_POS
,0);
4403 DEC_MID32(pDecOut
) = pLi
->u
.HighPart
;
4404 DEC_LO32(pDecOut
) = pLi
->u
.LowPart
;
4409 /************************************************************************
4410 * VarDecFromUI8 (OLEAUT32.375)
4412 * Convert a VT_UI8 to a DECIMAL.
4416 * pDecOut [O] Destination
4421 HRESULT WINAPI
VarDecFromUI8(ULONG64 ullIn
, DECIMAL
* pDecOut
)
4423 DEC_SIGNSCALE(pDecOut
) = SIGNSCALE(DECIMAL_POS
,0);
4424 DEC_HI32(pDecOut
) = 0;
4425 DEC_LO64(pDecOut
) = ullIn
;
4429 /* Make two DECIMALS the same scale; used by math functions below */
4430 static HRESULT
VARIANT_DecScale(const DECIMAL
** ppDecLeft
,
4431 const DECIMAL
** ppDecRight
,
4434 static DECIMAL scaleFactor
;
4437 HRESULT hRet
= S_OK
;
4439 if (DEC_SIGN(*ppDecLeft
) & ~DECIMAL_NEG
|| DEC_SIGN(*ppDecRight
) & ~DECIMAL_NEG
)
4440 return E_INVALIDARG
;
4442 DEC_LO32(&scaleFactor
) = 10;
4444 i
= scaleAmount
= DEC_SCALE(*ppDecLeft
) - DEC_SCALE(*ppDecRight
);
4447 return S_OK
; /* Same scale */
4449 if (scaleAmount
> 0)
4451 decTemp
= *(*ppDecRight
); /* Left is bigger - scale the right hand side */
4452 *ppDecRight
= pDecOut
;
4456 decTemp
= *(*ppDecLeft
); /* Right is bigger - scale the left hand side */
4457 *ppDecLeft
= pDecOut
;
4458 i
= scaleAmount
= -scaleAmount
;
4461 if (DEC_SCALE(&decTemp
) + scaleAmount
> DEC_MAX_SCALE
)
4462 return DISP_E_OVERFLOW
; /* Can't scale up */
4464 /* Multiply up the value to be scaled by the correct amount */
4465 while (SUCCEEDED(hRet
) && i
--)
4467 /* Note we are multiplying by a value with a scale of 0, so we don't recurse */
4468 hRet
= VarDecMul(&decTemp
, &scaleFactor
, pDecOut
);
4471 DEC_SCALE(pDecOut
) += scaleAmount
; /* Set the new scale */
4475 /* Add two unsigned 32 bit values with overflow */
4476 static ULONG
VARIANT_Add(ULONG ulLeft
, ULONG ulRight
, ULONG
* pulHigh
)
4478 ULARGE_INTEGER ul64
;
4480 ul64
.QuadPart
= (ULONG64
)ulLeft
+ (ULONG64
)ulRight
+ (ULONG64
)*pulHigh
;
4481 *pulHigh
= ul64
.u
.HighPart
;
4482 return ul64
.u
.LowPart
;
4485 /* Subtract two unsigned 32 bit values with underflow */
4486 static ULONG
VARIANT_Sub(ULONG ulLeft
, ULONG ulRight
, ULONG
* pulHigh
)
4488 BOOL invert
= FALSE
;
4489 ULARGE_INTEGER ul64
;
4491 ul64
.QuadPart
= (LONG64
)ulLeft
- (ULONG64
)ulRight
;
4492 if (ulLeft
< ulRight
)
4495 if (ul64
.QuadPart
> (ULONG64
)*pulHigh
)
4496 ul64
.QuadPart
-= (ULONG64
)*pulHigh
;
4499 ul64
.QuadPart
-= (ULONG64
)*pulHigh
;
4503 ul64
.u
.HighPart
= -ul64
.u
.HighPart
;
4505 *pulHigh
= ul64
.u
.HighPart
;
4506 return ul64
.u
.LowPart
;
4509 /* Multiply two unsigned 32 bit values with overflow */
4510 static ULONG
VARIANT_Mul(ULONG ulLeft
, ULONG ulRight
, ULONG
* pulHigh
)
4512 ULARGE_INTEGER ul64
;
4514 ul64
.QuadPart
= (ULONG64
)ulLeft
* (ULONG64
)ulRight
+ (ULONG64
)*pulHigh
;
4515 *pulHigh
= ul64
.u
.HighPart
;
4516 return ul64
.u
.LowPart
;
4519 /* Compare two decimals that have the same scale */
4520 static inline int VARIANT_DecCmp(const DECIMAL
*pDecLeft
, const DECIMAL
*pDecRight
)
4522 if ( DEC_HI32(pDecLeft
) < DEC_HI32(pDecRight
) ||
4523 (DEC_HI32(pDecLeft
) <= DEC_HI32(pDecRight
) && DEC_LO64(pDecLeft
) < DEC_LO64(pDecRight
)))
4525 else if (DEC_HI32(pDecLeft
) == DEC_HI32(pDecRight
) && DEC_LO64(pDecLeft
) == DEC_LO64(pDecRight
))
4530 /************************************************************************
4531 * VarDecAdd (OLEAUT32.177)
4533 * Add one DECIMAL to another.
4536 * pDecLeft [I] Source
4537 * pDecRight [I] Value to add
4538 * pDecOut [O] Destination
4542 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
4544 HRESULT WINAPI
VarDecAdd(const DECIMAL
* pDecLeft
, const DECIMAL
* pDecRight
, DECIMAL
* pDecOut
)
4549 hRet
= VARIANT_DecScale(&pDecLeft
, &pDecRight
, &scaled
);
4551 if (SUCCEEDED(hRet
))
4553 /* Our decimals now have the same scale, we can add them as 96 bit integers */
4555 BYTE sign
= DECIMAL_POS
;
4558 /* Correct for the sign of the result */
4559 if (DEC_SIGN(pDecLeft
) && DEC_SIGN(pDecRight
))
4561 /* -x + -y : Negative */
4563 goto VarDecAdd_AsPositive
;
4565 else if (DEC_SIGN(pDecLeft
) && !DEC_SIGN(pDecRight
))
4567 cmp
= VARIANT_DecCmp(pDecLeft
, pDecRight
);
4569 /* -x + y : Negative if x > y */
4573 VarDecAdd_AsNegative
:
4574 DEC_LO32(pDecOut
) = VARIANT_Sub(DEC_LO32(pDecLeft
), DEC_LO32(pDecRight
), &overflow
);
4575 DEC_MID32(pDecOut
) = VARIANT_Sub(DEC_MID32(pDecLeft
), DEC_MID32(pDecRight
), &overflow
);
4576 DEC_HI32(pDecOut
) = VARIANT_Sub(DEC_HI32(pDecLeft
), DEC_HI32(pDecRight
), &overflow
);
4580 VarDecAdd_AsInvertedNegative
:
4581 DEC_LO32(pDecOut
) = VARIANT_Sub(DEC_LO32(pDecRight
), DEC_LO32(pDecLeft
), &overflow
);
4582 DEC_MID32(pDecOut
) = VARIANT_Sub(DEC_MID32(pDecRight
), DEC_MID32(pDecLeft
), &overflow
);
4583 DEC_HI32(pDecOut
) = VARIANT_Sub(DEC_HI32(pDecRight
), DEC_HI32(pDecLeft
), &overflow
);
4586 else if (!DEC_SIGN(pDecLeft
) && DEC_SIGN(pDecRight
))
4588 cmp
= VARIANT_DecCmp(pDecLeft
, pDecRight
);
4590 /* x + -y : Negative if x <= y */
4594 goto VarDecAdd_AsInvertedNegative
;
4596 goto VarDecAdd_AsNegative
;
4600 /* x + y : Positive */
4601 VarDecAdd_AsPositive
:
4602 DEC_LO32(pDecOut
) = VARIANT_Add(DEC_LO32(pDecLeft
), DEC_LO32(pDecRight
), &overflow
);
4603 DEC_MID32(pDecOut
) = VARIANT_Add(DEC_MID32(pDecLeft
), DEC_MID32(pDecRight
), &overflow
);
4604 DEC_HI32(pDecOut
) = VARIANT_Add(DEC_HI32(pDecLeft
), DEC_HI32(pDecRight
), &overflow
);
4608 return DISP_E_OVERFLOW
; /* overflowed */
4610 DEC_SCALE(pDecOut
) = DEC_SCALE(pDecLeft
);
4611 DEC_SIGN(pDecOut
) = sign
;
4616 /* translate from external DECIMAL format into an internal representation */
4617 static void VARIANT_DIFromDec(const DECIMAL
* from
, VARIANT_DI
* to
)
4619 to
->scale
= DEC_SCALE(from
);
4620 to
->sign
= DEC_SIGN(from
) ? 1 : 0;
4622 to
->bitsnum
[0] = DEC_LO32(from
);
4623 to
->bitsnum
[1] = DEC_MID32(from
);
4624 to
->bitsnum
[2] = DEC_HI32(from
);
4627 static void VARIANT_DecFromDI(const VARIANT_DI
* from
, DECIMAL
* to
)
4630 DEC_SIGNSCALE(to
) = SIGNSCALE(DECIMAL_NEG
, from
->scale
);
4632 DEC_SIGNSCALE(to
) = SIGNSCALE(DECIMAL_POS
, from
->scale
);
4635 DEC_LO32(to
) = from
->bitsnum
[0];
4636 DEC_MID32(to
) = from
->bitsnum
[1];
4637 DEC_HI32(to
) = from
->bitsnum
[2];
4640 /* clear an internal representation of a DECIMAL */
4641 static void VARIANT_DI_clear(VARIANT_DI
* i
)
4643 memset(i
, 0, sizeof(VARIANT_DI
));
4646 /* divide the (unsigned) number stored in p (LSB) by a byte value (<= 0xff). Any nonzero
4647 size is supported. The value in p is replaced by the quotient of the division, and
4648 the remainder is returned as a result. This routine is most often used with a divisor
4649 of 10 in order to scale up numbers, and in the DECIMAL->string conversion.
4651 static unsigned char VARIANT_int_divbychar(DWORD
* p
, unsigned int n
, unsigned char divisor
)
4656 } else if (divisor
== 1) {
4657 /* dividend remains unchanged */
4660 unsigned char remainder
= 0;
4661 ULONGLONG iTempDividend
;
4664 for (i
= n
- 1; i
>= 0 && !p
[i
]; i
--); /* skip leading zeros */
4665 for (; i
>= 0; i
--) {
4666 iTempDividend
= ((ULONGLONG
)remainder
<< 32) + p
[i
];
4667 remainder
= iTempDividend
% divisor
;
4668 p
[i
] = iTempDividend
/ divisor
;
4675 /* check to test if encoded number is a zero. Returns 1 if zero, 0 for nonzero */
4676 static BOOL
VARIANT_int_iszero(const DWORD
* p
, unsigned int n
)
4678 for (; n
> 0; n
--) if (*p
++ != 0) return FALSE
;
4682 /* multiply two DECIMALS, without changing either one, and place result in third
4683 parameter. Result is normalized when scale is > 0. Attempts to remove significant
4684 digits when scale > 0 in order to fit an overflowing result. Final overflow
4687 static int VARIANT_DI_mul(const VARIANT_DI
* a
, const VARIANT_DI
* b
, VARIANT_DI
* result
)
4689 BOOL r_overflow
= FALSE
;
4691 signed int mulstart
;
4693 VARIANT_DI_clear(result
);
4694 result
->sign
= (a
->sign
^ b
->sign
) ? 1 : 0;
4696 /* Multiply 128-bit operands into a (max) 256-bit result. The scale
4697 of the result is formed by adding the scales of the operands.
4699 result
->scale
= a
->scale
+ b
->scale
;
4700 memset(running
, 0, sizeof(running
));
4702 /* count number of leading zero-bytes in operand A */
4703 for (mulstart
= sizeof(a
->bitsnum
)/sizeof(DWORD
) - 1; mulstart
>= 0 && !a
->bitsnum
[mulstart
]; mulstart
--);
4705 /* result is 0, because operand A is 0 */
4709 unsigned char remainder
= 0;
4712 /* perform actual multiplication */
4713 for (iA
= 0; iA
<= mulstart
; iA
++) {
4717 for (iOverflowMul
= 0, iB
= 0; iB
< sizeof(b
->bitsnum
)/sizeof(DWORD
); iB
++) {
4721 iRV
= VARIANT_Mul(b
->bitsnum
[iB
], a
->bitsnum
[iA
], &iOverflowMul
);
4724 running
[iR
] = VARIANT_Add(running
[iR
], 0, &iRV
);
4730 /* Too bad - native oleaut does not do this, so we should not either */
4732 /* While the result is divisible by 10, and the scale > 0, divide by 10.
4733 This operation should not lose significant digits, and gives an
4734 opportunity to reduce the possibility of overflows in future
4735 operations issued by the application.
4737 while (result
->scale
> 0) {
4738 memcpy(quotient
, running
, sizeof(quotient
));
4739 remainder
= VARIANT_int_divbychar(quotient
, sizeof(quotient
) / sizeof(DWORD
), 10);
4740 if (remainder
> 0) break;
4741 memcpy(running
, quotient
, sizeof(quotient
));
4745 /* While the 256-bit result overflows, and the scale > 0, divide by 10.
4746 This operation *will* lose significant digits of the result because
4747 all the factors of 10 were consumed by the previous operation.
4749 while (result
->scale
> 0 && !VARIANT_int_iszero(
4750 running
+ sizeof(result
->bitsnum
) / sizeof(DWORD
),
4751 (sizeof(running
) - sizeof(result
->bitsnum
)) / sizeof(DWORD
))) {
4753 remainder
= VARIANT_int_divbychar(running
, sizeof(running
) / sizeof(DWORD
), 10);
4754 if (remainder
> 0) WARN("losing significant digits (remainder %u)...\n", remainder
);
4758 /* round up the result - native oleaut32 does this */
4759 if (remainder
>= 5) {
4761 for (remainder
= 1, i
= 0; i
< sizeof(running
)/sizeof(DWORD
) && remainder
; i
++) {
4762 ULONGLONG digit
= running
[i
] + 1;
4763 remainder
= (digit
> 0xFFFFFFFF) ? 1 : 0;
4764 running
[i
] = digit
& 0xFFFFFFFF;
4768 /* Signal overflow if scale == 0 and 256-bit result still overflows,
4769 and copy result bits into result structure
4771 r_overflow
= !VARIANT_int_iszero(
4772 running
+ sizeof(result
->bitsnum
)/sizeof(DWORD
),
4773 (sizeof(running
) - sizeof(result
->bitsnum
))/sizeof(DWORD
));
4774 memcpy(result
->bitsnum
, running
, sizeof(result
->bitsnum
));
4779 /* cast DECIMAL into string. Any scale should be handled properly. en_US locale is
4780 hardcoded (period for decimal separator, dash as negative sign). Returns TRUE for
4781 success, FALSE if insufficient space in output buffer.
4783 static BOOL
VARIANT_DI_tostringW(const VARIANT_DI
* a
, WCHAR
* s
, unsigned int n
)
4785 BOOL overflow
= FALSE
;
4787 unsigned char remainder
;
4790 /* place negative sign */
4791 if (!VARIANT_int_iszero(a
->bitsnum
, sizeof(a
->bitsnum
) / sizeof(DWORD
)) && a
->sign
) {
4796 else overflow
= TRUE
;
4799 /* prepare initial 0 */
4804 } else overflow
= TRUE
;
4808 memcpy(quotient
, a
->bitsnum
, sizeof(a
->bitsnum
));
4809 while (!overflow
&& !VARIANT_int_iszero(quotient
, sizeof(quotient
) / sizeof(DWORD
))) {
4810 remainder
= VARIANT_int_divbychar(quotient
, sizeof(quotient
) / sizeof(DWORD
), 10);
4814 s
[i
++] = '0' + remainder
;
4819 if (!overflow
&& !VARIANT_int_iszero(a
->bitsnum
, sizeof(a
->bitsnum
) / sizeof(DWORD
))) {
4821 /* reverse order of digits */
4822 WCHAR
* x
= s
; WCHAR
* y
= s
+ i
- 1;
4829 /* check for decimal point. "i" now has string length */
4830 if (i
<= a
->scale
) {
4831 unsigned int numzeroes
= a
->scale
+ 1 - i
;
4832 if (i
+ 1 + numzeroes
>= n
) {
4835 memmove(s
+ numzeroes
, s
, (i
+ 1) * sizeof(WCHAR
));
4837 while (numzeroes
> 0) {
4838 s
[--numzeroes
] = '0';
4843 /* place decimal point */
4845 unsigned int periodpos
= i
- a
->scale
;
4849 memmove(s
+ periodpos
+ 1, s
+ periodpos
, (i
+ 1 - periodpos
) * sizeof(WCHAR
));
4850 s
[periodpos
] = '.'; i
++;
4852 /* remove extra zeros at the end, if any */
4853 while (s
[i
- 1] == '0') s
[--i
] = '\0';
4854 if (s
[i
- 1] == '.') s
[--i
] = '\0';
4862 /* shift the bits of a DWORD array to the left. p[0] is assumed LSB */
4863 static void VARIANT_int_shiftleft(DWORD
* p
, unsigned int n
, unsigned int shift
)
4868 /* shift whole DWORDs to the left */
4871 memmove(p
+ 1, p
, (n
- 1) * sizeof(DWORD
));
4872 *p
= 0; shift
-= 32;
4875 /* shift remainder (1..31 bits) */
4877 if (shift
> 0) for (i
= 0; i
< n
; i
++)
4880 b
= p
[i
] >> (32 - shift
);
4881 p
[i
] = (p
[i
] << shift
) | shifted
;
4886 /* add the (unsigned) numbers stored in two DWORD arrays with LSB at index 0.
4887 Value at v is incremented by the value at p. Any size is supported, provided
4888 that v is not shorter than p. Any unapplied carry is returned as a result.
4890 static unsigned char VARIANT_int_add(DWORD
* v
, unsigned int nv
, const DWORD
* p
,
4893 unsigned char carry
= 0;
4899 for (i
= 0; i
< np
; i
++) {
4900 sum
= (ULONGLONG
)v
[i
]
4903 v
[i
] = sum
& 0xffffffff;
4906 for (; i
< nv
&& carry
; i
++) {
4907 sum
= (ULONGLONG
)v
[i
]
4909 v
[i
] = sum
& 0xffffffff;
4916 /* perform integral division with operand p as dividend. Parameter n indicates
4917 number of available DWORDs in divisor p, but available space in p must be
4918 actually at least 2 * n DWORDs, because the remainder of the integral
4919 division is built in the next n DWORDs past the start of the quotient. This
4920 routine replaces the dividend in p with the quotient, and appends n
4921 additional DWORDs for the remainder.
4923 Thanks to Lee & Mark Atkinson for their book _Using_C_ (my very first book on
4924 C/C++ :-) where the "longhand binary division" algorithm was exposed for the
4925 source code to the VLI (Very Large Integer) division operator. This algorithm
4926 was then heavily modified by me (Alex Villacis Lasso) in order to handle
4927 variably-scaled integers such as the MS DECIMAL representation.
4929 static void VARIANT_int_div(DWORD
* p
, unsigned int n
, const DWORD
* divisor
,
4934 DWORD
* negdivisor
= tempsub
+ n
;
4936 /* build 2s-complement of divisor */
4937 for (i
= 0; i
< n
; i
++) negdivisor
[i
] = (i
< dn
) ? ~divisor
[i
] : 0xFFFFFFFF;
4939 VARIANT_int_add(negdivisor
, n
, p
+ n
, 1);
4940 memset(p
+ n
, 0, n
* sizeof(DWORD
));
4942 /* skip all leading zero DWORDs in quotient */
4943 for (i
= 0; i
< n
&& !p
[n
- 1]; i
++) VARIANT_int_shiftleft(p
, n
, 32);
4944 /* i is now number of DWORDs left to process */
4945 for (i
<<= 5; i
< (n
<< 5); i
++) {
4946 VARIANT_int_shiftleft(p
, n
<< 1, 1); /* shl quotient+remainder */
4948 /* trial subtraction */
4949 memcpy(tempsub
, p
+ n
, n
* sizeof(DWORD
));
4950 VARIANT_int_add(tempsub
, n
, negdivisor
, n
);
4952 /* check whether result of subtraction was negative */
4953 if ((tempsub
[n
- 1] & 0x80000000) == 0) {
4954 memcpy(p
+ n
, tempsub
, n
* sizeof(DWORD
));
4960 /* perform integral multiplication by a byte operand. Used for scaling by 10 */
4961 static unsigned char VARIANT_int_mulbychar(DWORD
* p
, unsigned int n
, unsigned char m
)
4966 for (iOverflowMul
= 0, i
= 0; i
< n
; i
++)
4967 p
[i
] = VARIANT_Mul(p
[i
], m
, &iOverflowMul
);
4968 return (unsigned char)iOverflowMul
;
4971 /* increment value in A by the value indicated in B, with scale adjusting.
4972 Modifies parameters by adjusting scales. Returns 0 if addition was
4973 successful, nonzero if a parameter underflowed before it could be
4974 successfully used in the addition.
4976 static int VARIANT_int_addlossy(
4977 DWORD
* a
, int * ascale
, unsigned int an
,
4978 DWORD
* b
, int * bscale
, unsigned int bn
)
4982 if (VARIANT_int_iszero(a
, an
)) {
4983 /* if A is zero, copy B into A, after removing digits */
4984 while (bn
> an
&& !VARIANT_int_iszero(b
+ an
, bn
- an
)) {
4985 VARIANT_int_divbychar(b
, bn
, 10);
4988 memcpy(a
, b
, an
* sizeof(DWORD
));
4990 } else if (!VARIANT_int_iszero(b
, bn
)) {
4991 unsigned int tn
= an
+ 1;
4994 if (bn
+ 1 > tn
) tn
= bn
+ 1;
4995 if (*ascale
!= *bscale
) {
4996 /* first (optimistic) try - try to scale down the one with the bigger
4997 scale, while this number is divisible by 10 */
4998 DWORD
* digitchosen
;
4999 unsigned int nchosen
;
5003 if (*ascale
< *bscale
) {
5004 targetscale
= *ascale
;
5005 scalechosen
= bscale
;
5009 targetscale
= *bscale
;
5010 scalechosen
= ascale
;
5014 memset(t
, 0, tn
* sizeof(DWORD
));
5015 memcpy(t
, digitchosen
, nchosen
* sizeof(DWORD
));
5017 /* divide by 10 until target scale is reached */
5018 while (*scalechosen
> targetscale
) {
5019 unsigned char remainder
= VARIANT_int_divbychar(t
, tn
, 10);
5022 memcpy(digitchosen
, t
, nchosen
* sizeof(DWORD
));
5027 if (*ascale
!= *bscale
) {
5028 DWORD
* digitchosen
;
5029 unsigned int nchosen
;
5033 /* try to scale up the one with the smaller scale */
5034 if (*ascale
> *bscale
) {
5035 targetscale
= *ascale
;
5036 scalechosen
= bscale
;
5040 targetscale
= *bscale
;
5041 scalechosen
= ascale
;
5045 memset(t
, 0, tn
* sizeof(DWORD
));
5046 memcpy(t
, digitchosen
, nchosen
* sizeof(DWORD
));
5048 /* multiply by 10 until target scale is reached, or
5049 significant bytes overflow the number
5051 while (*scalechosen
< targetscale
&& t
[nchosen
] == 0) {
5052 VARIANT_int_mulbychar(t
, tn
, 10);
5053 if (t
[nchosen
] == 0) {
5054 /* still does not overflow */
5056 memcpy(digitchosen
, t
, nchosen
* sizeof(DWORD
));
5061 if (*ascale
!= *bscale
) {
5062 /* still different? try to scale down the one with the bigger scale
5063 (this *will* lose significant digits) */
5064 DWORD
* digitchosen
;
5065 unsigned int nchosen
;
5069 if (*ascale
< *bscale
) {
5070 targetscale
= *ascale
;
5071 scalechosen
= bscale
;
5075 targetscale
= *bscale
;
5076 scalechosen
= ascale
;
5080 memset(t
, 0, tn
* sizeof(DWORD
));
5081 memcpy(t
, digitchosen
, nchosen
* sizeof(DWORD
));
5083 /* divide by 10 until target scale is reached */
5084 while (*scalechosen
> targetscale
) {
5085 VARIANT_int_divbychar(t
, tn
, 10);
5087 memcpy(digitchosen
, t
, nchosen
* sizeof(DWORD
));
5091 /* check whether any of the operands still has significant digits
5094 if (VARIANT_int_iszero(a
, an
) || VARIANT_int_iszero(b
, bn
)) {
5097 /* at this step, both numbers have the same scale and can be added
5098 as integers. However, the result might not fit in A, so further
5099 scaling down might be necessary.
5101 while (!underflow
) {
5102 memset(t
, 0, tn
* sizeof(DWORD
));
5103 memcpy(t
, a
, an
* sizeof(DWORD
));
5105 VARIANT_int_add(t
, tn
, b
, bn
);
5106 if (VARIANT_int_iszero(t
+ an
, tn
- an
)) {
5107 /* addition was successful */
5108 memcpy(a
, t
, an
* sizeof(DWORD
));
5111 /* addition overflowed - remove significant digits
5112 from both operands and try again */
5113 VARIANT_int_divbychar(a
, an
, 10); (*ascale
)--;
5114 VARIANT_int_divbychar(b
, bn
, 10); (*bscale
)--;
5115 /* check whether any operand keeps significant digits after
5116 scaledown (underflow case 2)
5118 underflow
= (VARIANT_int_iszero(a
, an
) || VARIANT_int_iszero(b
, bn
));
5126 /* perform complete DECIMAL division in the internal representation. Returns
5127 0 if the division was completed (even if quotient is set to 0), or nonzero
5128 in case of quotient overflow.
5130 static HRESULT
VARIANT_DI_div(const VARIANT_DI
* dividend
, const VARIANT_DI
* divisor
,
5131 VARIANT_DI
* quotient
, BOOL round_remainder
)
5133 HRESULT r_overflow
= S_OK
;
5135 if (VARIANT_int_iszero(divisor
->bitsnum
, sizeof(divisor
->bitsnum
)/sizeof(DWORD
))) {
5137 r_overflow
= DISP_E_DIVBYZERO
;
5138 } else if (VARIANT_int_iszero(dividend
->bitsnum
, sizeof(dividend
->bitsnum
)/sizeof(DWORD
))) {
5139 VARIANT_DI_clear(quotient
);
5141 int quotientscale
, remainderscale
, tempquotientscale
;
5142 DWORD remainderplusquotient
[8];
5145 quotientscale
= remainderscale
= (int)dividend
->scale
- (int)divisor
->scale
;
5146 tempquotientscale
= quotientscale
;
5147 VARIANT_DI_clear(quotient
);
5148 quotient
->sign
= (dividend
->sign
^ divisor
->sign
) ? 1 : 0;
5150 /* The following strategy is used for division
5151 1) if there was a nonzero remainder from previous iteration, use it as
5152 dividend for this iteration, else (for first iteration) use intended
5154 2) perform integer division in temporary buffer, develop quotient in
5155 low-order part, remainder in high-order part
5156 3) add quotient from step 2 to final result, with possible loss of
5158 4) multiply integer part of remainder by 10, while incrementing the
5159 scale of the remainder. This operation preserves the intended value
5161 5) loop to step 1 until one of the following is true:
5162 a) remainder is zero (exact division achieved)
5163 b) addition in step 3 fails to modify bits in quotient (remainder underflow)
5165 memset(remainderplusquotient
, 0, sizeof(remainderplusquotient
));
5166 memcpy(remainderplusquotient
, dividend
->bitsnum
, sizeof(dividend
->bitsnum
));
5169 remainderplusquotient
, 4,
5170 divisor
->bitsnum
, sizeof(divisor
->bitsnum
)/sizeof(DWORD
));
5171 underflow
= VARIANT_int_addlossy(
5172 quotient
->bitsnum
, "ientscale
, sizeof(quotient
->bitsnum
) / sizeof(DWORD
),
5173 remainderplusquotient
, &tempquotientscale
, 4);
5174 if (round_remainder
) {
5175 if(remainderplusquotient
[4] >= 5){
5177 unsigned char remainder
= 1;
5178 for (i
= 0; i
< sizeof(quotient
->bitsnum
) / sizeof(DWORD
) && remainder
; i
++) {
5179 ULONGLONG digit
= quotient
->bitsnum
[i
] + 1;
5180 remainder
= (digit
> 0xFFFFFFFF) ? 1 : 0;
5181 quotient
->bitsnum
[i
] = digit
& 0xFFFFFFFF;
5184 memset(remainderplusquotient
, 0, sizeof(remainderplusquotient
));
5186 VARIANT_int_mulbychar(remainderplusquotient
+ 4, 4, 10);
5187 memcpy(remainderplusquotient
, remainderplusquotient
+ 4, 4 * sizeof(DWORD
));
5189 tempquotientscale
= ++remainderscale
;
5190 } while (!underflow
&& !VARIANT_int_iszero(remainderplusquotient
+ 4, 4));
5192 /* quotient scale might now be negative (extremely big number). If, so, try
5193 to multiply quotient by 10 (without overflowing), while adjusting the scale,
5194 until scale is 0. If this cannot be done, it is a real overflow.
5196 while (r_overflow
== S_OK
&& quotientscale
< 0) {
5197 memset(remainderplusquotient
, 0, sizeof(remainderplusquotient
));
5198 memcpy(remainderplusquotient
, quotient
->bitsnum
, sizeof(quotient
->bitsnum
));
5199 VARIANT_int_mulbychar(remainderplusquotient
, sizeof(remainderplusquotient
)/sizeof(DWORD
), 10);
5200 if (VARIANT_int_iszero(remainderplusquotient
+ sizeof(quotient
->bitsnum
)/sizeof(DWORD
),
5201 (sizeof(remainderplusquotient
) - sizeof(quotient
->bitsnum
))/sizeof(DWORD
))) {
5203 memcpy(quotient
->bitsnum
, remainderplusquotient
, sizeof(quotient
->bitsnum
));
5204 } else r_overflow
= DISP_E_OVERFLOW
;
5206 if (r_overflow
== S_OK
) {
5207 if (quotientscale
<= 255) quotient
->scale
= quotientscale
;
5208 else VARIANT_DI_clear(quotient
);
5214 /* This procedure receives a VARIANT_DI with a defined mantissa and sign, but
5215 with an undefined scale, which will be assigned to (if possible). It also
5216 receives an exponent of 2. This procedure will then manipulate the mantissa
5217 and calculate a corresponding scale, so that the exponent2 value is assimilated
5218 into the VARIANT_DI and is therefore no longer necessary. Returns S_OK if
5219 successful, or DISP_E_OVERFLOW if the represented value is too big to fit into
5221 static HRESULT
VARIANT_DI_normalize(VARIANT_DI
* val
, int exponent2
, BOOL isDouble
)
5223 HRESULT hres
= S_OK
;
5224 int exponent5
, exponent10
;
5226 /* A factor of 2^exponent2 is equivalent to (10^exponent2)/(5^exponent2), and
5227 thus equal to (5^-exponent2)*(10^exponent2). After all manipulations,
5228 exponent10 might be used to set the VARIANT_DI scale directly. However,
5229 the value of 5^-exponent5 must be assimilated into the VARIANT_DI. */
5230 exponent5
= -exponent2
;
5231 exponent10
= exponent2
;
5233 /* Handle exponent5 > 0 */
5234 while (exponent5
> 0) {
5238 /* In order to multiply the value represented by the VARIANT_DI by 5, it
5239 is best to multiply by 10/2. Therefore, exponent10 is incremented, and
5240 somehow the mantissa should be divided by 2. */
5241 if ((val
->bitsnum
[0] & 1) == 0) {
5242 /* The mantissa is divisible by 2. Therefore the division can be done
5243 without losing significant digits. */
5244 exponent10
++; exponent5
--;
5247 bPrevCarryBit
= val
->bitsnum
[2] & 1;
5248 val
->bitsnum
[2] >>= 1;
5249 bCurrCarryBit
= val
->bitsnum
[1] & 1;
5250 val
->bitsnum
[1] = (val
->bitsnum
[1] >> 1) | (bPrevCarryBit
? 0x80000000 : 0);
5251 val
->bitsnum
[0] = (val
->bitsnum
[0] >> 1) | (bCurrCarryBit
? 0x80000000 : 0);
5253 /* The mantissa is NOT divisible by 2. Therefore the mantissa should
5254 be multiplied by 5, unless the multiplication overflows. */
5255 DWORD temp_bitsnum
[3];
5259 memcpy(temp_bitsnum
, val
->bitsnum
, 3 * sizeof(DWORD
));
5260 if (0 == VARIANT_int_mulbychar(temp_bitsnum
, 3, 5)) {
5261 /* Multiplication succeeded without overflow, so copy result back
5263 memcpy(val
->bitsnum
, temp_bitsnum
, 3 * sizeof(DWORD
));
5265 /* Mask out 3 extraneous bits introduced by the multiply */
5267 /* Multiplication by 5 overflows. The mantissa should be divided
5268 by 2, and therefore will lose significant digits. */
5272 bPrevCarryBit
= val
->bitsnum
[2] & 1;
5273 val
->bitsnum
[2] >>= 1;
5274 bCurrCarryBit
= val
->bitsnum
[1] & 1;
5275 val
->bitsnum
[1] = (val
->bitsnum
[1] >> 1) | (bPrevCarryBit
? 0x80000000 : 0);
5276 val
->bitsnum
[0] = (val
->bitsnum
[0] >> 1) | (bCurrCarryBit
? 0x80000000 : 0);
5281 /* Handle exponent5 < 0 */
5282 while (exponent5
< 0) {
5283 /* In order to divide the value represented by the VARIANT_DI by 5, it
5284 is best to multiply by 2/10. Therefore, exponent10 is decremented,
5285 and the mantissa should be multiplied by 2 */
5286 if ((val
->bitsnum
[2] & 0x80000000) == 0) {
5287 /* The mantissa can withstand a shift-left without overflowing */
5288 exponent10
--; exponent5
++;
5289 VARIANT_int_shiftleft(val
->bitsnum
, 3, 1);
5291 /* The mantissa would overflow if shifted. Therefore it should be
5292 directly divided by 5. This will lose significant digits, unless
5293 by chance the mantissa happens to be divisible by 5 */
5295 VARIANT_int_divbychar(val
->bitsnum
, 3, 5);
5299 /* At this point, the mantissa has assimilated the exponent5, but the
5300 exponent10 might not be suitable for assignment. The exponent10 must be
5301 in the range [-DEC_MAX_SCALE..0], so the mantissa must be scaled up or
5302 down appropriately. */
5303 while (hres
== S_OK
&& exponent10
> 0) {
5304 /* In order to bring exponent10 down to 0, the mantissa should be
5305 multiplied by 10 to compensate. If the exponent10 is too big, this
5306 will cause the mantissa to overflow. */
5307 if (0 == VARIANT_int_mulbychar(val
->bitsnum
, 3, 10)) {
5310 hres
= DISP_E_OVERFLOW
;
5313 while (exponent10
< -DEC_MAX_SCALE
) {
5315 /* In order to bring exponent up to -DEC_MAX_SCALE, the mantissa should
5316 be divided by 10 to compensate. If the exponent10 is too small, this
5317 will cause the mantissa to underflow and become 0 */
5318 rem10
= VARIANT_int_divbychar(val
->bitsnum
, 3, 10);
5320 if (VARIANT_int_iszero(val
->bitsnum
, 3)) {
5321 /* Underflow, unable to keep dividing */
5323 } else if (rem10
>= 5) {
5325 VARIANT_int_add(val
->bitsnum
, 3, &x
, 1);
5328 /* This step is required in order to remove excess bits of precision from the
5329 end of the bit representation, down to the precision guaranteed by the
5330 floating point number. */
5332 while (exponent10
< 0 && (val
->bitsnum
[2] != 0 || (val
->bitsnum
[2] == 0 && (val
->bitsnum
[1] & 0xFFE00000) != 0))) {
5335 rem10
= VARIANT_int_divbychar(val
->bitsnum
, 3, 10);
5339 VARIANT_int_add(val
->bitsnum
, 3, &x
, 1);
5343 while (exponent10
< 0 && (val
->bitsnum
[2] != 0 || val
->bitsnum
[1] != 0 ||
5344 (val
->bitsnum
[2] == 0 && val
->bitsnum
[1] == 0 && (val
->bitsnum
[0] & 0xFF000000) != 0))) {
5347 rem10
= VARIANT_int_divbychar(val
->bitsnum
, 3, 10);
5351 VARIANT_int_add(val
->bitsnum
, 3, &x
, 1);
5355 /* Remove multiples of 10 from the representation */
5356 while (exponent10
< 0) {
5357 DWORD temp_bitsnum
[3];
5359 memcpy(temp_bitsnum
, val
->bitsnum
, 3 * sizeof(DWORD
));
5360 if (0 == VARIANT_int_divbychar(temp_bitsnum
, 3, 10)) {
5362 memcpy(val
->bitsnum
, temp_bitsnum
, 3 * sizeof(DWORD
));
5366 /* Scale assignment */
5367 if (hres
== S_OK
) val
->scale
= -exponent10
;
5376 unsigned int m
: 23;
5377 unsigned int exp_bias
: 8;
5378 unsigned int sign
: 1;
5383 /* Convert a 32-bit floating point number into a DECIMAL, without using an
5384 intermediate string step. */
5385 static HRESULT
VARIANT_DI_FromR4(float source
, VARIANT_DI
* dest
)
5387 HRESULT hres
= S_OK
;
5392 /* Detect special cases */
5393 if (fx
.i
.m
== 0 && fx
.i
.exp_bias
== 0) {
5394 /* Floating-point zero */
5395 VARIANT_DI_clear(dest
);
5396 } else if (fx
.i
.m
== 0 && fx
.i
.exp_bias
== 0xFF) {
5397 /* Floating-point infinity */
5398 hres
= DISP_E_OVERFLOW
;
5399 } else if (fx
.i
.exp_bias
== 0xFF) {
5400 /* Floating-point NaN */
5401 hres
= DISP_E_BADVARTYPE
;
5404 VARIANT_DI_clear(dest
);
5406 exponent2
= fx
.i
.exp_bias
- 127; /* Get unbiased exponent */
5407 dest
->sign
= fx
.i
.sign
; /* Sign is simply copied */
5409 /* Copy significant bits to VARIANT_DI mantissa */
5410 dest
->bitsnum
[0] = fx
.i
.m
;
5411 dest
->bitsnum
[0] &= 0x007FFFFF;
5412 if (fx
.i
.exp_bias
== 0) {
5413 /* Denormalized number - correct exponent */
5416 /* Add hidden bit to mantissa */
5417 dest
->bitsnum
[0] |= 0x00800000;
5420 /* The act of copying a FP mantissa as integer bits is equivalent to
5421 shifting left the mantissa 23 bits. The exponent2 is reduced to
5425 hres
= VARIANT_DI_normalize(dest
, exponent2
, FALSE
);
5435 unsigned int m_lo
: 32; /* 52 bits of precision */
5436 unsigned int m_hi
: 20;
5437 unsigned int exp_bias
: 11; /* bias == 1023 */
5438 unsigned int sign
: 1;
5443 /* Convert a 64-bit floating point number into a DECIMAL, without using an
5444 intermediate string step. */
5445 static HRESULT
VARIANT_DI_FromR8(double source
, VARIANT_DI
* dest
)
5447 HRESULT hres
= S_OK
;
5452 /* Detect special cases */
5453 if (fx
.i
.m_lo
== 0 && fx
.i
.m_hi
== 0 && fx
.i
.exp_bias
== 0) {
5454 /* Floating-point zero */
5455 VARIANT_DI_clear(dest
);
5456 } else if (fx
.i
.m_lo
== 0 && fx
.i
.m_hi
== 0 && fx
.i
.exp_bias
== 0x7FF) {
5457 /* Floating-point infinity */
5458 hres
= DISP_E_OVERFLOW
;
5459 } else if (fx
.i
.exp_bias
== 0x7FF) {
5460 /* Floating-point NaN */
5461 hres
= DISP_E_BADVARTYPE
;
5464 VARIANT_DI_clear(dest
);
5466 exponent2
= fx
.i
.exp_bias
- 1023; /* Get unbiased exponent */
5467 dest
->sign
= fx
.i
.sign
; /* Sign is simply copied */
5469 /* Copy significant bits to VARIANT_DI mantissa */
5470 dest
->bitsnum
[0] = fx
.i
.m_lo
;
5471 dest
->bitsnum
[1] = fx
.i
.m_hi
;
5472 dest
->bitsnum
[1] &= 0x000FFFFF;
5473 if (fx
.i
.exp_bias
== 0) {
5474 /* Denormalized number - correct exponent */
5477 /* Add hidden bit to mantissa */
5478 dest
->bitsnum
[1] |= 0x00100000;
5481 /* The act of copying a FP mantissa as integer bits is equivalent to
5482 shifting left the mantissa 52 bits. The exponent2 is reduced to
5486 hres
= VARIANT_DI_normalize(dest
, exponent2
, TRUE
);
5492 static HRESULT
VARIANT_do_division(const DECIMAL
*pDecLeft
, const DECIMAL
*pDecRight
, DECIMAL
*pDecOut
,
5495 HRESULT hRet
= S_OK
;
5496 VARIANT_DI di_left
, di_right
, di_result
;
5499 VARIANT_DIFromDec(pDecLeft
, &di_left
);
5500 VARIANT_DIFromDec(pDecRight
, &di_right
);
5501 divresult
= VARIANT_DI_div(&di_left
, &di_right
, &di_result
, round
);
5502 if (divresult
!= S_OK
)
5504 /* division actually overflowed */
5511 if (di_result
.scale
> DEC_MAX_SCALE
)
5513 unsigned char remainder
= 0;
5515 /* division underflowed. In order to comply with the MSDN
5516 specifications for DECIMAL ranges, some significant digits
5519 WARN("result scale is %u, scaling (with loss of significant digits)...\n",
5521 while (di_result
.scale
> DEC_MAX_SCALE
&&
5522 !VARIANT_int_iszero(di_result
.bitsnum
, sizeof(di_result
.bitsnum
) / sizeof(DWORD
)))
5524 remainder
= VARIANT_int_divbychar(di_result
.bitsnum
, sizeof(di_result
.bitsnum
) / sizeof(DWORD
), 10);
5527 if (di_result
.scale
> DEC_MAX_SCALE
)
5529 WARN("result underflowed, setting to 0\n");
5530 di_result
.scale
= 0;
5533 else if (remainder
>= 5) /* round up result - native oleaut32 does this */
5536 for (remainder
= 1, i
= 0; i
< sizeof(di_result
.bitsnum
) / sizeof(DWORD
) && remainder
; i
++) {
5537 ULONGLONG digit
= di_result
.bitsnum
[i
] + 1;
5538 remainder
= (digit
> 0xFFFFFFFF) ? 1 : 0;
5539 di_result
.bitsnum
[i
] = digit
& 0xFFFFFFFF;
5543 VARIANT_DecFromDI(&di_result
, pDecOut
);
5548 /************************************************************************
5549 * VarDecDiv (OLEAUT32.178)
5551 * Divide one DECIMAL by another.
5554 * pDecLeft [I] Source
5555 * pDecRight [I] Value to divide by
5556 * pDecOut [O] Destination
5560 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
5562 HRESULT WINAPI
VarDecDiv(const DECIMAL
* pDecLeft
, const DECIMAL
* pDecRight
, DECIMAL
* pDecOut
)
5564 if (!pDecLeft
|| !pDecRight
|| !pDecOut
) return E_INVALIDARG
;
5566 return VARIANT_do_division(pDecLeft
, pDecRight
, pDecOut
, FALSE
);
5569 /************************************************************************
5570 * VarDecMul (OLEAUT32.179)
5572 * Multiply one DECIMAL by another.
5575 * pDecLeft [I] Source
5576 * pDecRight [I] Value to multiply by
5577 * pDecOut [O] Destination
5581 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
5583 HRESULT WINAPI
VarDecMul(const DECIMAL
* pDecLeft
, const DECIMAL
* pDecRight
, DECIMAL
* pDecOut
)
5585 HRESULT hRet
= S_OK
;
5586 VARIANT_DI di_left
, di_right
, di_result
;
5589 VARIANT_DIFromDec(pDecLeft
, &di_left
);
5590 VARIANT_DIFromDec(pDecRight
, &di_right
);
5591 mulresult
= VARIANT_DI_mul(&di_left
, &di_right
, &di_result
);
5594 /* multiplication actually overflowed */
5595 hRet
= DISP_E_OVERFLOW
;
5599 if (di_result
.scale
> DEC_MAX_SCALE
)
5601 /* multiplication underflowed. In order to comply with the MSDN
5602 specifications for DECIMAL ranges, some significant digits
5605 WARN("result scale is %u, scaling (with loss of significant digits)...\n",
5607 while (di_result
.scale
> DEC_MAX_SCALE
&&
5608 !VARIANT_int_iszero(di_result
.bitsnum
, sizeof(di_result
.bitsnum
)/sizeof(DWORD
)))
5610 VARIANT_int_divbychar(di_result
.bitsnum
, sizeof(di_result
.bitsnum
)/sizeof(DWORD
), 10);
5613 if (di_result
.scale
> DEC_MAX_SCALE
)
5615 WARN("result underflowed, setting to 0\n");
5616 di_result
.scale
= 0;
5620 VARIANT_DecFromDI(&di_result
, pDecOut
);
5625 /************************************************************************
5626 * VarDecSub (OLEAUT32.181)
5628 * Subtract one DECIMAL from another.
5631 * pDecLeft [I] Source
5632 * pDecRight [I] DECIMAL to subtract from pDecLeft
5633 * pDecOut [O] Destination
5636 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
5638 HRESULT WINAPI
VarDecSub(const DECIMAL
* pDecLeft
, const DECIMAL
* pDecRight
, DECIMAL
* pDecOut
)
5642 /* Implement as addition of the negative */
5643 VarDecNeg(pDecRight
, &decRight
);
5644 return VarDecAdd(pDecLeft
, &decRight
, pDecOut
);
5647 /************************************************************************
5648 * VarDecAbs (OLEAUT32.182)
5650 * Convert a DECIMAL into its absolute value.
5654 * pDecOut [O] Destination
5657 * S_OK. This function does not fail.
5659 HRESULT WINAPI
VarDecAbs(const DECIMAL
* pDecIn
, DECIMAL
* pDecOut
)
5662 DEC_SIGN(pDecOut
) &= ~DECIMAL_NEG
;
5666 /************************************************************************
5667 * VarDecFix (OLEAUT32.187)
5669 * Return the integer portion of a DECIMAL.
5673 * pDecOut [O] Destination
5677 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
5680 * - The difference between this function and VarDecInt() is that VarDecInt() rounds
5681 * negative numbers away from 0, while this function rounds them towards zero.
5683 HRESULT WINAPI
VarDecFix(const DECIMAL
* pDecIn
, DECIMAL
* pDecOut
)
5688 if (DEC_SIGN(pDecIn
) & ~DECIMAL_NEG
)
5689 return E_INVALIDARG
;
5691 if (!DEC_SCALE(pDecIn
))
5693 *pDecOut
= *pDecIn
; /* Already an integer */
5697 hr
= VarR8FromDec(pDecIn
, &dbl
);
5698 if (SUCCEEDED(hr
)) {
5699 LONGLONG rounded
= dbl
;
5701 hr
= VarDecFromI8(rounded
, pDecOut
);
5706 /************************************************************************
5707 * VarDecInt (OLEAUT32.188)
5709 * Return the integer portion of a DECIMAL.
5713 * pDecOut [O] Destination
5717 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
5720 * - The difference between this function and VarDecFix() is that VarDecFix() rounds
5721 * negative numbers towards 0, while this function rounds them away from zero.
5723 HRESULT WINAPI
VarDecInt(const DECIMAL
* pDecIn
, DECIMAL
* pDecOut
)
5728 if (DEC_SIGN(pDecIn
) & ~DECIMAL_NEG
)
5729 return E_INVALIDARG
;
5731 if (!(DEC_SIGN(pDecIn
) & DECIMAL_NEG
) || !DEC_SCALE(pDecIn
))
5732 return VarDecFix(pDecIn
, pDecOut
); /* The same, if +ve or no fractionals */
5734 hr
= VarR8FromDec(pDecIn
, &dbl
);
5735 if (SUCCEEDED(hr
)) {
5736 LONGLONG rounded
= dbl
>= 0.0 ? dbl
+ 0.5 : dbl
- 0.5;
5738 hr
= VarDecFromI8(rounded
, pDecOut
);
5743 /************************************************************************
5744 * VarDecNeg (OLEAUT32.189)
5746 * Change the sign of a DECIMAL.
5750 * pDecOut [O] Destination
5753 * S_OK. This function does not fail.
5755 HRESULT WINAPI
VarDecNeg(const DECIMAL
* pDecIn
, DECIMAL
* pDecOut
)
5758 DEC_SIGN(pDecOut
) ^= DECIMAL_NEG
;
5762 /************************************************************************
5763 * VarDecRound (OLEAUT32.203)
5765 * Change the precision of a DECIMAL.
5769 * cDecimals [I] New number of decimals to keep
5770 * pDecOut [O] Destination
5773 * Success: S_OK. pDecOut contains the rounded value.
5774 * Failure: E_INVALIDARG if any argument is invalid.
5776 HRESULT WINAPI
VarDecRound(const DECIMAL
* pDecIn
, int cDecimals
, DECIMAL
* pDecOut
)
5778 DECIMAL divisor
, tmp
;
5782 if (cDecimals
< 0 || (DEC_SIGN(pDecIn
) & ~DECIMAL_NEG
) || DEC_SCALE(pDecIn
) > DEC_MAX_SCALE
)
5783 return E_INVALIDARG
;
5785 if (cDecimals
>= DEC_SCALE(pDecIn
))
5787 *pDecOut
= *pDecIn
; /* More precision than we have */
5791 /* truncate significant digits and rescale */
5792 memset(&divisor
, 0, sizeof(divisor
));
5793 DEC_LO64(&divisor
) = 1;
5795 memset(&tmp
, 0, sizeof(tmp
));
5796 DEC_LO64(&tmp
) = 10;
5797 for (i
= 0; i
< DEC_SCALE(pDecIn
) - cDecimals
; ++i
)
5799 hr
= VarDecMul(&divisor
, &tmp
, &divisor
);
5804 hr
= VARIANT_do_division(pDecIn
, &divisor
, pDecOut
, TRUE
);
5808 DEC_SCALE(pDecOut
) = cDecimals
;
5813 /************************************************************************
5814 * VarDecCmp (OLEAUT32.204)
5816 * Compare two DECIMAL values.
5819 * pDecLeft [I] Source
5820 * pDecRight [I] Value to compare
5823 * Success: VARCMP_LT, VARCMP_EQ or VARCMP_GT indicating that pDecLeft
5824 * is less than, equal to or greater than pDecRight respectively.
5825 * Failure: DISP_E_OVERFLOW, if overflow occurs during the comparison
5827 HRESULT WINAPI
VarDecCmp(const DECIMAL
* pDecLeft
, const DECIMAL
* pDecRight
)
5832 if (!pDecLeft
|| !pDecRight
)
5835 if ((!(DEC_SIGN(pDecLeft
) & DECIMAL_NEG
)) && (DEC_SIGN(pDecRight
) & DECIMAL_NEG
) &&
5836 (DEC_HI32(pDecLeft
) | DEC_MID32(pDecLeft
) | DEC_LO32(pDecLeft
)))
5838 else if ((DEC_SIGN(pDecLeft
) & DECIMAL_NEG
) && (!(DEC_SIGN(pDecRight
) & DECIMAL_NEG
)) &&
5839 (DEC_HI32(pDecLeft
) | DEC_MID32(pDecLeft
) | DEC_LO32(pDecLeft
)))
5842 /* Subtract right from left, and compare the result to 0 */
5843 hRet
= VarDecSub(pDecLeft
, pDecRight
, &result
);
5845 if (SUCCEEDED(hRet
))
5847 int non_zero
= DEC_HI32(&result
) | DEC_MID32(&result
) | DEC_LO32(&result
);
5849 if ((DEC_SIGN(&result
) & DECIMAL_NEG
) && non_zero
)
5850 hRet
= (HRESULT
)VARCMP_LT
;
5852 hRet
= (HRESULT
)VARCMP_GT
;
5854 hRet
= (HRESULT
)VARCMP_EQ
;
5859 /************************************************************************
5860 * VarDecCmpR8 (OLEAUT32.298)
5862 * Compare a DECIMAL to a double
5865 * pDecLeft [I] DECIMAL Source
5866 * dblRight [I] double to compare to pDecLeft
5869 * Success: VARCMP_LT, VARCMP_EQ or VARCMP_GT indicating that dblRight
5870 * is less than, equal to or greater than pDecLeft respectively.
5871 * Failure: DISP_E_OVERFLOW, if overflow occurs during the comparison
5873 HRESULT WINAPI
VarDecCmpR8(const DECIMAL
* pDecLeft
, double dblRight
)
5878 hRet
= VarDecFromR8(dblRight
, &decRight
);
5880 if (SUCCEEDED(hRet
))
5881 hRet
= VarDecCmp(pDecLeft
, &decRight
);
5889 /************************************************************************
5890 * VarBoolFromUI1 (OLEAUT32.118)
5892 * Convert a VT_UI1 to a VT_BOOL.
5896 * pBoolOut [O] Destination
5901 HRESULT WINAPI
VarBoolFromUI1(BYTE bIn
, VARIANT_BOOL
*pBoolOut
)
5903 *pBoolOut
= bIn
? VARIANT_TRUE
: VARIANT_FALSE
;
5907 /************************************************************************
5908 * VarBoolFromI2 (OLEAUT32.119)
5910 * Convert a VT_I2 to a VT_BOOL.
5914 * pBoolOut [O] Destination
5919 HRESULT WINAPI
VarBoolFromI2(SHORT sIn
, VARIANT_BOOL
*pBoolOut
)
5921 *pBoolOut
= sIn
? VARIANT_TRUE
: VARIANT_FALSE
;
5925 /************************************************************************
5926 * VarBoolFromI4 (OLEAUT32.120)
5928 * Convert a VT_I4 to a VT_BOOL.
5932 * pBoolOut [O] Destination
5937 HRESULT WINAPI
VarBoolFromI4(LONG lIn
, VARIANT_BOOL
*pBoolOut
)
5939 *pBoolOut
= lIn
? VARIANT_TRUE
: VARIANT_FALSE
;
5943 /************************************************************************
5944 * VarBoolFromR4 (OLEAUT32.121)
5946 * Convert a VT_R4 to a VT_BOOL.
5950 * pBoolOut [O] Destination
5955 HRESULT WINAPI
VarBoolFromR4(FLOAT fltIn
, VARIANT_BOOL
*pBoolOut
)
5957 *pBoolOut
= fltIn
? VARIANT_TRUE
: VARIANT_FALSE
;
5961 /************************************************************************
5962 * VarBoolFromR8 (OLEAUT32.122)
5964 * Convert a VT_R8 to a VT_BOOL.
5968 * pBoolOut [O] Destination
5973 HRESULT WINAPI
VarBoolFromR8(double dblIn
, VARIANT_BOOL
*pBoolOut
)
5975 *pBoolOut
= dblIn
? VARIANT_TRUE
: VARIANT_FALSE
;
5979 /************************************************************************
5980 * VarBoolFromDate (OLEAUT32.123)
5982 * Convert a VT_DATE to a VT_BOOL.
5986 * pBoolOut [O] Destination
5991 HRESULT WINAPI
VarBoolFromDate(DATE dateIn
, VARIANT_BOOL
*pBoolOut
)
5993 *pBoolOut
= dateIn
? VARIANT_TRUE
: VARIANT_FALSE
;
5997 /************************************************************************
5998 * VarBoolFromCy (OLEAUT32.124)
6000 * Convert a VT_CY to a VT_BOOL.
6004 * pBoolOut [O] Destination
6009 HRESULT WINAPI
VarBoolFromCy(CY cyIn
, VARIANT_BOOL
*pBoolOut
)
6011 *pBoolOut
= cyIn
.int64
? VARIANT_TRUE
: VARIANT_FALSE
;
6015 /************************************************************************
6016 * VARIANT_GetLocalisedText [internal]
6018 * Get a localized string from the resources
6021 BOOL
VARIANT_GetLocalisedText(LANGID langId
, DWORD dwId
, WCHAR
*lpszDest
)
6025 hrsrc
= FindResourceExW( hProxyDll
, (LPWSTR
)RT_STRING
,
6026 MAKEINTRESOURCEW((dwId
>> 4) + 1), langId
);
6029 HGLOBAL hmem
= LoadResource( hProxyDll
, hrsrc
);
6036 p
= LockResource( hmem
);
6037 for (i
= 0; i
< (dwId
& 0x0f); i
++) p
+= *p
+ 1;
6039 memcpy( lpszDest
, p
+ 1, *p
* sizeof(WCHAR
) );
6040 lpszDest
[*p
] = '\0';
6041 TRACE("got %s for LANGID %08x\n", debugstr_w(lpszDest
), langId
);
6048 /************************************************************************
6049 * VarBoolFromStr (OLEAUT32.125)
6051 * Convert a VT_BSTR to a VT_BOOL.
6055 * lcid [I] LCID for the conversion
6056 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
6057 * pBoolOut [O] Destination
6061 * Failure: E_INVALIDARG, if pBoolOut is invalid.
6062 * DISP_E_TYPEMISMATCH, if the type cannot be converted
6065 * - strIn will be recognised if it contains "#TRUE#" or "#FALSE#". Additionally,
6066 * it may contain (in any case mapping) the text "true" or "false".
6067 * - If dwFlags includes VAR_LOCALBOOL, then the text may also match the
6068 * localised text of "True" or "False" in the language specified by lcid.
6069 * - If none of these matches occur, the string is treated as a numeric string
6070 * and the boolean pBoolOut will be set according to whether the number is zero
6071 * or not. The dwFlags parameter is passed to VarR8FromStr() for this conversion.
6072 * - If the text is not numeric and does not match any of the above, then
6073 * DISP_E_TYPEMISMATCH is returned.
6075 HRESULT WINAPI
VarBoolFromStr(OLECHAR
* strIn
, LCID lcid
, ULONG dwFlags
, VARIANT_BOOL
*pBoolOut
)
6077 /* Any VB/VBA programmers out there should recognise these strings... */
6078 static const WCHAR szFalse
[] = { '#','F','A','L','S','E','#','\0' };
6079 static const WCHAR szTrue
[] = { '#','T','R','U','E','#','\0' };
6081 LANGID langId
= MAKELANGID(LANG_ENGLISH
, SUBLANG_DEFAULT
);
6082 HRESULT hRes
= S_OK
;
6084 if (!strIn
|| !pBoolOut
)
6085 return DISP_E_TYPEMISMATCH
;
6087 /* Check if we should be comparing against localised text */
6088 if (dwFlags
& VAR_LOCALBOOL
)
6090 /* Convert our LCID into a usable value */
6091 lcid
= ConvertDefaultLocale(lcid
);
6093 langId
= LANGIDFROMLCID(lcid
);
6095 if (PRIMARYLANGID(langId
) == LANG_NEUTRAL
)
6096 langId
= MAKELANGID(LANG_ENGLISH
, SUBLANG_DEFAULT
);
6098 /* Note: Native oleaut32 always copies strIn and maps halfwidth characters.
6099 * I don't think this is needed unless any of the localised text strings
6100 * contain characters that can be so mapped. In the event that this is
6101 * true for a given language (possibly some Asian languages), then strIn
6102 * should be mapped here _only_ if langId is an Id for which this can occur.
6106 /* Note that if we are not comparing against localised strings, langId
6107 * will have its default value of LANG_ENGLISH. This allows us to mimic
6108 * the native behaviour of always checking against English strings even
6109 * after we've checked for localised ones.
6111 VarBoolFromStr_CheckLocalised
:
6112 if (VARIANT_GetLocalisedText(langId
, IDS_TRUE
, szBuff
))
6114 /* Compare against localised strings, ignoring case */
6115 if (!strcmpiW(strIn
, szBuff
))
6117 *pBoolOut
= VARIANT_TRUE
; /* Matched localised 'true' text */
6120 VARIANT_GetLocalisedText(langId
, IDS_FALSE
, szBuff
);
6121 if (!strcmpiW(strIn
, szBuff
))
6123 *pBoolOut
= VARIANT_FALSE
; /* Matched localised 'false' text */
6128 if (langId
!= MAKELANGID(LANG_ENGLISH
, SUBLANG_DEFAULT
))
6130 /* We have checked the localised text, now check English */
6131 langId
= MAKELANGID(LANG_ENGLISH
, SUBLANG_DEFAULT
);
6132 goto VarBoolFromStr_CheckLocalised
;
6135 /* All checks against localised text have failed, try #TRUE#/#FALSE# */
6136 if (!strcmpW(strIn
, szFalse
))
6137 *pBoolOut
= VARIANT_FALSE
;
6138 else if (!strcmpW(strIn
, szTrue
))
6139 *pBoolOut
= VARIANT_TRUE
;
6144 /* If this string is a number, convert it as one */
6145 hRes
= VarR8FromStr(strIn
, lcid
, dwFlags
, &d
);
6146 if (SUCCEEDED(hRes
)) *pBoolOut
= d
? VARIANT_TRUE
: VARIANT_FALSE
;
6151 /************************************************************************
6152 * VarBoolFromDisp (OLEAUT32.126)
6154 * Convert a VT_DISPATCH to a VT_BOOL.
6157 * pdispIn [I] Source
6158 * lcid [I] LCID for conversion
6159 * pBoolOut [O] Destination
6163 * Failure: E_INVALIDARG, if the source value is invalid
6164 * DISP_E_OVERFLOW, if the value will not fit in the destination
6165 * DISP_E_TYPEMISMATCH, if the type cannot be converted
6167 HRESULT WINAPI
VarBoolFromDisp(IDispatch
* pdispIn
, LCID lcid
, VARIANT_BOOL
*pBoolOut
)
6169 return VARIANT_FromDisp(pdispIn
, lcid
, pBoolOut
, VT_BOOL
, 0);
6172 /************************************************************************
6173 * VarBoolFromI1 (OLEAUT32.233)
6175 * Convert a VT_I1 to a VT_BOOL.
6179 * pBoolOut [O] Destination
6184 HRESULT WINAPI
VarBoolFromI1(signed char cIn
, VARIANT_BOOL
*pBoolOut
)
6186 *pBoolOut
= cIn
? VARIANT_TRUE
: VARIANT_FALSE
;
6190 /************************************************************************
6191 * VarBoolFromUI2 (OLEAUT32.234)
6193 * Convert a VT_UI2 to a VT_BOOL.
6197 * pBoolOut [O] Destination
6202 HRESULT WINAPI
VarBoolFromUI2(USHORT usIn
, VARIANT_BOOL
*pBoolOut
)
6204 *pBoolOut
= usIn
? VARIANT_TRUE
: VARIANT_FALSE
;
6208 /************************************************************************
6209 * VarBoolFromUI4 (OLEAUT32.235)
6211 * Convert a VT_UI4 to a VT_BOOL.
6215 * pBoolOut [O] Destination
6220 HRESULT WINAPI
VarBoolFromUI4(ULONG ulIn
, VARIANT_BOOL
*pBoolOut
)
6222 *pBoolOut
= ulIn
? VARIANT_TRUE
: VARIANT_FALSE
;
6226 /************************************************************************
6227 * VarBoolFromDec (OLEAUT32.236)
6229 * Convert a VT_DECIMAL to a VT_BOOL.
6233 * pBoolOut [O] Destination
6237 * Failure: E_INVALIDARG, if pDecIn is invalid.
6239 HRESULT WINAPI
VarBoolFromDec(DECIMAL
* pDecIn
, VARIANT_BOOL
*pBoolOut
)
6241 if (DEC_SCALE(pDecIn
) > DEC_MAX_SCALE
|| (DEC_SIGN(pDecIn
) & ~DECIMAL_NEG
))
6242 return E_INVALIDARG
;
6244 if (DEC_HI32(pDecIn
) || DEC_MID32(pDecIn
) || DEC_LO32(pDecIn
))
6245 *pBoolOut
= VARIANT_TRUE
;
6247 *pBoolOut
= VARIANT_FALSE
;
6251 /************************************************************************
6252 * VarBoolFromI8 (OLEAUT32.370)
6254 * Convert a VT_I8 to a VT_BOOL.
6258 * pBoolOut [O] Destination
6263 HRESULT WINAPI
VarBoolFromI8(LONG64 llIn
, VARIANT_BOOL
*pBoolOut
)
6265 *pBoolOut
= llIn
? VARIANT_TRUE
: VARIANT_FALSE
;
6269 /************************************************************************
6270 * VarBoolFromUI8 (OLEAUT32.371)
6272 * Convert a VT_UI8 to a VT_BOOL.
6276 * pBoolOut [O] Destination
6281 HRESULT WINAPI
VarBoolFromUI8(ULONG64 ullIn
, VARIANT_BOOL
*pBoolOut
)
6283 *pBoolOut
= ullIn
? VARIANT_TRUE
: VARIANT_FALSE
;
6290 /* Write a number from a UI8 and sign */
6291 static WCHAR
*VARIANT_WriteNumber(ULONG64 ulVal
, WCHAR
* szOut
)
6295 WCHAR ulNextDigit
= ulVal
% 10;
6297 *szOut
-- = '0' + ulNextDigit
;
6298 ulVal
= (ulVal
- ulNextDigit
) / 10;
6305 /* Create a (possibly localised) BSTR from a UI8 and sign */
6306 static BSTR
VARIANT_MakeBstr(LCID lcid
, DWORD dwFlags
, WCHAR
*szOut
)
6308 WCHAR szConverted
[256];
6310 if (dwFlags
& VAR_NEGATIVE
)
6313 if (dwFlags
& LOCALE_USE_NLS
)
6315 /* Format the number for the locale */
6316 szConverted
[0] = '\0';
6317 GetNumberFormatW(lcid
,
6318 dwFlags
& LOCALE_NOUSEROVERRIDE
,
6319 szOut
, NULL
, szConverted
, sizeof(szConverted
)/sizeof(WCHAR
));
6320 szOut
= szConverted
;
6322 return SysAllocStringByteLen((LPCSTR
)szOut
, strlenW(szOut
) * sizeof(WCHAR
));
6325 /* Create a (possibly localised) BSTR from a UI8 and sign */
6326 static HRESULT
VARIANT_BstrFromUInt(ULONG64 ulVal
, LCID lcid
, DWORD dwFlags
, BSTR
*pbstrOut
)
6328 WCHAR szBuff
[64], *szOut
= szBuff
+ sizeof(szBuff
)/sizeof(WCHAR
) - 1;
6331 return E_INVALIDARG
;
6333 /* Create the basic number string */
6335 szOut
= VARIANT_WriteNumber(ulVal
, szOut
);
6337 *pbstrOut
= VARIANT_MakeBstr(lcid
, dwFlags
, szOut
);
6338 TRACE("returning %s\n", debugstr_w(*pbstrOut
));
6339 return *pbstrOut
? S_OK
: E_OUTOFMEMORY
;
6342 /******************************************************************************
6343 * VarBstrFromUI1 (OLEAUT32.108)
6345 * Convert a VT_UI1 to a VT_BSTR.
6349 * lcid [I] LCID for the conversion
6350 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
6351 * pbstrOut [O] Destination
6355 * Failure: E_INVALIDARG, if pbstrOut is invalid.
6356 * E_OUTOFMEMORY, if memory allocation fails.
6358 HRESULT WINAPI
VarBstrFromUI1(BYTE bIn
, LCID lcid
, ULONG dwFlags
, BSTR
* pbstrOut
)
6360 return VARIANT_BstrFromUInt(bIn
, lcid
, dwFlags
, pbstrOut
);
6363 /******************************************************************************
6364 * VarBstrFromI2 (OLEAUT32.109)
6366 * Convert a VT_I2 to a VT_BSTR.
6370 * lcid [I] LCID for the conversion
6371 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
6372 * pbstrOut [O] Destination
6376 * Failure: E_INVALIDARG, if pbstrOut is invalid.
6377 * E_OUTOFMEMORY, if memory allocation fails.
6379 HRESULT WINAPI
VarBstrFromI2(short sIn
, LCID lcid
, ULONG dwFlags
, BSTR
* pbstrOut
)
6386 dwFlags
|= VAR_NEGATIVE
;
6388 return VARIANT_BstrFromUInt(ul64
, lcid
, dwFlags
, pbstrOut
);
6391 /******************************************************************************
6392 * VarBstrFromI4 (OLEAUT32.110)
6394 * Convert a VT_I4 to a VT_BSTR.
6398 * lcid [I] LCID for the conversion
6399 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
6400 * pbstrOut [O] Destination
6404 * Failure: E_INVALIDARG, if pbstrOut is invalid.
6405 * E_OUTOFMEMORY, if memory allocation fails.
6407 HRESULT WINAPI
VarBstrFromI4(LONG lIn
, LCID lcid
, ULONG dwFlags
, BSTR
* pbstrOut
)
6414 dwFlags
|= VAR_NEGATIVE
;
6416 return VARIANT_BstrFromUInt(ul64
, lcid
, dwFlags
, pbstrOut
);
6419 static BSTR
VARIANT_BstrReplaceDecimal(const WCHAR
* buff
, LCID lcid
, ULONG dwFlags
)
6422 WCHAR lpDecimalSep
[16];
6424 /* Native oleaut32 uses the locale-specific decimal separator even in the
6425 absence of the LOCALE_USE_NLS flag. For example, the Spanish/Latin
6426 American locales will see "one thousand and one tenth" as "1000,1"
6427 instead of "1000.1" (notice the comma). The following code checks for
6428 the need to replace the decimal separator, and if so, will prepare an
6429 appropriate NUMBERFMTW structure to do the job via GetNumberFormatW().
6431 GetLocaleInfoW(lcid
, LOCALE_SDECIMAL
| (dwFlags
& LOCALE_NOUSEROVERRIDE
),
6432 lpDecimalSep
, sizeof(lpDecimalSep
) / sizeof(WCHAR
));
6433 if (lpDecimalSep
[0] == '.' && lpDecimalSep
[1] == '\0')
6435 /* locale is compatible with English - return original string */
6436 bstrOut
= SysAllocString(buff
);
6442 WCHAR empty
[] = {'\0'};
6443 NUMBERFMTW minFormat
;
6445 minFormat
.NumDigits
= 0;
6446 minFormat
.LeadingZero
= 0;
6447 minFormat
.Grouping
= 0;
6448 minFormat
.lpDecimalSep
= lpDecimalSep
;
6449 minFormat
.lpThousandSep
= empty
;
6450 minFormat
.NegativeOrder
= 1; /* NLS_NEG_LEFT */
6452 /* count number of decimal digits in string */
6453 p
= strchrW( buff
, '.' );
6454 if (p
) minFormat
.NumDigits
= strlenW(p
+ 1);
6457 if (!GetNumberFormatW(lcid
, 0, buff
, &minFormat
, numbuff
, sizeof(numbuff
) / sizeof(WCHAR
)))
6459 WARN("GetNumberFormatW() failed, returning raw number string instead\n");
6460 bstrOut
= SysAllocString(buff
);
6464 TRACE("created minimal NLS string %s\n", debugstr_w(numbuff
));
6465 bstrOut
= SysAllocString(numbuff
);
6471 static HRESULT
VARIANT_BstrFromReal(DOUBLE dblIn
, LCID lcid
, ULONG dwFlags
,
6472 BSTR
* pbstrOut
, LPCWSTR lpszFormat
)
6477 return E_INVALIDARG
;
6479 sprintfW( buff
, lpszFormat
, dblIn
);
6481 /* Negative zeroes are disallowed (some applications depend on this).
6482 If buff starts with a minus, and then nothing follows but zeroes
6483 and/or a period, it is a negative zero and is replaced with a
6484 canonical zero. This duplicates native oleaut32 behavior.
6488 const WCHAR szAccept
[] = {'0', '.', '\0'};
6489 if (strlenW(buff
+ 1) == strspnW(buff
+ 1, szAccept
))
6490 { buff
[0] = '0'; buff
[1] = '\0'; }
6493 TRACE("created string %s\n", debugstr_w(buff
));
6494 if (dwFlags
& LOCALE_USE_NLS
)
6498 /* Format the number for the locale */
6500 GetNumberFormatW(lcid
, dwFlags
& LOCALE_NOUSEROVERRIDE
,
6501 buff
, NULL
, numbuff
, sizeof(numbuff
) / sizeof(WCHAR
));
6502 TRACE("created NLS string %s\n", debugstr_w(numbuff
));
6503 *pbstrOut
= SysAllocString(numbuff
);
6507 *pbstrOut
= VARIANT_BstrReplaceDecimal(buff
, lcid
, dwFlags
);
6509 return *pbstrOut
? S_OK
: E_OUTOFMEMORY
;
6512 /******************************************************************************
6513 * VarBstrFromR4 (OLEAUT32.111)
6515 * Convert a VT_R4 to a VT_BSTR.
6519 * lcid [I] LCID for the conversion
6520 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
6521 * pbstrOut [O] Destination
6525 * Failure: E_INVALIDARG, if pbstrOut is invalid.
6526 * E_OUTOFMEMORY, if memory allocation fails.
6528 HRESULT WINAPI
VarBstrFromR4(FLOAT fltIn
, LCID lcid
, ULONG dwFlags
, BSTR
* pbstrOut
)
6530 return VARIANT_BstrFromReal(fltIn
, lcid
, dwFlags
, pbstrOut
, szFloatFormatW
);
6533 /******************************************************************************
6534 * VarBstrFromR8 (OLEAUT32.112)
6536 * Convert a VT_R8 to a VT_BSTR.
6540 * lcid [I] LCID for the conversion
6541 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
6542 * pbstrOut [O] Destination
6546 * Failure: E_INVALIDARG, if pbstrOut is invalid.
6547 * E_OUTOFMEMORY, if memory allocation fails.
6549 HRESULT WINAPI
VarBstrFromR8(double dblIn
, LCID lcid
, ULONG dwFlags
, BSTR
* pbstrOut
)
6551 return VARIANT_BstrFromReal(dblIn
, lcid
, dwFlags
, pbstrOut
, szDoubleFormatW
);
6554 /******************************************************************************
6555 * VarBstrFromCy [OLEAUT32.113]
6557 * Convert a VT_CY to a VT_BSTR.
6561 * lcid [I] LCID for the conversion
6562 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
6563 * pbstrOut [O] Destination
6567 * Failure: E_INVALIDARG, if pbstrOut is invalid.
6568 * E_OUTOFMEMORY, if memory allocation fails.
6570 HRESULT WINAPI
VarBstrFromCy(CY cyIn
, LCID lcid
, ULONG dwFlags
, BSTR
*pbstrOut
)
6576 return E_INVALIDARG
;
6580 decVal
.bitsnum
[0] = cyIn
.s
.Lo
;
6581 decVal
.bitsnum
[1] = cyIn
.s
.Hi
;
6582 if (cyIn
.s
.Hi
& 0x80000000UL
) {
6585 /* Negative number! */
6587 decVal
.bitsnum
[0] = ~decVal
.bitsnum
[0];
6588 decVal
.bitsnum
[1] = ~decVal
.bitsnum
[1];
6589 VARIANT_int_add(decVal
.bitsnum
, 3, &one
, 1);
6591 decVal
.bitsnum
[2] = 0;
6592 VARIANT_DI_tostringW(&decVal
, buff
, sizeof(buff
)/sizeof(buff
[0]));
6594 if (dwFlags
& LOCALE_USE_NLS
)
6598 /* Format the currency for the locale */
6600 GetCurrencyFormatW(lcid
, dwFlags
& LOCALE_NOUSEROVERRIDE
,
6601 buff
, NULL
, cybuff
, sizeof(cybuff
) / sizeof(WCHAR
));
6602 *pbstrOut
= SysAllocString(cybuff
);
6605 *pbstrOut
= VARIANT_BstrReplaceDecimal(buff
,lcid
,dwFlags
);
6607 return *pbstrOut
? S_OK
: E_OUTOFMEMORY
;
6610 static inline int output_int_len(int o
, int min_len
, WCHAR
*date
, int date_len
)
6614 if(min_len
>= date_len
)
6617 for(len
=0, tmp
=o
; tmp
; tmp
/=10) len
++;
6622 for(tmp
=min_len
-len
; tmp
>0; tmp
--)
6624 for(tmp
=len
; tmp
>0; tmp
--, o
/=10)
6625 date
[tmp
-1] = '0' + o
%10;
6626 return min_len
>len
? min_len
: len
;
6629 /* format date string, similar to GetDateFormatW function but works on bigger range of dates */
6630 BOOL
get_date_format(LCID lcid
, DWORD flags
, const SYSTEMTIME
*st
,
6631 const WCHAR
*fmt
, WCHAR
*date
, int date_len
)
6633 static const LCTYPE dayname
[] = {
6634 LOCALE_SDAYNAME7
, LOCALE_SDAYNAME1
, LOCALE_SDAYNAME2
, LOCALE_SDAYNAME3
,
6635 LOCALE_SDAYNAME4
, LOCALE_SDAYNAME5
, LOCALE_SDAYNAME6
6637 static const LCTYPE sdayname
[] = {
6638 LOCALE_SABBREVDAYNAME7
, LOCALE_SABBREVDAYNAME1
, LOCALE_SABBREVDAYNAME2
,
6639 LOCALE_SABBREVDAYNAME3
, LOCALE_SABBREVDAYNAME4
, LOCALE_SABBREVDAYNAME5
,
6640 LOCALE_SABBREVDAYNAME6
6642 static const LCTYPE monthname
[] = {
6643 LOCALE_SMONTHNAME1
, LOCALE_SMONTHNAME2
, LOCALE_SMONTHNAME3
, LOCALE_SMONTHNAME4
,
6644 LOCALE_SMONTHNAME5
, LOCALE_SMONTHNAME6
, LOCALE_SMONTHNAME7
, LOCALE_SMONTHNAME8
,
6645 LOCALE_SMONTHNAME9
, LOCALE_SMONTHNAME10
, LOCALE_SMONTHNAME11
, LOCALE_SMONTHNAME12
6647 static const LCTYPE smonthname
[] = {
6648 LOCALE_SABBREVMONTHNAME1
, LOCALE_SABBREVMONTHNAME2
, LOCALE_SABBREVMONTHNAME3
,
6649 LOCALE_SABBREVMONTHNAME4
, LOCALE_SABBREVMONTHNAME5
, LOCALE_SABBREVMONTHNAME6
,
6650 LOCALE_SABBREVMONTHNAME7
, LOCALE_SABBREVMONTHNAME8
, LOCALE_SABBREVMONTHNAME9
,
6651 LOCALE_SABBREVMONTHNAME10
, LOCALE_SABBREVMONTHNAME11
, LOCALE_SABBREVMONTHNAME12
6654 if(flags
& ~(LOCALE_NOUSEROVERRIDE
|VAR_DATEVALUEONLY
))
6655 FIXME("ignoring flags %x\n", flags
);
6656 flags
&= LOCALE_NOUSEROVERRIDE
;
6658 while(*fmt
&& date_len
) {
6666 while(*fmt
== *(fmt
+count
))
6674 count
= GetLocaleInfoW(lcid
, dayname
[st
->wDayOfWeek
] | flags
, date
, date_len
)-1;
6676 count
= GetLocaleInfoW(lcid
, sdayname
[st
->wDayOfWeek
] | flags
, date
, date_len
)-1;
6678 count
= output_int_len(st
->wDay
, count
, date
, date_len
);
6682 count
= GetLocaleInfoW(lcid
, monthname
[st
->wMonth
-1] | flags
, date
, date_len
)-1;
6684 count
= GetLocaleInfoW(lcid
, smonthname
[st
->wMonth
-1] | flags
, date
, date_len
)-1;
6686 count
= output_int_len(st
->wMonth
, count
, date
, date_len
);
6690 count
= output_int_len(st
->wYear
, 0, date
, date_len
);
6692 count
= output_int_len(st
->wYear
%100, count
, date
, date_len
);
6696 FIXME("Should be using GetCalendarInfo(CAL_SERASTRING), defaulting to 'AD'\n");
6724 /******************************************************************************
6725 * VarBstrFromDate [OLEAUT32.114]
6727 * Convert a VT_DATE to a VT_BSTR.
6731 * lcid [I] LCID for the conversion
6732 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
6733 * pbstrOut [O] Destination
6737 * Failure: E_INVALIDARG, if pbstrOut or dateIn is invalid.
6738 * E_OUTOFMEMORY, if memory allocation fails.
6740 HRESULT WINAPI
VarBstrFromDate(DATE dateIn
, LCID lcid
, ULONG dwFlags
, BSTR
* pbstrOut
)
6743 DWORD dwFormatFlags
= dwFlags
& LOCALE_NOUSEROVERRIDE
;
6744 WCHAR date
[128], fmt_buff
[80], *time
;
6746 TRACE("(%g,0x%08x,0x%08x,%p)\n", dateIn
, lcid
, dwFlags
, pbstrOut
);
6748 if (!pbstrOut
|| !VariantTimeToSystemTime(dateIn
, &st
))
6749 return E_INVALIDARG
;
6753 if (dwFlags
& VAR_CALENDAR_THAI
)
6754 st
.wYear
+= 553; /* Use the Thai buddhist calendar year */
6755 else if (dwFlags
& (VAR_CALENDAR_HIJRI
|VAR_CALENDAR_GREGORIAN
))
6756 FIXME("VAR_CALENDAR_HIJRI/VAR_CALENDAR_GREGORIAN not handled\n");
6758 if (dwFlags
& LOCALE_USE_NLS
)
6759 dwFlags
&= ~(VAR_TIMEVALUEONLY
|VAR_DATEVALUEONLY
);
6762 double whole
= dateIn
< 0 ? ceil(dateIn
) : floor(dateIn
);
6763 double partial
= dateIn
- whole
;
6766 dwFlags
|= VAR_TIMEVALUEONLY
;
6767 else if (partial
> -1e-12 && partial
< 1e-12)
6768 dwFlags
|= VAR_DATEVALUEONLY
;
6771 if (dwFlags
& VAR_TIMEVALUEONLY
)
6774 if (!GetLocaleInfoW(lcid
, LOCALE_SSHORTDATE
, fmt_buff
, sizeof(fmt_buff
)/sizeof(WCHAR
)) ||
6775 !get_date_format(lcid
, dwFlags
, &st
, fmt_buff
, date
, sizeof(date
)/sizeof(WCHAR
)))
6776 return E_INVALIDARG
;
6778 if (!(dwFlags
& VAR_DATEVALUEONLY
))
6780 time
= date
+ strlenW(date
);
6783 if (!GetTimeFormatW(lcid
, dwFormatFlags
, &st
, NULL
, time
,
6784 sizeof(date
)/sizeof(WCHAR
)-(time
-date
)))
6785 return E_INVALIDARG
;
6788 *pbstrOut
= SysAllocString(date
);
6790 TRACE("returning %s\n", debugstr_w(*pbstrOut
));
6791 return *pbstrOut
? S_OK
: E_OUTOFMEMORY
;
6794 /******************************************************************************
6795 * VarBstrFromBool (OLEAUT32.116)
6797 * Convert a VT_BOOL to a VT_BSTR.
6801 * lcid [I] LCID for the conversion
6802 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
6803 * pbstrOut [O] Destination
6807 * Failure: E_INVALIDARG, if pbstrOut is invalid.
6808 * E_OUTOFMEMORY, if memory allocation fails.
6811 * If dwFlags includes VARIANT_LOCALBOOL, this function converts to the
6812 * localised text of "True" or "False". To convert a bool into a
6813 * numeric string of "0" or "-1", use VariantChangeTypeTypeEx().
6815 HRESULT WINAPI
VarBstrFromBool(VARIANT_BOOL boolIn
, LCID lcid
, ULONG dwFlags
, BSTR
* pbstrOut
)
6818 DWORD dwResId
= IDS_TRUE
;
6821 TRACE("%d,0x%08x,0x%08x,%p\n", boolIn
, lcid
, dwFlags
, pbstrOut
);
6824 return E_INVALIDARG
;
6826 /* VAR_BOOLONOFF and VAR_BOOLYESNO are internal flags used
6827 * for variant formatting */
6828 switch (dwFlags
& (VAR_LOCALBOOL
|VAR_BOOLONOFF
|VAR_BOOLYESNO
))
6839 lcid
= MAKELCID(MAKELANGID(LANG_ENGLISH
, SUBLANG_DEFAULT
),SORT_DEFAULT
);
6842 lcid
= ConvertDefaultLocale(lcid
);
6843 langId
= LANGIDFROMLCID(lcid
);
6844 if (PRIMARYLANGID(langId
) == LANG_NEUTRAL
)
6845 langId
= MAKELANGID(LANG_ENGLISH
, SUBLANG_DEFAULT
);
6847 if (boolIn
== VARIANT_FALSE
)
6848 dwResId
++; /* Use negative form */
6850 VarBstrFromBool_GetLocalised
:
6851 if (VARIANT_GetLocalisedText(langId
, dwResId
, szBuff
))
6853 *pbstrOut
= SysAllocString(szBuff
);
6854 return *pbstrOut
? S_OK
: E_OUTOFMEMORY
;
6857 if (langId
!= MAKELANGID(LANG_ENGLISH
, SUBLANG_DEFAULT
))
6859 langId
= MAKELANGID(LANG_ENGLISH
, SUBLANG_DEFAULT
);
6860 goto VarBstrFromBool_GetLocalised
;
6863 /* Should never get here */
6864 WARN("Failed to load bool text!\n");
6865 return E_OUTOFMEMORY
;
6868 /******************************************************************************
6869 * VarBstrFromI1 (OLEAUT32.229)
6871 * Convert a VT_I1 to a VT_BSTR.
6875 * lcid [I] LCID for the conversion
6876 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
6877 * pbstrOut [O] Destination
6881 * Failure: E_INVALIDARG, if pbstrOut is invalid.
6882 * E_OUTOFMEMORY, if memory allocation fails.
6884 HRESULT WINAPI
VarBstrFromI1(signed char cIn
, LCID lcid
, ULONG dwFlags
, BSTR
* pbstrOut
)
6891 dwFlags
|= VAR_NEGATIVE
;
6893 return VARIANT_BstrFromUInt(ul64
, lcid
, dwFlags
, pbstrOut
);
6896 /******************************************************************************
6897 * VarBstrFromUI2 (OLEAUT32.230)
6899 * Convert a VT_UI2 to a VT_BSTR.
6903 * lcid [I] LCID for the conversion
6904 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
6905 * pbstrOut [O] Destination
6909 * Failure: E_INVALIDARG, if pbstrOut is invalid.
6910 * E_OUTOFMEMORY, if memory allocation fails.
6912 HRESULT WINAPI
VarBstrFromUI2(USHORT usIn
, LCID lcid
, ULONG dwFlags
, BSTR
* pbstrOut
)
6914 return VARIANT_BstrFromUInt(usIn
, lcid
, dwFlags
, pbstrOut
);
6917 /******************************************************************************
6918 * VarBstrFromUI4 (OLEAUT32.231)
6920 * Convert a VT_UI4 to a VT_BSTR.
6924 * lcid [I] LCID for the conversion
6925 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
6926 * pbstrOut [O] Destination
6930 * Failure: E_INVALIDARG, if pbstrOut is invalid.
6931 * E_OUTOFMEMORY, if memory allocation fails.
6933 HRESULT WINAPI
VarBstrFromUI4(ULONG ulIn
, LCID lcid
, ULONG dwFlags
, BSTR
* pbstrOut
)
6935 return VARIANT_BstrFromUInt(ulIn
, lcid
, dwFlags
, pbstrOut
);
6938 /******************************************************************************
6939 * VarBstrFromDec (OLEAUT32.232)
6941 * Convert a VT_DECIMAL to a VT_BSTR.
6945 * lcid [I] LCID for the conversion
6946 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
6947 * pbstrOut [O] Destination
6951 * Failure: E_INVALIDARG, if pbstrOut is invalid.
6952 * E_OUTOFMEMORY, if memory allocation fails.
6954 HRESULT WINAPI
VarBstrFromDec(DECIMAL
* pDecIn
, LCID lcid
, ULONG dwFlags
, BSTR
* pbstrOut
)
6960 return E_INVALIDARG
;
6962 VARIANT_DIFromDec(pDecIn
, &temp
);
6963 VARIANT_DI_tostringW(&temp
, buff
, 256);
6965 if (dwFlags
& LOCALE_USE_NLS
)
6969 /* Format the number for the locale */
6971 GetNumberFormatW(lcid
, dwFlags
& LOCALE_NOUSEROVERRIDE
,
6972 buff
, NULL
, numbuff
, sizeof(numbuff
) / sizeof(WCHAR
));
6973 TRACE("created NLS string %s\n", debugstr_w(numbuff
));
6974 *pbstrOut
= SysAllocString(numbuff
);
6978 *pbstrOut
= VARIANT_BstrReplaceDecimal(buff
, lcid
, dwFlags
);
6981 TRACE("returning %s\n", debugstr_w(*pbstrOut
));
6982 return *pbstrOut
? S_OK
: E_OUTOFMEMORY
;
6985 /************************************************************************
6986 * VarBstrFromI8 (OLEAUT32.370)
6988 * Convert a VT_I8 to a VT_BSTR.
6992 * lcid [I] LCID for the conversion
6993 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
6994 * pbstrOut [O] Destination
6998 * Failure: E_INVALIDARG, if pbstrOut is invalid.
6999 * E_OUTOFMEMORY, if memory allocation fails.
7001 HRESULT WINAPI
VarBstrFromI8(LONG64 llIn
, LCID lcid
, ULONG dwFlags
, BSTR
* pbstrOut
)
7003 ULONG64 ul64
= llIn
;
7008 dwFlags
|= VAR_NEGATIVE
;
7010 return VARIANT_BstrFromUInt(ul64
, lcid
, dwFlags
, pbstrOut
);
7013 /************************************************************************
7014 * VarBstrFromUI8 (OLEAUT32.371)
7016 * Convert a VT_UI8 to a VT_BSTR.
7020 * lcid [I] LCID for the conversion
7021 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
7022 * pbstrOut [O] Destination
7026 * Failure: E_INVALIDARG, if pbstrOut is invalid.
7027 * E_OUTOFMEMORY, if memory allocation fails.
7029 HRESULT WINAPI
VarBstrFromUI8(ULONG64 ullIn
, LCID lcid
, ULONG dwFlags
, BSTR
* pbstrOut
)
7031 return VARIANT_BstrFromUInt(ullIn
, lcid
, dwFlags
, pbstrOut
);
7034 /************************************************************************
7035 * VarBstrFromDisp (OLEAUT32.115)
7037 * Convert a VT_DISPATCH to a BSTR.
7040 * pdispIn [I] Source
7041 * lcid [I] LCID for conversion
7042 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
7043 * pbstrOut [O] Destination
7047 * Failure: E_INVALIDARG, if the source value is invalid
7048 * DISP_E_TYPEMISMATCH, if the type cannot be converted
7050 HRESULT WINAPI
VarBstrFromDisp(IDispatch
* pdispIn
, LCID lcid
, ULONG dwFlags
, BSTR
* pbstrOut
)
7052 return VARIANT_FromDisp(pdispIn
, lcid
, pbstrOut
, VT_BSTR
, dwFlags
);
7055 /**********************************************************************
7056 * VarBstrCat (OLEAUT32.313)
7058 * Concatenate two BSTR values.
7061 * pbstrLeft [I] Source
7062 * pbstrRight [I] Value to concatenate
7063 * pbstrOut [O] Destination
7067 * Failure: E_INVALIDARG, if pbstrOut is invalid.
7068 * E_OUTOFMEMORY, if memory allocation fails.
7070 HRESULT WINAPI
VarBstrCat(BSTR pbstrLeft
, BSTR pbstrRight
, BSTR
*pbstrOut
)
7072 unsigned int lenLeft
, lenRight
;
7075 debugstr_wn(pbstrLeft
, SysStringLen(pbstrLeft
)),
7076 debugstr_wn(pbstrRight
, SysStringLen(pbstrRight
)), pbstrOut
);
7079 return E_INVALIDARG
;
7081 /* use byte length here to properly handle ansi-allocated BSTRs */
7082 lenLeft
= pbstrLeft
? SysStringByteLen(pbstrLeft
) : 0;
7083 lenRight
= pbstrRight
? SysStringByteLen(pbstrRight
) : 0;
7085 *pbstrOut
= SysAllocStringByteLen(NULL
, lenLeft
+ lenRight
);
7087 return E_OUTOFMEMORY
;
7089 (*pbstrOut
)[0] = '\0';
7092 memcpy(*pbstrOut
, pbstrLeft
, lenLeft
);
7095 memcpy((CHAR
*)*pbstrOut
+ lenLeft
, pbstrRight
, lenRight
);
7097 TRACE("%s\n", debugstr_wn(*pbstrOut
, SysStringLen(*pbstrOut
)));
7101 /**********************************************************************
7102 * VarBstrCmp (OLEAUT32.314)
7104 * Compare two BSTR values.
7107 * pbstrLeft [I] Source
7108 * pbstrRight [I] Value to compare
7109 * lcid [I] LCID for the comparison
7110 * dwFlags [I] Flags to pass directly to CompareStringW().
7113 * VARCMP_LT, VARCMP_EQ or VARCMP_GT indicating that pbstrLeft is less
7114 * than, equal to or greater than pbstrRight respectively.
7117 * VARCMP_NULL is NOT returned if either string is NULL unlike MSDN
7118 * states. A NULL BSTR pointer is equivalent to an empty string.
7119 * If LCID is equal to 0, a byte by byte comparison is performed.
7121 HRESULT WINAPI
VarBstrCmp(BSTR pbstrLeft
, BSTR pbstrRight
, LCID lcid
, DWORD dwFlags
)
7126 TRACE("%s,%s,%d,%08x\n",
7127 debugstr_wn(pbstrLeft
, SysStringLen(pbstrLeft
)),
7128 debugstr_wn(pbstrRight
, SysStringLen(pbstrRight
)), lcid
, dwFlags
);
7130 if (!pbstrLeft
|| !*pbstrLeft
)
7132 if (pbstrRight
&& *pbstrRight
)
7135 else if (!pbstrRight
|| !*pbstrRight
)
7140 unsigned int lenLeft
= SysStringByteLen(pbstrLeft
);
7141 unsigned int lenRight
= SysStringByteLen(pbstrRight
);
7142 ret
= memcmp(pbstrLeft
, pbstrRight
, min(lenLeft
, lenRight
));
7147 if (lenLeft
< lenRight
)
7149 if (lenLeft
> lenRight
)
7155 unsigned int lenLeft
= SysStringLen(pbstrLeft
);
7156 unsigned int lenRight
= SysStringLen(pbstrRight
);
7158 if (lenLeft
== 0 || lenRight
== 0)
7160 if (lenLeft
== 0 && lenRight
== 0) return VARCMP_EQ
;
7161 return lenLeft
< lenRight
? VARCMP_LT
: VARCMP_GT
;
7164 hres
= CompareStringW(lcid
, dwFlags
, pbstrLeft
, lenLeft
,
7165 pbstrRight
, lenRight
) - CSTR_LESS_THAN
;
7166 TRACE("%d\n", hres
);
7175 /******************************************************************************
7176 * VarDateFromUI1 (OLEAUT32.88)
7178 * Convert a VT_UI1 to a VT_DATE.
7182 * pdateOut [O] Destination
7187 HRESULT WINAPI
VarDateFromUI1(BYTE bIn
, DATE
* pdateOut
)
7189 return VarR8FromUI1(bIn
, pdateOut
);
7192 /******************************************************************************
7193 * VarDateFromI2 (OLEAUT32.89)
7195 * Convert a VT_I2 to a VT_DATE.
7199 * pdateOut [O] Destination
7204 HRESULT WINAPI
VarDateFromI2(short sIn
, DATE
* pdateOut
)
7206 return VarR8FromI2(sIn
, pdateOut
);
7209 /******************************************************************************
7210 * VarDateFromI4 (OLEAUT32.90)
7212 * Convert a VT_I4 to a VT_DATE.
7216 * pdateOut [O] Destination
7221 HRESULT WINAPI
VarDateFromI4(LONG lIn
, DATE
* pdateOut
)
7223 return VarDateFromR8(lIn
, pdateOut
);
7226 /******************************************************************************
7227 * VarDateFromR4 (OLEAUT32.91)
7229 * Convert a VT_R4 to a VT_DATE.
7233 * pdateOut [O] Destination
7238 HRESULT WINAPI
VarDateFromR4(FLOAT fltIn
, DATE
* pdateOut
)
7240 return VarR8FromR4(fltIn
, pdateOut
);
7243 /******************************************************************************
7244 * VarDateFromR8 (OLEAUT32.92)
7246 * Convert a VT_R8 to a VT_DATE.
7250 * pdateOut [O] Destination
7255 HRESULT WINAPI
VarDateFromR8(double dblIn
, DATE
* pdateOut
)
7257 if (dblIn
<= (DATE_MIN
- 1.0) || dblIn
>= (DATE_MAX
+ 1.0)) return DISP_E_OVERFLOW
;
7258 *pdateOut
= (DATE
)dblIn
;
7262 /**********************************************************************
7263 * VarDateFromDisp (OLEAUT32.95)
7265 * Convert a VT_DISPATCH to a VT_DATE.
7268 * pdispIn [I] Source
7269 * lcid [I] LCID for conversion
7270 * pdateOut [O] Destination
7274 * Failure: E_INVALIDARG, if the source value is invalid
7275 * DISP_E_OVERFLOW, if the value will not fit in the destination
7276 * DISP_E_TYPEMISMATCH, if the type cannot be converted
7278 HRESULT WINAPI
VarDateFromDisp(IDispatch
* pdispIn
, LCID lcid
, DATE
* pdateOut
)
7280 return VARIANT_FromDisp(pdispIn
, lcid
, pdateOut
, VT_DATE
, 0);
7283 /******************************************************************************
7284 * VarDateFromBool (OLEAUT32.96)
7286 * Convert a VT_BOOL to a VT_DATE.
7290 * pdateOut [O] Destination
7295 HRESULT WINAPI
VarDateFromBool(VARIANT_BOOL boolIn
, DATE
* pdateOut
)
7297 return VarR8FromBool(boolIn
, pdateOut
);
7300 /**********************************************************************
7301 * VarDateFromCy (OLEAUT32.93)
7303 * Convert a VT_CY to a VT_DATE.
7307 * pdateOut [O] Destination
7312 HRESULT WINAPI
VarDateFromCy(CY cyIn
, DATE
* pdateOut
)
7314 return VarR8FromCy(cyIn
, pdateOut
);
7317 /* Date string parsing */
7318 #define DP_TIMESEP 0x01 /* Time separator ( _must_ remain 0x1, used as a bitmask) */
7319 #define DP_DATESEP 0x02 /* Date separator */
7320 #define DP_MONTH 0x04 /* Month name */
7321 #define DP_AM 0x08 /* AM */
7322 #define DP_PM 0x10 /* PM */
7324 typedef struct tagDATEPARSE
7326 DWORD dwCount
; /* Number of fields found so far (maximum 6) */
7327 DWORD dwParseFlags
; /* Global parse flags (DP_ Flags above) */
7328 DWORD dwFlags
[6]; /* Flags for each field */
7329 DWORD dwValues
[6]; /* Value of each field */
7332 #define TIMEFLAG(i) ((dp.dwFlags[i] & DP_TIMESEP) << i)
7334 #define IsLeapYear(y) (((y % 4) == 0) && (((y % 100) != 0) || ((y % 400) == 0)))
7336 /* Determine if a day is valid in a given month of a given year */
7337 static BOOL
VARIANT_IsValidMonthDay(DWORD day
, DWORD month
, DWORD year
)
7339 static const BYTE days
[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
7341 if (day
&& month
&& month
< 13)
7343 if (day
<= days
[month
] || (month
== 2 && day
== 29 && IsLeapYear(year
)))
7349 /* Possible orders for 3 numbers making up a date */
7350 #define ORDER_MDY 0x01
7351 #define ORDER_YMD 0x02
7352 #define ORDER_YDM 0x04
7353 #define ORDER_DMY 0x08
7354 #define ORDER_MYD 0x10 /* Synthetic order, used only for funky 2 digit dates */
7356 /* Determine a date for a particular locale, from 3 numbers */
7357 static inline HRESULT
VARIANT_MakeDate(DATEPARSE
*dp
, DWORD iDate
,
7358 DWORD offset
, SYSTEMTIME
*st
)
7360 DWORD dwAllOrders
, dwTry
, dwCount
= 0, v1
, v2
, v3
;
7364 v1
= 30; /* Default to (Variant) 0 date part */
7367 goto VARIANT_MakeDate_OK
;
7370 v1
= dp
->dwValues
[offset
+ 0];
7371 v2
= dp
->dwValues
[offset
+ 1];
7372 if (dp
->dwCount
== 2)
7375 GetSystemTime(¤t
);
7379 v3
= dp
->dwValues
[offset
+ 2];
7381 TRACE("(%d,%d,%d,%d,%d)\n", v1
, v2
, v3
, iDate
, offset
);
7383 /* If one number must be a month (Because a month name was given), then only
7384 * consider orders with the month in that position.
7385 * If we took the current year as 'v3', then only allow a year in that position.
7387 if (dp
->dwFlags
[offset
+ 0] & DP_MONTH
)
7389 dwAllOrders
= ORDER_MDY
;
7391 else if (dp
->dwFlags
[offset
+ 1] & DP_MONTH
)
7393 dwAllOrders
= ORDER_DMY
;
7394 if (dp
->dwCount
> 2)
7395 dwAllOrders
|= ORDER_YMD
;
7397 else if (dp
->dwCount
> 2 && dp
->dwFlags
[offset
+ 2] & DP_MONTH
)
7399 dwAllOrders
= ORDER_YDM
;
7403 dwAllOrders
= ORDER_MDY
|ORDER_DMY
;
7404 if (dp
->dwCount
> 2)
7405 dwAllOrders
|= (ORDER_YMD
|ORDER_YDM
);
7408 VARIANT_MakeDate_Start
:
7409 TRACE("dwAllOrders is 0x%08x\n", dwAllOrders
);
7417 /* First: Try the order given by iDate */
7420 case 0: dwTry
= dwAllOrders
& ORDER_MDY
; break;
7421 case 1: dwTry
= dwAllOrders
& ORDER_DMY
; break;
7422 default: dwTry
= dwAllOrders
& ORDER_YMD
; break;
7425 else if (dwCount
== 1)
7427 /* Second: Try all the orders compatible with iDate */
7430 case 0: dwTry
= dwAllOrders
& ~(ORDER_DMY
|ORDER_YDM
); break;
7431 case 1: dwTry
= dwAllOrders
& ~(ORDER_MDY
|ORDER_YDM
|ORDER_MYD
); break;
7432 default: dwTry
= dwAllOrders
& ~(ORDER_DMY
|ORDER_YDM
); break;
7437 /* Finally: Try any remaining orders */
7438 dwTry
= dwAllOrders
;
7441 TRACE("Attempt %d, dwTry is 0x%08x\n", dwCount
, dwTry
);
7447 #define DATE_SWAP(x,y) do { dwTemp = x; x = y; y = dwTemp; } while (0)
7449 if (dwTry
& ORDER_MDY
)
7451 if (VARIANT_IsValidMonthDay(v2
,v1
,v3
))
7454 goto VARIANT_MakeDate_OK
;
7456 dwAllOrders
&= ~ORDER_MDY
;
7458 if (dwTry
& ORDER_YMD
)
7460 if (VARIANT_IsValidMonthDay(v3
,v2
,v1
))
7463 goto VARIANT_MakeDate_OK
;
7465 dwAllOrders
&= ~ORDER_YMD
;
7467 if (dwTry
& ORDER_YDM
)
7469 if (VARIANT_IsValidMonthDay(v2
,v3
,v1
))
7473 goto VARIANT_MakeDate_OK
;
7475 dwAllOrders
&= ~ORDER_YDM
;
7477 if (dwTry
& ORDER_DMY
)
7479 if (VARIANT_IsValidMonthDay(v1
,v2
,v3
))
7480 goto VARIANT_MakeDate_OK
;
7481 dwAllOrders
&= ~ORDER_DMY
;
7483 if (dwTry
& ORDER_MYD
)
7485 /* Only occurs if we are trying a 2 year date as M/Y not D/M */
7486 if (VARIANT_IsValidMonthDay(v3
,v1
,v2
))
7490 goto VARIANT_MakeDate_OK
;
7492 dwAllOrders
&= ~ORDER_MYD
;
7496 if (dp
->dwCount
== 2)
7498 /* We couldn't make a date as D/M or M/D, so try M/Y or Y/M */
7499 v3
= 1; /* 1st of the month */
7500 dwAllOrders
= ORDER_YMD
|ORDER_MYD
;
7501 dp
->dwCount
= 0; /* Don't return to this code path again */
7503 goto VARIANT_MakeDate_Start
;
7506 /* No valid dates were able to be constructed */
7507 return DISP_E_TYPEMISMATCH
;
7509 VARIANT_MakeDate_OK
:
7511 /* Check that the time part is ok */
7512 if (st
->wHour
> 23 || st
->wMinute
> 59 || st
->wSecond
> 59)
7513 return DISP_E_TYPEMISMATCH
;
7515 TRACE("Time %d %d %d\n", st
->wHour
, st
->wMinute
, st
->wSecond
);
7516 if (st
->wHour
< 12 && (dp
->dwParseFlags
& DP_PM
))
7518 else if (st
->wHour
== 12 && (dp
->dwParseFlags
& DP_AM
))
7520 TRACE("Time %d %d %d\n", st
->wHour
, st
->wMinute
, st
->wSecond
);
7524 /* FIXME: For 2 digit dates, I'm not sure if 30 is hard coded or not. It may
7525 * be retrieved from:
7526 * HKCU\Control Panel\International\Calendars\TwoDigitYearMax
7527 * But Wine doesn't have/use that key as at the time of writing.
7529 st
->wYear
= v3
< 30 ? 2000 + v3
: v3
< 100 ? 1900 + v3
: v3
;
7530 TRACE("Returning date %d/%d/%d\n", v1
, v2
, st
->wYear
);
7534 /******************************************************************************
7535 * VarDateFromStr [OLEAUT32.94]
7537 * Convert a VT_BSTR to at VT_DATE.
7540 * strIn [I] String to convert
7541 * lcid [I] Locale identifier for the conversion
7542 * dwFlags [I] Flags affecting the conversion (VAR_ flags from "oleauto.h")
7543 * pdateOut [O] Destination for the converted value
7546 * Success: S_OK. pdateOut contains the converted value.
7547 * FAILURE: An HRESULT error code indicating the problem.
7550 * Any date format that can be created using the date formats from lcid
7551 * (Either from kernel Nls functions, variant conversion or formatting) is a
7552 * valid input to this function. In addition, a few more esoteric formats are
7553 * also supported for compatibility with the native version. The date is
7554 * interpreted according to the date settings in the control panel, unless
7555 * the date is invalid in that format, in which the most compatible format
7556 * that produces a valid date will be used.
7558 HRESULT WINAPI
VarDateFromStr(OLECHAR
* strIn
, LCID lcid
, ULONG dwFlags
, DATE
* pdateOut
)
7560 static const USHORT ParseDateTokens
[] =
7562 LOCALE_SMONTHNAME1
, LOCALE_SMONTHNAME2
, LOCALE_SMONTHNAME3
, LOCALE_SMONTHNAME4
,
7563 LOCALE_SMONTHNAME5
, LOCALE_SMONTHNAME6
, LOCALE_SMONTHNAME7
, LOCALE_SMONTHNAME8
,
7564 LOCALE_SMONTHNAME9
, LOCALE_SMONTHNAME10
, LOCALE_SMONTHNAME11
, LOCALE_SMONTHNAME12
,
7565 LOCALE_SMONTHNAME13
,
7566 LOCALE_SABBREVMONTHNAME1
, LOCALE_SABBREVMONTHNAME2
, LOCALE_SABBREVMONTHNAME3
,
7567 LOCALE_SABBREVMONTHNAME4
, LOCALE_SABBREVMONTHNAME5
, LOCALE_SABBREVMONTHNAME6
,
7568 LOCALE_SABBREVMONTHNAME7
, LOCALE_SABBREVMONTHNAME8
, LOCALE_SABBREVMONTHNAME9
,
7569 LOCALE_SABBREVMONTHNAME10
, LOCALE_SABBREVMONTHNAME11
, LOCALE_SABBREVMONTHNAME12
,
7570 LOCALE_SABBREVMONTHNAME13
,
7571 LOCALE_SDAYNAME1
, LOCALE_SDAYNAME2
, LOCALE_SDAYNAME3
, LOCALE_SDAYNAME4
,
7572 LOCALE_SDAYNAME5
, LOCALE_SDAYNAME6
, LOCALE_SDAYNAME7
,
7573 LOCALE_SABBREVDAYNAME1
, LOCALE_SABBREVDAYNAME2
, LOCALE_SABBREVDAYNAME3
,
7574 LOCALE_SABBREVDAYNAME4
, LOCALE_SABBREVDAYNAME5
, LOCALE_SABBREVDAYNAME6
,
7575 LOCALE_SABBREVDAYNAME7
,
7576 LOCALE_S1159
, LOCALE_S2359
,
7579 static const BYTE ParseDateMonths
[] =
7581 1,2,3,4,5,6,7,8,9,10,11,12,13,
7582 1,2,3,4,5,6,7,8,9,10,11,12,13
7585 BSTR tokens
[sizeof(ParseDateTokens
)/sizeof(ParseDateTokens
[0])];
7587 DWORD dwDateSeps
= 0, iDate
= 0;
7588 HRESULT hRet
= S_OK
;
7590 if ((dwFlags
& (VAR_TIMEVALUEONLY
|VAR_DATEVALUEONLY
)) ==
7591 (VAR_TIMEVALUEONLY
|VAR_DATEVALUEONLY
))
7592 return E_INVALIDARG
;
7595 return DISP_E_TYPEMISMATCH
;
7599 TRACE("(%s,0x%08x,0x%08x,%p)\n", debugstr_w(strIn
), lcid
, dwFlags
, pdateOut
);
7601 memset(&dp
, 0, sizeof(dp
));
7603 GetLocaleInfoW(lcid
, LOCALE_IDATE
|LOCALE_RETURN_NUMBER
|(dwFlags
& LOCALE_NOUSEROVERRIDE
),
7604 (LPWSTR
)&iDate
, sizeof(iDate
)/sizeof(WCHAR
));
7605 TRACE("iDate is %d\n", iDate
);
7607 /* Get the month/day/am/pm tokens for this locale */
7608 for (i
= 0; i
< sizeof(tokens
)/sizeof(tokens
[0]); i
++)
7611 LCTYPE lctype
= ParseDateTokens
[i
] | (dwFlags
& LOCALE_NOUSEROVERRIDE
);
7613 /* FIXME: Alternate calendars - should use GetCalendarInfo() and/or
7614 * GetAltMonthNames(). We should really cache these strings too.
7617 GetLocaleInfoW(lcid
, lctype
, buff
, sizeof(buff
)/sizeof(WCHAR
));
7618 tokens
[i
] = SysAllocString(buff
);
7619 TRACE("token %d is %s\n", i
, debugstr_w(tokens
[i
]));
7622 /* Parse the string into our structure */
7625 if (dp
.dwCount
>= 6)
7628 if (isdigitW(*strIn
))
7630 dp
.dwValues
[dp
.dwCount
] = strtoulW(strIn
, &strIn
, 10);
7634 else if (isalpha(*strIn
))
7636 BOOL bFound
= FALSE
;
7638 for (i
= 0; i
< sizeof(tokens
)/sizeof(tokens
[0]); i
++)
7640 DWORD dwLen
= strlenW(tokens
[i
]);
7641 if (dwLen
&& !strncmpiW(strIn
, tokens
[i
], dwLen
))
7645 dp
.dwValues
[dp
.dwCount
] = ParseDateMonths
[i
];
7646 dp
.dwFlags
[dp
.dwCount
] |= (DP_MONTH
|DP_DATESEP
);
7649 else if (i
> 39 && i
< 42)
7651 if (!dp
.dwCount
|| dp
.dwParseFlags
& (DP_AM
|DP_PM
))
7652 hRet
= DISP_E_TYPEMISMATCH
;
7655 dp
.dwFlags
[dp
.dwCount
- 1] |= (i
== 40 ? DP_AM
: DP_PM
);
7656 dp
.dwParseFlags
|= (i
== 40 ? DP_AM
: DP_PM
);
7659 strIn
+= (dwLen
- 1);
7667 if ((*strIn
== 'a' || *strIn
== 'A' || *strIn
== 'p' || *strIn
== 'P') &&
7668 (dp
.dwCount
&& !(dp
.dwParseFlags
& (DP_AM
|DP_PM
))))
7670 /* Special case - 'a' and 'p' are recognised as short for am/pm */
7671 if (*strIn
== 'a' || *strIn
== 'A')
7673 dp
.dwFlags
[dp
.dwCount
- 1] |= DP_AM
;
7674 dp
.dwParseFlags
|= DP_AM
;
7678 dp
.dwFlags
[dp
.dwCount
- 1] |= DP_PM
;
7679 dp
.dwParseFlags
|= DP_PM
;
7685 TRACE("No matching token for %s\n", debugstr_w(strIn
));
7686 hRet
= DISP_E_TYPEMISMATCH
;
7691 else if (*strIn
== ':' || *strIn
== '.')
7693 if (!dp
.dwCount
|| !strIn
[1])
7694 hRet
= DISP_E_TYPEMISMATCH
;
7696 if (tokens
[42][0] == *strIn
)
7700 hRet
= DISP_E_TYPEMISMATCH
;
7702 dp
.dwFlags
[dp
.dwCount
- 1] |= DP_DATESEP
;
7705 dp
.dwFlags
[dp
.dwCount
- 1] |= DP_TIMESEP
;
7707 else if (*strIn
== '-' || *strIn
== '/')
7710 if (dwDateSeps
> 2 || !dp
.dwCount
|| !strIn
[1])
7711 hRet
= DISP_E_TYPEMISMATCH
;
7713 dp
.dwFlags
[dp
.dwCount
- 1] |= DP_DATESEP
;
7715 else if (*strIn
== ',' || isspaceW(*strIn
))
7717 if (*strIn
== ',' && !strIn
[1])
7718 hRet
= DISP_E_TYPEMISMATCH
;
7722 hRet
= DISP_E_TYPEMISMATCH
;
7727 if (!dp
.dwCount
|| dp
.dwCount
> 6 ||
7728 (dp
.dwCount
== 1 && !(dp
.dwParseFlags
& (DP_AM
|DP_PM
))))
7729 hRet
= DISP_E_TYPEMISMATCH
;
7731 if (SUCCEEDED(hRet
))
7734 DWORD dwOffset
= 0; /* Start of date fields in dp.dwValues */
7736 st
.wDayOfWeek
= st
.wHour
= st
.wMinute
= st
.wSecond
= st
.wMilliseconds
= 0;
7738 /* Figure out which numbers correspond to which fields.
7740 * This switch statement works based on the fact that native interprets any
7741 * fields that are not joined with a time separator ('.' or ':') as date
7742 * fields. Thus we construct a value from 0-32 where each set bit indicates
7743 * a time field. This encapsulates the hundreds of permutations of 2-6 fields.
7744 * For valid permutations, we set dwOffset to point to the first date field
7745 * and shorten dp.dwCount by the number of time fields found. The real
7746 * magic here occurs in VARIANT_MakeDate() above, where we determine what
7747 * each date number must represent in the context of iDate.
7749 TRACE("0x%08x\n", TIMEFLAG(0)|TIMEFLAG(1)|TIMEFLAG(2)|TIMEFLAG(3)|TIMEFLAG(4));
7751 switch (TIMEFLAG(0)|TIMEFLAG(1)|TIMEFLAG(2)|TIMEFLAG(3)|TIMEFLAG(4))
7753 case 0x1: /* TT TTDD TTDDD */
7754 if (dp
.dwCount
> 3 &&
7755 ((dp
.dwFlags
[2] & (DP_AM
|DP_PM
)) || (dp
.dwFlags
[3] & (DP_AM
|DP_PM
)) ||
7756 (dp
.dwFlags
[4] & (DP_AM
|DP_PM
))))
7757 hRet
= DISP_E_TYPEMISMATCH
;
7758 else if (dp
.dwCount
!= 2 && dp
.dwCount
!= 4 && dp
.dwCount
!= 5)
7759 hRet
= DISP_E_TYPEMISMATCH
;
7760 st
.wHour
= dp
.dwValues
[0];
7761 st
.wMinute
= dp
.dwValues
[1];
7766 case 0x3: /* TTT TTTDD TTTDDD */
7767 if (dp
.dwCount
> 4 &&
7768 ((dp
.dwFlags
[3] & (DP_AM
|DP_PM
)) || (dp
.dwFlags
[4] & (DP_AM
|DP_PM
)) ||
7769 (dp
.dwFlags
[5] & (DP_AM
|DP_PM
))))
7770 hRet
= DISP_E_TYPEMISMATCH
;
7771 else if (dp
.dwCount
!= 3 && dp
.dwCount
!= 5 && dp
.dwCount
!= 6)
7772 hRet
= DISP_E_TYPEMISMATCH
;
7773 st
.wHour
= dp
.dwValues
[0];
7774 st
.wMinute
= dp
.dwValues
[1];
7775 st
.wSecond
= dp
.dwValues
[2];
7780 case 0x4: /* DDTT */
7781 if (dp
.dwCount
!= 4 ||
7782 (dp
.dwFlags
[0] & (DP_AM
|DP_PM
)) || (dp
.dwFlags
[1] & (DP_AM
|DP_PM
)))
7783 hRet
= DISP_E_TYPEMISMATCH
;
7785 st
.wHour
= dp
.dwValues
[2];
7786 st
.wMinute
= dp
.dwValues
[3];
7790 case 0x0: /* T DD DDD TDDD TDDD */
7791 if (dp
.dwCount
== 1 && (dp
.dwParseFlags
& (DP_AM
|DP_PM
)))
7793 st
.wHour
= dp
.dwValues
[0]; /* T */
7797 else if (dp
.dwCount
> 4 || (dp
.dwCount
< 3 && dp
.dwParseFlags
& (DP_AM
|DP_PM
)))
7799 hRet
= DISP_E_TYPEMISMATCH
;
7801 else if (dp
.dwCount
== 3)
7803 if (dp
.dwFlags
[0] & (DP_AM
|DP_PM
)) /* TDD */
7806 st
.wHour
= dp
.dwValues
[0];
7810 if (dp
.dwFlags
[2] & (DP_AM
|DP_PM
)) /* DDT */
7813 st
.wHour
= dp
.dwValues
[2];
7816 else if (dp
.dwParseFlags
& (DP_AM
|DP_PM
))
7817 hRet
= DISP_E_TYPEMISMATCH
;
7819 else if (dp
.dwCount
== 4)
7822 if (dp
.dwFlags
[0] & (DP_AM
|DP_PM
)) /* TDDD */
7824 st
.wHour
= dp
.dwValues
[0];
7827 else if (dp
.dwFlags
[3] & (DP_AM
|DP_PM
)) /* DDDT */
7829 st
.wHour
= dp
.dwValues
[3];
7832 hRet
= DISP_E_TYPEMISMATCH
;
7835 /* .. fall through .. */
7837 case 0x8: /* DDDTT */
7838 if ((dp
.dwCount
== 2 && (dp
.dwParseFlags
& (DP_AM
|DP_PM
))) ||
7839 (dp
.dwCount
== 5 && ((dp
.dwFlags
[0] & (DP_AM
|DP_PM
)) ||
7840 (dp
.dwFlags
[1] & (DP_AM
|DP_PM
)) || (dp
.dwFlags
[2] & (DP_AM
|DP_PM
)))) ||
7841 dp
.dwCount
== 4 || dp
.dwCount
== 6)
7842 hRet
= DISP_E_TYPEMISMATCH
;
7843 st
.wHour
= dp
.dwValues
[3];
7844 st
.wMinute
= dp
.dwValues
[4];
7845 if (dp
.dwCount
== 5)
7849 case 0xC: /* DDTTT */
7850 if (dp
.dwCount
!= 5 ||
7851 (dp
.dwFlags
[0] & (DP_AM
|DP_PM
)) || (dp
.dwFlags
[1] & (DP_AM
|DP_PM
)))
7852 hRet
= DISP_E_TYPEMISMATCH
;
7853 st
.wHour
= dp
.dwValues
[2];
7854 st
.wMinute
= dp
.dwValues
[3];
7855 st
.wSecond
= dp
.dwValues
[4];
7859 case 0x18: /* DDDTTT */
7860 if ((dp
.dwFlags
[0] & (DP_AM
|DP_PM
)) || (dp
.dwFlags
[1] & (DP_AM
|DP_PM
)) ||
7861 (dp
.dwFlags
[2] & (DP_AM
|DP_PM
)))
7862 hRet
= DISP_E_TYPEMISMATCH
;
7863 st
.wHour
= dp
.dwValues
[3];
7864 st
.wMinute
= dp
.dwValues
[4];
7865 st
.wSecond
= dp
.dwValues
[5];
7870 hRet
= DISP_E_TYPEMISMATCH
;
7874 if (SUCCEEDED(hRet
))
7876 hRet
= VARIANT_MakeDate(&dp
, iDate
, dwOffset
, &st
);
7878 if (dwFlags
& VAR_TIMEVALUEONLY
)
7884 else if (dwFlags
& VAR_DATEVALUEONLY
)
7885 st
.wHour
= st
.wMinute
= st
.wSecond
= 0;
7887 /* Finally, convert the value to a VT_DATE */
7888 if (SUCCEEDED(hRet
))
7889 hRet
= SystemTimeToVariantTime(&st
, pdateOut
) ? S_OK
: DISP_E_TYPEMISMATCH
;
7893 for (i
= 0; i
< sizeof(tokens
)/sizeof(tokens
[0]); i
++)
7894 SysFreeString(tokens
[i
]);
7898 /******************************************************************************
7899 * VarDateFromI1 (OLEAUT32.221)
7901 * Convert a VT_I1 to a VT_DATE.
7905 * pdateOut [O] Destination
7910 HRESULT WINAPI
VarDateFromI1(signed char cIn
, DATE
* pdateOut
)
7912 return VarR8FromI1(cIn
, pdateOut
);
7915 /******************************************************************************
7916 * VarDateFromUI2 (OLEAUT32.222)
7918 * Convert a VT_UI2 to a VT_DATE.
7922 * pdateOut [O] Destination
7927 HRESULT WINAPI
VarDateFromUI2(USHORT uiIn
, DATE
* pdateOut
)
7929 return VarR8FromUI2(uiIn
, pdateOut
);
7932 /******************************************************************************
7933 * VarDateFromUI4 (OLEAUT32.223)
7935 * Convert a VT_UI4 to a VT_DATE.
7939 * pdateOut [O] Destination
7944 HRESULT WINAPI
VarDateFromUI4(ULONG ulIn
, DATE
* pdateOut
)
7946 return VarDateFromR8(ulIn
, pdateOut
);
7949 /**********************************************************************
7950 * VarDateFromDec (OLEAUT32.224)
7952 * Convert a VT_DECIMAL to a VT_DATE.
7956 * pdateOut [O] Destination
7961 HRESULT WINAPI
VarDateFromDec(DECIMAL
*pdecIn
, DATE
* pdateOut
)
7963 return VarR8FromDec(pdecIn
, pdateOut
);
7966 /******************************************************************************
7967 * VarDateFromI8 (OLEAUT32.364)
7969 * Convert a VT_I8 to a VT_DATE.
7973 * pdateOut [O] Destination
7977 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
7979 HRESULT WINAPI
VarDateFromI8(LONG64 llIn
, DATE
* pdateOut
)
7981 if (llIn
< DATE_MIN
|| llIn
> DATE_MAX
) return DISP_E_OVERFLOW
;
7982 *pdateOut
= (DATE
)llIn
;
7986 /******************************************************************************
7987 * VarDateFromUI8 (OLEAUT32.365)
7989 * Convert a VT_UI8 to a VT_DATE.
7993 * pdateOut [O] Destination
7997 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
7999 HRESULT WINAPI
VarDateFromUI8(ULONG64 ullIn
, DATE
* pdateOut
)
8001 if (ullIn
> DATE_MAX
) return DISP_E_OVERFLOW
;
8002 *pdateOut
= (DATE
)ullIn
;