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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #define NONAMELESSUNION
21 #define NONAMELESSSTRUCT
22 #include "wine/debug.h"
23 #include "wine/unicode.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(ole);
32 extern HMODULE OLEAUT32_hModule;
34 static const WCHAR szFloatFormatW[] = { '%','.','7','G','\0' };
35 static const WCHAR szDoubleFormatW[] = { '%','.','1','5','G','\0' };
37 /* Copy data from one variant to another. */
38 static inline void VARIANT_CopyData(const VARIANT *srcVar, VARTYPE vt, void *pOut)
43 case VT_UI1: memcpy(pOut, &V_UI1(srcVar), sizeof(BYTE)); break;
46 case VT_UI2: memcpy(pOut, &V_UI2(srcVar), sizeof(SHORT)); break;
51 case VT_UI4: memcpy(pOut, &V_UI4(srcVar), sizeof (LONG)); break;
56 case VT_UI8: memcpy(pOut, &V_UI8(srcVar), sizeof (LONG64)); break;
57 case VT_INT_PTR: memcpy(pOut, &V_INT_PTR(srcVar), sizeof (INT_PTR)); break;
58 case VT_DECIMAL: memcpy(pOut, &V_DECIMAL(srcVar), sizeof (DECIMAL)); break;
60 FIXME("VT_ type %d unhandled, please report!\n", vt);
65 /* Coerce VT_BSTR to a numeric type */
66 HRESULT VARIANT_NumberFromBstr(OLECHAR* pStrIn, LCID lcid, ULONG ulFlags,
67 void* pOut, VARTYPE vt)
74 /* Use VarParseNumFromStr/VarNumFromParseNum as MSDN indicates */
75 np.cDig = sizeof(rgb) / sizeof(BYTE);
76 np.dwInFlags = NUMPRS_STD;
78 hRet = VarParseNumFromStr(pStrIn, lcid, ulFlags, &np, rgb);
82 /* 1 << vt gives us the VTBIT constant for the destination number type */
83 hRet = VarNumFromParseNum(&np, rgb, 1 << vt, &dstVar);
85 VARIANT_CopyData(&dstVar, vt, pOut);
90 /* Coerce VT_DISPATCH to another type */
91 HRESULT VARIANT_FromDisp(IDispatch* pdispIn, LCID lcid, void* pOut, VARTYPE vt)
93 static const DISPPARAMS emptyParams = { NULL, NULL, 0, 0 };
94 VARIANTARG srcVar, dstVar;
98 return DISP_E_BADVARTYPE;
100 /* Get the default 'value' property from the IDispatch */
101 hRet = IDispatch_Invoke(pdispIn, DISPID_VALUE, &IID_NULL, lcid, DISPATCH_PROPERTYGET,
102 (DISPPARAMS*)&emptyParams, &srcVar, NULL, NULL);
106 /* Convert the property to the requested type */
107 V_VT(&dstVar) = VT_EMPTY;
108 hRet = VariantChangeTypeEx(&dstVar, &srcVar, lcid, 0, vt);
109 VariantClear(&srcVar);
113 VARIANT_CopyData(&dstVar, vt, pOut);
114 VariantClear(&srcVar);
118 hRet = DISP_E_TYPEMISMATCH;
125 /************************************************************************
126 * VarI1FromUI1 (OLEAUT32.244)
128 * Convert a VT_UI1 to a VT_I1.
132 * pcOut [O] Destination
136 * Failure: E_INVALIDARG, if the source value is invalid
137 * DISP_E_OVERFLOW, if the value will not fit in the destination
139 HRESULT WINAPI VarI1FromUI1(BYTE bIn, signed char* pcOut)
141 return _VarI1FromUI1(bIn, pcOut);
144 /************************************************************************
145 * VarI1FromI2 (OLEAUT32.245)
147 * Convert a VT_I2 to a VT_I1.
151 * pcOut [O] Destination
155 * Failure: E_INVALIDARG, if the source value is invalid
156 * DISP_E_OVERFLOW, if the value will not fit in the destination
158 HRESULT WINAPI VarI1FromI2(SHORT sIn, signed char* pcOut)
160 return _VarI1FromI2(sIn, pcOut);
163 /************************************************************************
164 * VarI1FromI4 (OLEAUT32.246)
166 * Convert a VT_I4 to a VT_I1.
170 * pcOut [O] Destination
174 * Failure: E_INVALIDARG, if the source value is invalid
175 * DISP_E_OVERFLOW, if the value will not fit in the destination
177 HRESULT WINAPI VarI1FromI4(LONG iIn, signed char* pcOut)
179 return _VarI1FromI4(iIn, pcOut);
182 /************************************************************************
183 * VarI1FromR4 (OLEAUT32.247)
185 * Convert a VT_R4 to a VT_I1.
189 * pcOut [O] Destination
193 * Failure: E_INVALIDARG, if the source value is invalid
194 * DISP_E_OVERFLOW, if the value will not fit in the destination
196 HRESULT WINAPI VarI1FromR4(FLOAT fltIn, signed char* pcOut)
198 return _VarI1FromR4(fltIn, pcOut);
201 /************************************************************************
202 * VarI1FromR8 (OLEAUT32.248)
204 * Convert a VT_R8 to a VT_I1.
208 * pcOut [O] Destination
212 * Failure: E_INVALIDARG, if the source value is invalid
213 * DISP_E_OVERFLOW, if the value will not fit in the destination
216 * See VarI8FromR8() for details concerning rounding.
218 HRESULT WINAPI VarI1FromR8(double dblIn, signed char* pcOut)
220 if (dblIn < (double)I1_MIN || dblIn > (double)I1_MAX)
221 return DISP_E_OVERFLOW;
222 OLEAUT32_DutchRound(CHAR, dblIn, *pcOut);
226 /************************************************************************
227 * VarI1FromDate (OLEAUT32.249)
229 * Convert a VT_DATE to a VT_I1.
233 * pcOut [O] Destination
237 * Failure: E_INVALIDARG, if the source value is invalid
238 * DISP_E_OVERFLOW, if the value will not fit in the destination
240 HRESULT WINAPI VarI1FromDate(DATE dateIn, signed char* pcOut)
242 return _VarI1FromDate(dateIn, pcOut);
245 /************************************************************************
246 * VarI1FromCy (OLEAUT32.250)
248 * Convert a VT_CY to a VT_I1.
252 * pcOut [O] Destination
256 * Failure: E_INVALIDARG, if the source value is invalid
257 * DISP_E_OVERFLOW, if the value will not fit in the destination
259 HRESULT WINAPI VarI1FromCy(CY cyIn, signed char* pcOut)
263 _VarI4FromCy(cyIn, &i);
264 return _VarI1FromI4(i, pcOut);
267 /************************************************************************
268 * VarI1FromStr (OLEAUT32.251)
270 * Convert a VT_BSTR to a VT_I1.
274 * lcid [I] LCID for the conversion
275 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
276 * pcOut [O] Destination
280 * Failure: E_INVALIDARG, if the source value is invalid
281 * DISP_E_OVERFLOW, if the value will not fit in the destination
282 * DISP_E_TYPEMISMATCH, if the type cannot be converted
284 HRESULT WINAPI VarI1FromStr(OLECHAR* strIn, LCID lcid, ULONG dwFlags, signed char* pcOut)
286 return _VarI1FromStr(strIn, lcid, dwFlags, pcOut);
289 /************************************************************************
290 * VarI1FromDisp (OLEAUT32.252)
292 * Convert a VT_DISPATCH to a VT_I1.
296 * lcid [I] LCID for conversion
297 * pcOut [O] Destination
301 * Failure: E_INVALIDARG, if the source value is invalid
302 * DISP_E_OVERFLOW, if the value will not fit in the destination
303 * DISP_E_TYPEMISMATCH, if the type cannot be converted
305 HRESULT WINAPI VarI1FromDisp(IDispatch* pdispIn, LCID lcid, signed char* pcOut)
307 return _VarI1FromDisp(pdispIn, lcid, pcOut);
310 /************************************************************************
311 * VarI1FromBool (OLEAUT32.253)
313 * Convert a VT_BOOL to a VT_I1.
317 * pcOut [O] Destination
322 HRESULT WINAPI VarI1FromBool(VARIANT_BOOL boolIn, signed char* pcOut)
324 return _VarI1FromBool(boolIn, pcOut);
327 /************************************************************************
328 * VarI1FromUI2 (OLEAUT32.254)
330 * Convert a VT_UI2 to a VT_I1.
334 * pcOut [O] Destination
338 * Failure: E_INVALIDARG, if the source value is invalid
339 * DISP_E_OVERFLOW, if the value will not fit in the destination
341 HRESULT WINAPI VarI1FromUI2(USHORT usIn, signed char* pcOut)
343 return _VarI1FromUI2(usIn, pcOut);
346 /************************************************************************
347 * VarI1FromUI4 (OLEAUT32.255)
349 * Convert a VT_UI4 to a VT_I1.
353 * pcOut [O] Destination
357 * Failure: E_INVALIDARG, if the source value is invalid
358 * DISP_E_OVERFLOW, if the value will not fit in the destination
359 * DISP_E_TYPEMISMATCH, if the type cannot be converted
361 HRESULT WINAPI VarI1FromUI4(ULONG ulIn, signed char* pcOut)
363 return _VarI1FromUI4(ulIn, pcOut);
366 /************************************************************************
367 * VarI1FromDec (OLEAUT32.256)
369 * Convert a VT_DECIMAL to a VT_I1.
373 * pcOut [O] Destination
377 * Failure: E_INVALIDARG, if the source value is invalid
378 * DISP_E_OVERFLOW, if the value will not fit in the destination
380 HRESULT WINAPI VarI1FromDec(DECIMAL *pdecIn, signed char* pcOut)
385 hRet = _VarI8FromDec(pdecIn, &i64);
388 hRet = _VarI1FromI8(i64, pcOut);
392 /************************************************************************
393 * VarI1FromI8 (OLEAUT32.376)
395 * Convert a VT_I8 to a VT_I1.
399 * pcOut [O] Destination
403 * Failure: E_INVALIDARG, if the source value is invalid
404 * DISP_E_OVERFLOW, if the value will not fit in the destination
406 HRESULT WINAPI VarI1FromI8(LONG64 llIn, signed char* pcOut)
408 return _VarI1FromI8(llIn, pcOut);
411 /************************************************************************
412 * VarI1FromUI8 (OLEAUT32.377)
414 * Convert a VT_UI8 to a VT_I1.
418 * pcOut [O] Destination
422 * Failure: E_INVALIDARG, if the source value is invalid
423 * DISP_E_OVERFLOW, if the value will not fit in the destination
425 HRESULT WINAPI VarI1FromUI8(ULONG64 ullIn, signed char* pcOut)
427 return _VarI1FromUI8(ullIn, pcOut);
433 /************************************************************************
434 * VarUI1FromI2 (OLEAUT32.130)
436 * Convert a VT_I2 to a VT_UI1.
440 * pbOut [O] Destination
444 * Failure: E_INVALIDARG, if the source value is invalid
445 * DISP_E_OVERFLOW, if the value will not fit in the destination
447 HRESULT WINAPI VarUI1FromI2(SHORT sIn, BYTE* pbOut)
449 return _VarUI1FromI2(sIn, pbOut);
452 /************************************************************************
453 * VarUI1FromI4 (OLEAUT32.131)
455 * Convert a VT_I4 to a VT_UI1.
459 * pbOut [O] Destination
463 * Failure: E_INVALIDARG, if the source value is invalid
464 * DISP_E_OVERFLOW, if the value will not fit in the destination
466 HRESULT WINAPI VarUI1FromI4(LONG iIn, BYTE* pbOut)
468 return _VarUI1FromI4(iIn, pbOut);
471 /************************************************************************
472 * VarUI1FromR4 (OLEAUT32.132)
474 * Convert a VT_R4 to a VT_UI1.
478 * pbOut [O] Destination
482 * Failure: E_INVALIDARG, if the source value is invalid
483 * DISP_E_OVERFLOW, if the value will not fit in the destination
484 * DISP_E_TYPEMISMATCH, if the type cannot be converted
486 HRESULT WINAPI VarUI1FromR4(FLOAT fltIn, BYTE* pbOut)
488 return _VarUI1FromR4(fltIn, pbOut);
491 /************************************************************************
492 * VarUI1FromR8 (OLEAUT32.133)
494 * Convert a VT_R8 to a VT_UI1.
498 * pbOut [O] Destination
502 * Failure: E_INVALIDARG, if the source value is invalid
503 * DISP_E_OVERFLOW, if the value will not fit in the destination
506 * See VarI8FromR8() for details concerning rounding.
508 HRESULT WINAPI VarUI1FromR8(double dblIn, BYTE* pbOut)
510 if (dblIn < -0.5 || dblIn > (double)UI1_MAX)
511 return DISP_E_OVERFLOW;
512 OLEAUT32_DutchRound(BYTE, dblIn, *pbOut);
516 /************************************************************************
517 * VarUI1FromCy (OLEAUT32.134)
519 * Convert a VT_CY to a VT_UI1.
523 * pbOut [O] Destination
527 * Failure: E_INVALIDARG, if the source value is invalid
528 * DISP_E_OVERFLOW, if the value will not fit in the destination
531 * Negative values >= -5000 will be converted to 0.
533 HRESULT WINAPI VarUI1FromCy(CY cyIn, BYTE* pbOut)
535 ULONG i = UI1_MAX + 1;
537 _VarUI4FromCy(cyIn, &i);
538 return _VarUI1FromUI4(i, pbOut);
541 /************************************************************************
542 * VarUI1FromDate (OLEAUT32.135)
544 * Convert a VT_DATE to a VT_UI1.
548 * pbOut [O] Destination
552 * Failure: E_INVALIDARG, if the source value is invalid
553 * DISP_E_OVERFLOW, if the value will not fit in the destination
555 HRESULT WINAPI VarUI1FromDate(DATE dateIn, BYTE* pbOut)
557 return _VarUI1FromDate(dateIn, pbOut);
560 /************************************************************************
561 * VarUI1FromStr (OLEAUT32.136)
563 * Convert a VT_BSTR to a VT_UI1.
567 * lcid [I] LCID for the conversion
568 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
569 * pbOut [O] Destination
573 * Failure: E_INVALIDARG, if the source value is invalid
574 * DISP_E_OVERFLOW, if the value will not fit in the destination
575 * DISP_E_TYPEMISMATCH, if the type cannot be converted
577 HRESULT WINAPI VarUI1FromStr(OLECHAR* strIn, LCID lcid, ULONG dwFlags, BYTE* pbOut)
579 return _VarUI1FromStr(strIn, lcid, dwFlags, pbOut);
582 /************************************************************************
583 * VarUI1FromDisp (OLEAUT32.137)
585 * Convert a VT_DISPATCH to a VT_UI1.
589 * lcid [I] LCID for conversion
590 * pbOut [O] Destination
594 * Failure: E_INVALIDARG, if the source value is invalid
595 * DISP_E_OVERFLOW, if the value will not fit in the destination
596 * DISP_E_TYPEMISMATCH, if the type cannot be converted
598 HRESULT WINAPI VarUI1FromDisp(IDispatch* pdispIn, LCID lcid, BYTE* pbOut)
600 return _VarUI1FromDisp(pdispIn, lcid, pbOut);
603 /************************************************************************
604 * VarUI1FromBool (OLEAUT32.138)
606 * Convert a VT_BOOL to a VT_UI1.
610 * pbOut [O] Destination
615 HRESULT WINAPI VarUI1FromBool(VARIANT_BOOL boolIn, BYTE* pbOut)
617 return _VarUI1FromBool(boolIn, pbOut);
620 /************************************************************************
621 * VarUI1FromI1 (OLEAUT32.237)
623 * Convert a VT_I1 to a VT_UI1.
627 * pbOut [O] Destination
631 * Failure: E_INVALIDARG, if the source value is invalid
632 * DISP_E_OVERFLOW, if the value will not fit in the destination
634 HRESULT WINAPI VarUI1FromI1(signed char cIn, BYTE* pbOut)
636 return _VarUI1FromI1(cIn, pbOut);
639 /************************************************************************
640 * VarUI1FromUI2 (OLEAUT32.238)
642 * Convert a VT_UI2 to a VT_UI1.
646 * pbOut [O] Destination
650 * Failure: E_INVALIDARG, if the source value is invalid
651 * DISP_E_OVERFLOW, if the value will not fit in the destination
653 HRESULT WINAPI VarUI1FromUI2(USHORT usIn, BYTE* pbOut)
655 return _VarUI1FromUI2(usIn, pbOut);
658 /************************************************************************
659 * VarUI1FromUI4 (OLEAUT32.239)
661 * Convert a VT_UI4 to a VT_UI1.
665 * pbOut [O] Destination
669 * Failure: E_INVALIDARG, if the source value is invalid
670 * DISP_E_OVERFLOW, if the value will not fit in the destination
672 HRESULT WINAPI VarUI1FromUI4(ULONG ulIn, BYTE* pbOut)
674 return _VarUI1FromUI4(ulIn, pbOut);
677 /************************************************************************
678 * VarUI1FromDec (OLEAUT32.240)
680 * Convert a VT_DECIMAL to a VT_UI1.
684 * pbOut [O] Destination
688 * Failure: E_INVALIDARG, if the source value is invalid
689 * DISP_E_OVERFLOW, if the value will not fit in the destination
691 HRESULT WINAPI VarUI1FromDec(DECIMAL *pdecIn, BYTE* pbOut)
696 hRet = _VarI8FromDec(pdecIn, &i64);
699 hRet = _VarUI1FromI8(i64, pbOut);
703 /************************************************************************
704 * VarUI1FromI8 (OLEAUT32.372)
706 * Convert a VT_I8 to a VT_UI1.
710 * pbOut [O] Destination
714 * Failure: E_INVALIDARG, if the source value is invalid
715 * DISP_E_OVERFLOW, if the value will not fit in the destination
717 HRESULT WINAPI VarUI1FromI8(LONG64 llIn, BYTE* pbOut)
719 return _VarUI1FromI8(llIn, pbOut);
722 /************************************************************************
723 * VarUI1FromUI8 (OLEAUT32.373)
725 * Convert a VT_UI8 to a VT_UI1.
729 * pbOut [O] Destination
733 * Failure: E_INVALIDARG, if the source value is invalid
734 * DISP_E_OVERFLOW, if the value will not fit in the destination
736 HRESULT WINAPI VarUI1FromUI8(ULONG64 ullIn, BYTE* pbOut)
738 return _VarUI1FromUI8(ullIn, pbOut);
745 /************************************************************************
746 * VarI2FromUI1 (OLEAUT32.48)
748 * Convert a VT_UI2 to a VT_I2.
752 * psOut [O] Destination
757 HRESULT WINAPI VarI2FromUI1(BYTE bIn, SHORT* psOut)
759 return _VarI2FromUI1(bIn, psOut);
762 /************************************************************************
763 * VarI2FromI4 (OLEAUT32.49)
765 * Convert a VT_I4 to a VT_I2.
769 * psOut [O] Destination
773 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
775 HRESULT WINAPI VarI2FromI4(LONG iIn, SHORT* psOut)
777 return _VarI2FromI4(iIn, psOut);
780 /************************************************************************
781 * VarI2FromR4 (OLEAUT32.50)
783 * Convert a VT_R4 to a VT_I2.
787 * psOut [O] Destination
791 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
793 HRESULT WINAPI VarI2FromR4(FLOAT fltIn, SHORT* psOut)
795 return _VarI2FromR4(fltIn, psOut);
798 /************************************************************************
799 * VarI2FromR8 (OLEAUT32.51)
801 * Convert a VT_R8 to a VT_I2.
805 * psOut [O] Destination
809 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
812 * See VarI8FromR8() for details concerning rounding.
814 HRESULT WINAPI VarI2FromR8(double dblIn, SHORT* psOut)
816 if (dblIn < (double)I2_MIN || dblIn > (double)I2_MAX)
817 return DISP_E_OVERFLOW;
818 OLEAUT32_DutchRound(SHORT, dblIn, *psOut);
822 /************************************************************************
823 * VarI2FromCy (OLEAUT32.52)
825 * Convert a VT_CY to a VT_I2.
829 * psOut [O] Destination
833 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
835 HRESULT WINAPI VarI2FromCy(CY cyIn, SHORT* psOut)
839 _VarI4FromCy(cyIn, &i);
840 return _VarI2FromI4(i, psOut);
843 /************************************************************************
844 * VarI2FromDate (OLEAUT32.53)
846 * Convert a VT_DATE to a VT_I2.
850 * psOut [O] Destination
854 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
856 HRESULT WINAPI VarI2FromDate(DATE dateIn, SHORT* psOut)
858 return _VarI2FromDate(dateIn, psOut);
861 /************************************************************************
862 * VarI2FromStr (OLEAUT32.54)
864 * Convert a VT_BSTR to a VT_I2.
868 * lcid [I] LCID for the conversion
869 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
870 * psOut [O] Destination
874 * Failure: E_INVALIDARG, if any parameter is invalid
875 * DISP_E_OVERFLOW, if the value will not fit in the destination
876 * DISP_E_TYPEMISMATCH, if the type cannot be converted
878 HRESULT WINAPI VarI2FromStr(OLECHAR* strIn, LCID lcid, ULONG dwFlags, SHORT* psOut)
880 return _VarI2FromStr(strIn, lcid, dwFlags, psOut);
883 /************************************************************************
884 * VarI2FromDisp (OLEAUT32.55)
886 * Convert a VT_DISPATCH to a VT_I2.
890 * lcid [I] LCID for conversion
891 * psOut [O] Destination
895 * Failure: E_INVALIDARG, if pdispIn is invalid,
896 * DISP_E_OVERFLOW, if the value will not fit in the destination,
897 * DISP_E_TYPEMISMATCH, if the type cannot be converted
899 HRESULT WINAPI VarI2FromDisp(IDispatch* pdispIn, LCID lcid, SHORT* psOut)
901 return _VarI2FromDisp(pdispIn, lcid, psOut);
904 /************************************************************************
905 * VarI2FromBool (OLEAUT32.56)
907 * Convert a VT_BOOL to a VT_I2.
911 * psOut [O] Destination
916 HRESULT WINAPI VarI2FromBool(VARIANT_BOOL boolIn, SHORT* psOut)
918 return _VarI2FromBool(boolIn, psOut);
921 /************************************************************************
922 * VarI2FromI1 (OLEAUT32.205)
924 * Convert a VT_I1 to a VT_I2.
928 * psOut [O] Destination
933 HRESULT WINAPI VarI2FromI1(signed char cIn, SHORT* psOut)
935 return _VarI2FromI1(cIn, psOut);
938 /************************************************************************
939 * VarI2FromUI2 (OLEAUT32.206)
941 * Convert a VT_UI2 to a VT_I2.
945 * psOut [O] Destination
949 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
951 HRESULT WINAPI VarI2FromUI2(USHORT usIn, SHORT* psOut)
953 return _VarI2FromUI2(usIn, psOut);
956 /************************************************************************
957 * VarI2FromUI4 (OLEAUT32.207)
959 * Convert a VT_UI4 to a VT_I2.
963 * psOut [O] Destination
967 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
969 HRESULT WINAPI VarI2FromUI4(ULONG ulIn, SHORT* psOut)
971 return _VarI2FromUI4(ulIn, psOut);
974 /************************************************************************
975 * VarI2FromDec (OLEAUT32.208)
977 * Convert a VT_DECIMAL to a VT_I2.
981 * psOut [O] Destination
985 * Failure: E_INVALIDARG, if the source value is invalid
986 * DISP_E_OVERFLOW, if the value will not fit in the destination
988 HRESULT WINAPI VarI2FromDec(DECIMAL *pdecIn, SHORT* psOut)
993 hRet = _VarI8FromDec(pdecIn, &i64);
996 hRet = _VarI2FromI8(i64, psOut);
1000 /************************************************************************
1001 * VarI2FromI8 (OLEAUT32.346)
1003 * Convert a VT_I8 to a VT_I2.
1007 * psOut [O] Destination
1011 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1013 HRESULT WINAPI VarI2FromI8(LONG64 llIn, SHORT* psOut)
1015 return _VarI2FromI8(llIn, psOut);
1018 /************************************************************************
1019 * VarI2FromUI8 (OLEAUT32.347)
1021 * Convert a VT_UI8 to a VT_I2.
1025 * psOut [O] Destination
1029 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1031 HRESULT WINAPI VarI2FromUI8(ULONG64 ullIn, SHORT* psOut)
1033 return _VarI2FromUI8(ullIn, psOut);
1039 /************************************************************************
1040 * VarUI2FromUI1 (OLEAUT32.257)
1042 * Convert a VT_UI1 to a VT_UI2.
1046 * pusOut [O] Destination
1051 HRESULT WINAPI VarUI2FromUI1(BYTE bIn, USHORT* pusOut)
1053 return _VarUI2FromUI1(bIn, pusOut);
1056 /************************************************************************
1057 * VarUI2FromI2 (OLEAUT32.258)
1059 * Convert a VT_I2 to a VT_UI2.
1063 * pusOut [O] Destination
1067 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1069 HRESULT WINAPI VarUI2FromI2(SHORT sIn, USHORT* pusOut)
1071 return _VarUI2FromI2(sIn, pusOut);
1074 /************************************************************************
1075 * VarUI2FromI4 (OLEAUT32.259)
1077 * Convert a VT_I4 to a VT_UI2.
1081 * pusOut [O] Destination
1085 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1087 HRESULT WINAPI VarUI2FromI4(LONG iIn, USHORT* pusOut)
1089 return _VarUI2FromI4(iIn, pusOut);
1092 /************************************************************************
1093 * VarUI2FromR4 (OLEAUT32.260)
1095 * Convert a VT_R4 to a VT_UI2.
1099 * pusOut [O] Destination
1103 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1105 HRESULT WINAPI VarUI2FromR4(FLOAT fltIn, USHORT* pusOut)
1107 return _VarUI2FromR4(fltIn, pusOut);
1110 /************************************************************************
1111 * VarUI2FromR8 (OLEAUT32.261)
1113 * Convert a VT_R8 to a VT_UI2.
1117 * pusOut [O] Destination
1121 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1124 * See VarI8FromR8() for details concerning rounding.
1126 HRESULT WINAPI VarUI2FromR8(double dblIn, USHORT* pusOut)
1128 if (dblIn < -0.5 || dblIn > (double)UI2_MAX)
1129 return DISP_E_OVERFLOW;
1130 OLEAUT32_DutchRound(USHORT, dblIn, *pusOut);
1134 /************************************************************************
1135 * VarUI2FromDate (OLEAUT32.262)
1137 * Convert a VT_DATE to a VT_UI2.
1141 * pusOut [O] Destination
1145 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1147 HRESULT WINAPI VarUI2FromDate(DATE dateIn, USHORT* pusOut)
1149 return _VarUI2FromDate(dateIn, pusOut);
1152 /************************************************************************
1153 * VarUI2FromCy (OLEAUT32.263)
1155 * Convert a VT_CY to a VT_UI2.
1159 * pusOut [O] Destination
1163 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1166 * Negative values >= -5000 will be converted to 0.
1168 HRESULT WINAPI VarUI2FromCy(CY cyIn, USHORT* pusOut)
1170 ULONG i = UI2_MAX + 1;
1172 _VarUI4FromCy(cyIn, &i);
1173 return _VarUI2FromUI4(i, pusOut);
1176 /************************************************************************
1177 * VarUI2FromStr (OLEAUT32.264)
1179 * Convert a VT_BSTR to a VT_UI2.
1183 * lcid [I] LCID for the conversion
1184 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1185 * pusOut [O] Destination
1189 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1190 * DISP_E_TYPEMISMATCH, if the type cannot be converted
1192 HRESULT WINAPI VarUI2FromStr(OLECHAR* strIn, LCID lcid, ULONG dwFlags, USHORT* pusOut)
1194 return _VarUI2FromStr(strIn, lcid, dwFlags, pusOut);
1197 /************************************************************************
1198 * VarUI2FromDisp (OLEAUT32.265)
1200 * Convert a VT_DISPATCH to a VT_UI2.
1203 * pdispIn [I] Source
1204 * lcid [I] LCID for conversion
1205 * pusOut [O] Destination
1209 * Failure: E_INVALIDARG, if the source value is invalid
1210 * DISP_E_OVERFLOW, if the value will not fit in the destination
1211 * DISP_E_TYPEMISMATCH, if the type cannot be converted
1213 HRESULT WINAPI VarUI2FromDisp(IDispatch* pdispIn, LCID lcid, USHORT* pusOut)
1215 return _VarUI2FromDisp(pdispIn, lcid, pusOut);
1218 /************************************************************************
1219 * VarUI2FromBool (OLEAUT32.266)
1221 * Convert a VT_BOOL to a VT_UI2.
1225 * pusOut [O] Destination
1230 HRESULT WINAPI VarUI2FromBool(VARIANT_BOOL boolIn, USHORT* pusOut)
1232 return _VarUI2FromBool(boolIn, pusOut);
1235 /************************************************************************
1236 * VarUI2FromI1 (OLEAUT32.267)
1238 * Convert a VT_I1 to a VT_UI2.
1242 * pusOut [O] Destination
1246 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1248 HRESULT WINAPI VarUI2FromI1(signed char cIn, USHORT* pusOut)
1250 return _VarUI2FromI1(cIn, pusOut);
1253 /************************************************************************
1254 * VarUI2FromUI4 (OLEAUT32.268)
1256 * Convert a VT_UI4 to a VT_UI2.
1260 * pusOut [O] Destination
1264 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1266 HRESULT WINAPI VarUI2FromUI4(ULONG ulIn, USHORT* pusOut)
1268 return _VarUI2FromUI4(ulIn, pusOut);
1271 /************************************************************************
1272 * VarUI2FromDec (OLEAUT32.269)
1274 * Convert a VT_DECIMAL to a VT_UI2.
1278 * pusOut [O] Destination
1282 * Failure: E_INVALIDARG, if the source value is invalid
1283 * DISP_E_OVERFLOW, if the value will not fit in the destination
1285 HRESULT WINAPI VarUI2FromDec(DECIMAL *pdecIn, USHORT* pusOut)
1290 hRet = _VarI8FromDec(pdecIn, &i64);
1292 if (SUCCEEDED(hRet))
1293 hRet = _VarUI2FromI8(i64, pusOut);
1297 /************************************************************************
1298 * VarUI2FromI8 (OLEAUT32.378)
1300 * Convert a VT_I8 to a VT_UI2.
1304 * pusOut [O] Destination
1308 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1310 HRESULT WINAPI VarUI2FromI8(LONG64 llIn, USHORT* pusOut)
1312 return _VarUI2FromI8(llIn, pusOut);
1315 /************************************************************************
1316 * VarUI2FromUI8 (OLEAUT32.379)
1318 * Convert a VT_UI8 to a VT_UI2.
1322 * pusOut [O] Destination
1326 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1328 HRESULT WINAPI VarUI2FromUI8(ULONG64 ullIn, USHORT* pusOut)
1330 return _VarUI2FromUI8(ullIn, pusOut);
1336 /************************************************************************
1337 * VarI4FromUI1 (OLEAUT32.58)
1339 * Convert a VT_UI1 to a VT_I4.
1343 * piOut [O] Destination
1348 HRESULT WINAPI VarI4FromUI1(BYTE bIn, LONG *piOut)
1350 return _VarI4FromUI1(bIn, piOut);
1353 /************************************************************************
1354 * VarI4FromI2 (OLEAUT32.59)
1356 * Convert a VT_I2 to a VT_I4.
1360 * piOut [O] Destination
1364 * Failure: E_INVALIDARG, if the source value is invalid
1365 * DISP_E_OVERFLOW, if the value will not fit in the destination
1367 HRESULT WINAPI VarI4FromI2(SHORT sIn, LONG *piOut)
1369 return _VarI4FromI2(sIn, piOut);
1372 /************************************************************************
1373 * VarI4FromR4 (OLEAUT32.60)
1375 * Convert a VT_R4 to a VT_I4.
1379 * piOut [O] Destination
1383 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1385 HRESULT WINAPI VarI4FromR4(FLOAT fltIn, LONG *piOut)
1387 return _VarI4FromR4(fltIn, piOut);
1390 /************************************************************************
1391 * VarI4FromR8 (OLEAUT32.61)
1393 * Convert a VT_R8 to a VT_I4.
1397 * piOut [O] Destination
1401 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1404 * See VarI8FromR8() for details concerning rounding.
1406 HRESULT WINAPI VarI4FromR8(double dblIn, LONG *piOut)
1408 if (dblIn < (double)I4_MIN || dblIn > (double)I4_MAX)
1409 return DISP_E_OVERFLOW;
1410 OLEAUT32_DutchRound(LONG, dblIn, *piOut);
1414 /************************************************************************
1415 * VarI4FromCy (OLEAUT32.62)
1417 * Convert a VT_CY to a VT_I4.
1421 * piOut [O] Destination
1425 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1427 HRESULT WINAPI VarI4FromCy(CY cyIn, LONG *piOut)
1429 double d = cyIn.int64 / CY_MULTIPLIER_F;
1430 return _VarI4FromR8(d, piOut);
1433 /************************************************************************
1434 * VarI4FromDate (OLEAUT32.63)
1436 * Convert a VT_DATE to a VT_I4.
1440 * piOut [O] Destination
1444 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1446 HRESULT WINAPI VarI4FromDate(DATE dateIn, LONG *piOut)
1448 return _VarI4FromDate(dateIn, piOut);
1451 /************************************************************************
1452 * VarI4FromStr (OLEAUT32.64)
1454 * Convert a VT_BSTR to a VT_I4.
1458 * lcid [I] LCID for the conversion
1459 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1460 * piOut [O] Destination
1464 * Failure: E_INVALIDARG, if any parameter is invalid
1465 * DISP_E_OVERFLOW, if the value will not fit in the destination
1466 * DISP_E_TYPEMISMATCH, if strIn cannot be converted
1468 HRESULT WINAPI VarI4FromStr(OLECHAR* strIn, LCID lcid, ULONG dwFlags, LONG *piOut)
1470 return _VarI4FromStr(strIn, lcid, dwFlags, piOut);
1473 /************************************************************************
1474 * VarI4FromDisp (OLEAUT32.65)
1476 * Convert a VT_DISPATCH to a VT_I4.
1479 * pdispIn [I] Source
1480 * lcid [I] LCID for conversion
1481 * piOut [O] Destination
1485 * Failure: E_INVALIDARG, if the source value is invalid
1486 * DISP_E_OVERFLOW, if the value will not fit in the destination
1487 * DISP_E_TYPEMISMATCH, if the type cannot be converted
1489 HRESULT WINAPI VarI4FromDisp(IDispatch* pdispIn, LCID lcid, LONG *piOut)
1491 return _VarI4FromDisp(pdispIn, lcid, piOut);
1494 /************************************************************************
1495 * VarI4FromBool (OLEAUT32.66)
1497 * Convert a VT_BOOL to a VT_I4.
1501 * piOut [O] Destination
1506 HRESULT WINAPI VarI4FromBool(VARIANT_BOOL boolIn, LONG *piOut)
1508 return _VarI4FromBool(boolIn, piOut);
1511 /************************************************************************
1512 * VarI4FromI1 (OLEAUT32.209)
1514 * Convert a VT_I4 to a VT_I4.
1518 * piOut [O] Destination
1523 HRESULT WINAPI VarI4FromI1(signed char cIn, LONG *piOut)
1525 return _VarI4FromI1(cIn, piOut);
1528 /************************************************************************
1529 * VarI4FromUI2 (OLEAUT32.210)
1531 * Convert a VT_UI2 to a VT_I4.
1535 * piOut [O] Destination
1540 HRESULT WINAPI VarI4FromUI2(USHORT usIn, LONG *piOut)
1542 return _VarI4FromUI2(usIn, piOut);
1545 /************************************************************************
1546 * VarI4FromUI4 (OLEAUT32.211)
1548 * Convert a VT_UI4 to a VT_I4.
1552 * piOut [O] Destination
1556 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1558 HRESULT WINAPI VarI4FromUI4(ULONG ulIn, LONG *piOut)
1560 return _VarI4FromUI4(ulIn, piOut);
1563 /************************************************************************
1564 * VarI4FromDec (OLEAUT32.212)
1566 * Convert a VT_DECIMAL to a VT_I4.
1570 * piOut [O] Destination
1574 * Failure: E_INVALIDARG, if pdecIn is invalid
1575 * DISP_E_OVERFLOW, if the value will not fit in the destination
1577 HRESULT WINAPI VarI4FromDec(DECIMAL *pdecIn, LONG *piOut)
1582 hRet = _VarI8FromDec(pdecIn, &i64);
1584 if (SUCCEEDED(hRet))
1585 hRet = _VarI4FromI8(i64, piOut);
1589 /************************************************************************
1590 * VarI4FromI8 (OLEAUT32.348)
1592 * Convert a VT_I8 to a VT_I4.
1596 * piOut [O] Destination
1600 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1602 HRESULT WINAPI VarI4FromI8(LONG64 llIn, LONG *piOut)
1604 return _VarI4FromI8(llIn, piOut);
1607 /************************************************************************
1608 * VarI4FromUI8 (OLEAUT32.349)
1610 * Convert a VT_UI8 to a VT_I4.
1614 * piOut [O] Destination
1618 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1620 HRESULT WINAPI VarI4FromUI8(ULONG64 ullIn, LONG *piOut)
1622 return _VarI4FromUI8(ullIn, piOut);
1628 /************************************************************************
1629 * VarUI4FromUI1 (OLEAUT32.270)
1631 * Convert a VT_UI1 to a VT_UI4.
1635 * pulOut [O] Destination
1640 HRESULT WINAPI VarUI4FromUI1(BYTE bIn, ULONG *pulOut)
1642 return _VarUI4FromUI1(bIn, pulOut);
1645 /************************************************************************
1646 * VarUI4FromI2 (OLEAUT32.271)
1648 * Convert a VT_I2 to a VT_UI4.
1652 * pulOut [O] Destination
1656 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1658 HRESULT WINAPI VarUI4FromI2(SHORT sIn, ULONG *pulOut)
1660 return _VarUI4FromI2(sIn, pulOut);
1663 /************************************************************************
1664 * VarUI4FromI4 (OLEAUT32.272)
1666 * Convert a VT_I4 to a VT_UI4.
1670 * pulOut [O] Destination
1674 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1676 HRESULT WINAPI VarUI4FromI4(LONG iIn, ULONG *pulOut)
1678 return _VarUI4FromI4(iIn, pulOut);
1681 /************************************************************************
1682 * VarUI4FromR4 (OLEAUT32.273)
1684 * Convert a VT_R4 to a VT_UI4.
1688 * pulOut [O] Destination
1692 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1694 HRESULT WINAPI VarUI4FromR4(FLOAT fltIn, ULONG *pulOut)
1696 return _VarUI4FromR4(fltIn, pulOut);
1699 /************************************************************************
1700 * VarUI4FromR8 (OLEAUT32.274)
1702 * Convert a VT_R8 to a VT_UI4.
1706 * pulOut [O] Destination
1710 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1713 * See VarI8FromR8() for details concerning rounding.
1715 HRESULT WINAPI VarUI4FromR8(double dblIn, ULONG *pulOut)
1717 if (dblIn < -0.5 || dblIn > (double)UI4_MAX)
1718 return DISP_E_OVERFLOW;
1719 OLEAUT32_DutchRound(ULONG, dblIn, *pulOut);
1723 /************************************************************************
1724 * VarUI4FromDate (OLEAUT32.275)
1726 * Convert a VT_DATE to a VT_UI4.
1730 * pulOut [O] Destination
1734 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1736 HRESULT WINAPI VarUI4FromDate(DATE dateIn, ULONG *pulOut)
1738 return _VarUI4FromDate(dateIn, pulOut);
1741 /************************************************************************
1742 * VarUI4FromCy (OLEAUT32.276)
1744 * Convert a VT_CY to a VT_UI4.
1748 * pulOut [O] Destination
1752 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1754 HRESULT WINAPI VarUI4FromCy(CY cyIn, ULONG *pulOut)
1756 double d = cyIn.int64 / CY_MULTIPLIER_F;
1757 return _VarUI4FromR8(d, pulOut);
1760 /************************************************************************
1761 * VarUI4FromStr (OLEAUT32.277)
1763 * Convert a VT_BSTR to a VT_UI4.
1767 * lcid [I] LCID for the conversion
1768 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1769 * pulOut [O] Destination
1773 * Failure: E_INVALIDARG, if any parameter is invalid
1774 * DISP_E_OVERFLOW, if the value will not fit in the destination
1775 * DISP_E_TYPEMISMATCH, if strIn cannot be converted
1777 HRESULT WINAPI VarUI4FromStr(OLECHAR* strIn, LCID lcid, ULONG dwFlags, ULONG *pulOut)
1779 return _VarUI4FromStr(strIn, lcid, dwFlags, pulOut);
1782 /************************************************************************
1783 * VarUI4FromDisp (OLEAUT32.278)
1785 * Convert a VT_DISPATCH to a VT_UI4.
1788 * pdispIn [I] Source
1789 * lcid [I] LCID for conversion
1790 * pulOut [O] Destination
1794 * Failure: E_INVALIDARG, if the source value is invalid
1795 * DISP_E_OVERFLOW, if the value will not fit in the destination
1796 * DISP_E_TYPEMISMATCH, if the type cannot be converted
1798 HRESULT WINAPI VarUI4FromDisp(IDispatch* pdispIn, LCID lcid, ULONG *pulOut)
1800 return _VarUI4FromDisp(pdispIn, lcid, pulOut);
1803 /************************************************************************
1804 * VarUI4FromBool (OLEAUT32.279)
1806 * Convert a VT_BOOL to a VT_UI4.
1810 * pulOut [O] Destination
1815 HRESULT WINAPI VarUI4FromBool(VARIANT_BOOL boolIn, ULONG *pulOut)
1817 return _VarUI4FromBool(boolIn, pulOut);
1820 /************************************************************************
1821 * VarUI4FromI1 (OLEAUT32.280)
1823 * Convert a VT_I1 to a VT_UI4.
1827 * pulOut [O] Destination
1831 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1833 HRESULT WINAPI VarUI4FromI1(signed char cIn, ULONG *pulOut)
1835 return _VarUI4FromI1(cIn, pulOut);
1838 /************************************************************************
1839 * VarUI4FromUI2 (OLEAUT32.281)
1841 * Convert a VT_UI2 to a VT_UI4.
1845 * pulOut [O] Destination
1850 HRESULT WINAPI VarUI4FromUI2(USHORT usIn, ULONG *pulOut)
1852 return _VarUI4FromUI2(usIn, pulOut);
1855 /************************************************************************
1856 * VarUI4FromDec (OLEAUT32.282)
1858 * Convert a VT_DECIMAL to a VT_UI4.
1862 * pulOut [O] Destination
1866 * Failure: E_INVALIDARG, if pdecIn is invalid
1867 * DISP_E_OVERFLOW, if the value will not fit in the destination
1869 HRESULT WINAPI VarUI4FromDec(DECIMAL *pdecIn, ULONG *pulOut)
1874 hRet = _VarI8FromDec(pdecIn, &i64);
1876 if (SUCCEEDED(hRet))
1877 hRet = _VarUI4FromI8(i64, pulOut);
1881 /************************************************************************
1882 * VarUI4FromI8 (OLEAUT32.425)
1884 * Convert a VT_I8 to a VT_UI4.
1888 * pulOut [O] Destination
1892 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1894 HRESULT WINAPI VarUI4FromI8(LONG64 llIn, ULONG *pulOut)
1896 return _VarUI4FromI8(llIn, pulOut);
1899 /************************************************************************
1900 * VarUI4FromUI8 (OLEAUT32.426)
1902 * Convert a VT_UI8 to a VT_UI4.
1906 * pulOut [O] Destination
1910 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
1912 HRESULT WINAPI VarUI4FromUI8(ULONG64 ullIn, ULONG *pulOut)
1914 return _VarUI4FromUI8(ullIn, pulOut);
1920 /************************************************************************
1921 * VarI8FromUI1 (OLEAUT32.333)
1923 * Convert a VT_UI1 to a VT_I8.
1927 * pi64Out [O] Destination
1932 HRESULT WINAPI VarI8FromUI1(BYTE bIn, LONG64* pi64Out)
1934 return _VarI8FromUI1(bIn, pi64Out);
1938 /************************************************************************
1939 * VarI8FromI2 (OLEAUT32.334)
1941 * Convert a VT_I2 to a VT_I8.
1945 * pi64Out [O] Destination
1950 HRESULT WINAPI VarI8FromI2(SHORT sIn, LONG64* pi64Out)
1952 return _VarI8FromI2(sIn, pi64Out);
1955 /************************************************************************
1956 * VarI8FromR4 (OLEAUT32.335)
1958 * Convert a VT_R4 to a VT_I8.
1962 * pi64Out [O] Destination
1966 * Failure: E_INVALIDARG, if the source value is invalid
1967 * DISP_E_OVERFLOW, if the value will not fit in the destination
1969 HRESULT WINAPI VarI8FromR4(FLOAT fltIn, LONG64* pi64Out)
1971 return _VarI8FromR4(fltIn, pi64Out);
1974 /************************************************************************
1975 * VarI8FromR8 (OLEAUT32.336)
1977 * Convert a VT_R8 to a VT_I8.
1981 * pi64Out [O] Destination
1985 * Failure: E_INVALIDARG, if the source value is invalid
1986 * DISP_E_OVERFLOW, if the value will not fit in the destination
1989 * Only values that fit into 63 bits are accepted. Due to rounding issues,
1990 * very high or low values will not be accurately converted.
1992 * Numbers are rounded using Dutch rounding, as follows:
1994 *| Fractional Part Sign Direction Example
1995 *| --------------- ---- --------- -------
1996 *| < 0.5 + Down 0.4 -> 0.0
1997 *| < 0.5 - Up -0.4 -> 0.0
1998 *| > 0.5 + Up 0.6 -> 1.0
1999 *| < 0.5 - Up -0.6 -> -1.0
2000 *| = 0.5 + Up/Down Down if even, Up if odd
2001 *| = 0.5 - Up/Down Up if even, Down if odd
2003 * This system is often used in supermarkets.
2005 HRESULT WINAPI VarI8FromR8(double dblIn, LONG64* pi64Out)
2007 if ( dblIn < -4611686018427387904.0 || dblIn >= 4611686018427387904.0)
2008 return DISP_E_OVERFLOW;
2009 OLEAUT32_DutchRound(LONG64, dblIn, *pi64Out);
2013 /************************************************************************
2014 * VarI8FromCy (OLEAUT32.337)
2016 * Convert a VT_CY to a VT_I8.
2020 * pi64Out [O] Destination
2026 * All negative numbers are rounded down by 1, including those that are
2027 * evenly divisible by 10000 (this is a Win32 bug that Wine mimics).
2028 * Positive numbers are rounded using Dutch rounding: See VarI8FromR8()
2031 HRESULT WINAPI VarI8FromCy(CY cyIn, LONG64* pi64Out)
2033 *pi64Out = cyIn.int64 / CY_MULTIPLIER;
2036 (*pi64Out)--; /* Mimic Win32 bug */
2039 cyIn.int64 -= *pi64Out * CY_MULTIPLIER; /* cyIn.s.Lo now holds fractional remainder */
2041 if (cyIn.s.Lo > CY_HALF || (cyIn.s.Lo == CY_HALF && (*pi64Out & 0x1)))
2047 /************************************************************************
2048 * VarI8FromDate (OLEAUT32.338)
2050 * Convert a VT_DATE to a VT_I8.
2054 * pi64Out [O] Destination
2058 * Failure: E_INVALIDARG, if the source value is invalid
2059 * DISP_E_OVERFLOW, if the value will not fit in the destination
2060 * DISP_E_TYPEMISMATCH, if the type cannot be converted
2062 HRESULT WINAPI VarI8FromDate(DATE dateIn, LONG64* pi64Out)
2064 return _VarI8FromDate(dateIn, pi64Out);
2067 /************************************************************************
2068 * VarI8FromStr (OLEAUT32.339)
2070 * Convert a VT_BSTR to a VT_I8.
2074 * lcid [I] LCID for the conversion
2075 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
2076 * pi64Out [O] Destination
2080 * Failure: E_INVALIDARG, if the source value is invalid
2081 * DISP_E_OVERFLOW, if the value will not fit in the destination
2082 * DISP_E_TYPEMISMATCH, if the type cannot be converted
2084 HRESULT WINAPI VarI8FromStr(OLECHAR* strIn, LCID lcid, ULONG dwFlags, LONG64* pi64Out)
2086 return _VarI8FromStr(strIn, lcid, dwFlags, pi64Out);
2089 /************************************************************************
2090 * VarI8FromDisp (OLEAUT32.340)
2092 * Convert a VT_DISPATCH to a VT_I8.
2095 * pdispIn [I] Source
2096 * lcid [I] LCID for conversion
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
2103 * DISP_E_TYPEMISMATCH, if the type cannot be converted
2105 HRESULT WINAPI VarI8FromDisp(IDispatch* pdispIn, LCID lcid, LONG64* pi64Out)
2107 return _VarI8FromDisp(pdispIn, lcid, pi64Out);
2110 /************************************************************************
2111 * VarI8FromBool (OLEAUT32.341)
2113 * Convert a VT_BOOL to a VT_I8.
2117 * pi64Out [O] Destination
2122 HRESULT WINAPI VarI8FromBool(VARIANT_BOOL boolIn, LONG64* pi64Out)
2124 return _VarI8FromBool(boolIn, pi64Out);
2127 /************************************************************************
2128 * VarI8FromI1 (OLEAUT32.342)
2130 * Convert a VT_I1 to a VT_I8.
2134 * pi64Out [O] Destination
2139 HRESULT WINAPI VarI8FromI1(signed char cIn, LONG64* pi64Out)
2141 return _VarI8FromI1(cIn, pi64Out);
2144 /************************************************************************
2145 * VarI8FromUI2 (OLEAUT32.343)
2147 * Convert a VT_UI2 to a VT_I8.
2151 * pi64Out [O] Destination
2156 HRESULT WINAPI VarI8FromUI2(USHORT usIn, LONG64* pi64Out)
2158 return _VarI8FromUI2(usIn, pi64Out);
2161 /************************************************************************
2162 * VarI8FromUI4 (OLEAUT32.344)
2164 * Convert a VT_UI4 to a VT_I8.
2168 * pi64Out [O] Destination
2173 HRESULT WINAPI VarI8FromUI4(ULONG ulIn, LONG64* pi64Out)
2175 return _VarI8FromUI4(ulIn, pi64Out);
2178 /************************************************************************
2179 * VarI8FromDec (OLEAUT32.345)
2181 * Convert a VT_DECIMAL to a VT_I8.
2185 * pi64Out [O] Destination
2189 * Failure: E_INVALIDARG, if the source value is invalid
2190 * DISP_E_OVERFLOW, if the value will not fit in the destination
2192 HRESULT WINAPI VarI8FromDec(DECIMAL *pdecIn, LONG64* pi64Out)
2194 if (!DEC_SCALE(pdecIn))
2196 /* This decimal is just a 96 bit integer */
2197 if (DEC_SIGN(pdecIn) & ~DECIMAL_NEG)
2198 return E_INVALIDARG;
2200 if (DEC_HI32(pdecIn) || DEC_MID32(pdecIn) & 0x80000000)
2201 return DISP_E_OVERFLOW;
2203 if (DEC_SIGN(pdecIn))
2204 *pi64Out = -DEC_LO64(pdecIn);
2206 *pi64Out = DEC_LO64(pdecIn);
2211 /* Decimal contains a floating point number */
2215 hRet = _VarR8FromDec(pdecIn, &dbl);
2216 if (SUCCEEDED(hRet))
2217 hRet = VarI8FromR8(dbl, pi64Out);
2222 /************************************************************************
2223 * VarI8FromUI8 (OLEAUT32.427)
2225 * Convert a VT_UI8 to a VT_I8.
2229 * pi64Out [O] Destination
2233 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
2235 HRESULT WINAPI VarI8FromUI8(ULONG64 ullIn, LONG64* pi64Out)
2237 return _VarI8FromUI8(ullIn, pi64Out);
2243 /************************************************************************
2244 * VarUI8FromI8 (OLEAUT32.428)
2246 * Convert a VT_I8 to a VT_UI8.
2250 * pui64Out [O] Destination
2254 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
2256 HRESULT WINAPI VarUI8FromI8(LONG64 llIn, ULONG64* pui64Out)
2258 return _VarUI8FromI8(llIn, pui64Out);
2261 /************************************************************************
2262 * VarUI8FromUI1 (OLEAUT32.429)
2264 * Convert a VT_UI1 to a VT_UI8.
2268 * pui64Out [O] Destination
2273 HRESULT WINAPI VarUI8FromUI1(BYTE bIn, ULONG64* pui64Out)
2275 return _VarUI8FromUI1(bIn, pui64Out);
2278 /************************************************************************
2279 * VarUI8FromI2 (OLEAUT32.430)
2281 * Convert a VT_I2 to a VT_UI8.
2285 * pui64Out [O] Destination
2290 HRESULT WINAPI VarUI8FromI2(SHORT sIn, ULONG64* pui64Out)
2292 return _VarUI8FromI2(sIn, pui64Out);
2295 /************************************************************************
2296 * VarUI8FromR4 (OLEAUT32.431)
2298 * Convert a VT_R4 to a VT_UI8.
2302 * pui64Out [O] Destination
2306 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
2308 HRESULT WINAPI VarUI8FromR4(FLOAT fltIn, ULONG64* pui64Out)
2310 return _VarUI8FromR4(fltIn, pui64Out);
2313 /************************************************************************
2314 * VarUI8FromR8 (OLEAUT32.432)
2316 * Convert a VT_R8 to a VT_UI8.
2320 * pui64Out [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
2328 * See VarI8FromR8() for details concerning rounding.
2330 HRESULT WINAPI VarUI8FromR8(double dblIn, ULONG64* pui64Out)
2332 if (dblIn < -0.5 || dblIn > 1.844674407370955e19)
2333 return DISP_E_OVERFLOW;
2334 OLEAUT32_DutchRound(ULONG64, dblIn, *pui64Out);
2338 /************************************************************************
2339 * VarUI8FromCy (OLEAUT32.433)
2341 * Convert a VT_CY to a VT_UI8.
2345 * pui64Out [O] Destination
2349 * Failure: E_INVALIDARG, if the source value is invalid
2350 * DISP_E_OVERFLOW, if the value will not fit in the destination
2353 * Negative values >= -5000 will be converted to 0.
2355 HRESULT WINAPI VarUI8FromCy(CY cyIn, ULONG64* pui64Out)
2359 if (cyIn.int64 < -CY_HALF)
2360 return DISP_E_OVERFLOW;
2365 *pui64Out = cyIn.int64 / CY_MULTIPLIER;
2367 cyIn.int64 -= *pui64Out * CY_MULTIPLIER; /* cyIn.s.Lo now holds fractional remainder */
2369 if (cyIn.s.Lo > CY_HALF || (cyIn.s.Lo == CY_HALF && (*pui64Out & 0x1)))
2375 /************************************************************************
2376 * VarUI8FromDate (OLEAUT32.434)
2378 * Convert a VT_DATE to a VT_UI8.
2382 * pui64Out [O] Destination
2386 * Failure: E_INVALIDARG, if the source value is invalid
2387 * DISP_E_OVERFLOW, if the value will not fit in the destination
2388 * DISP_E_TYPEMISMATCH, if the type cannot be converted
2390 HRESULT WINAPI VarUI8FromDate(DATE dateIn, ULONG64* pui64Out)
2392 return _VarUI8FromDate(dateIn, pui64Out);
2395 /************************************************************************
2396 * VarUI8FromStr (OLEAUT32.435)
2398 * Convert a VT_BSTR to a VT_UI8.
2402 * lcid [I] LCID for the conversion
2403 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
2404 * pui64Out [O] Destination
2408 * Failure: E_INVALIDARG, if the source value is invalid
2409 * DISP_E_OVERFLOW, if the value will not fit in the destination
2410 * DISP_E_TYPEMISMATCH, if the type cannot be converted
2412 HRESULT WINAPI VarUI8FromStr(OLECHAR* strIn, LCID lcid, ULONG dwFlags, ULONG64* pui64Out)
2414 return _VarUI8FromStr(strIn, lcid, dwFlags, pui64Out);
2417 /************************************************************************
2418 * VarUI8FromDisp (OLEAUT32.436)
2420 * Convert a VT_DISPATCH to a VT_UI8.
2423 * pdispIn [I] Source
2424 * lcid [I] LCID for conversion
2425 * pui64Out [O] Destination
2429 * Failure: E_INVALIDARG, if the source value is invalid
2430 * DISP_E_OVERFLOW, if the value will not fit in the destination
2431 * DISP_E_TYPEMISMATCH, if the type cannot be converted
2433 HRESULT WINAPI VarUI8FromDisp(IDispatch* pdispIn, LCID lcid, ULONG64* pui64Out)
2435 return _VarUI8FromDisp(pdispIn, lcid, pui64Out);
2438 /************************************************************************
2439 * VarUI8FromBool (OLEAUT32.437)
2441 * Convert a VT_BOOL to a VT_UI8.
2445 * pui64Out [O] Destination
2449 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
2451 HRESULT WINAPI VarUI8FromBool(VARIANT_BOOL boolIn, ULONG64* pui64Out)
2453 return _VarUI8FromBool(boolIn, pui64Out);
2455 /************************************************************************
2456 * VarUI8FromI1 (OLEAUT32.438)
2458 * Convert a VT_I1 to a VT_UI8.
2462 * pui64Out [O] Destination
2466 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
2468 HRESULT WINAPI VarUI8FromI1(signed char cIn, ULONG64* pui64Out)
2470 return _VarUI8FromI1(cIn, pui64Out);
2473 /************************************************************************
2474 * VarUI8FromUI2 (OLEAUT32.439)
2476 * Convert a VT_UI2 to a VT_UI8.
2480 * pui64Out [O] Destination
2485 HRESULT WINAPI VarUI8FromUI2(USHORT usIn, ULONG64* pui64Out)
2487 return _VarUI8FromUI2(usIn, pui64Out);
2490 /************************************************************************
2491 * VarUI8FromUI4 (OLEAUT32.440)
2493 * Convert a VT_UI4 to a VT_UI8.
2497 * pui64Out [O] Destination
2502 HRESULT WINAPI VarUI8FromUI4(ULONG ulIn, ULONG64* pui64Out)
2504 return _VarUI8FromUI4(ulIn, pui64Out);
2507 /************************************************************************
2508 * VarUI8FromDec (OLEAUT32.441)
2510 * Convert a VT_DECIMAL to a VT_UI8.
2514 * pui64Out [O] Destination
2518 * Failure: E_INVALIDARG, if the source value is invalid
2519 * DISP_E_OVERFLOW, if the value will not fit in the destination
2522 * Under native Win32, if the source value has a scale of 0, its sign is
2523 * ignored, i.e. this function takes the absolute value rather than fail
2524 * with DISP_E_OVERFLOW. This bug has been fixed in Wine's implementation
2525 * (use VarAbs() on pDecIn first if you really want this behaviour).
2527 HRESULT WINAPI VarUI8FromDec(DECIMAL *pdecIn, ULONG64* pui64Out)
2529 if (!DEC_SCALE(pdecIn))
2531 /* This decimal is just a 96 bit integer */
2532 if (DEC_SIGN(pdecIn) & ~DECIMAL_NEG)
2533 return E_INVALIDARG;
2535 if (DEC_HI32(pdecIn))
2536 return DISP_E_OVERFLOW;
2538 if (DEC_SIGN(pdecIn))
2540 WARN("Sign would be ignored under Win32!\n");
2541 return DISP_E_OVERFLOW;
2544 *pui64Out = DEC_LO64(pdecIn);
2549 /* Decimal contains a floating point number */
2553 hRet = _VarR8FromDec(pdecIn, &dbl);
2554 if (SUCCEEDED(hRet))
2555 hRet = VarUI8FromR8(dbl, pui64Out);
2563 /************************************************************************
2564 * VarR4FromUI1 (OLEAUT32.68)
2566 * Convert a VT_UI1 to a VT_R4.
2570 * pFltOut [O] Destination
2575 HRESULT WINAPI VarR4FromUI1(BYTE bIn, float *pFltOut)
2577 return _VarR4FromUI1(bIn, pFltOut);
2580 /************************************************************************
2581 * VarR4FromI2 (OLEAUT32.69)
2583 * Convert a VT_I2 to a VT_R4.
2587 * pFltOut [O] Destination
2592 HRESULT WINAPI VarR4FromI2(SHORT sIn, float *pFltOut)
2594 return _VarR4FromI2(sIn, pFltOut);
2597 /************************************************************************
2598 * VarR4FromI4 (OLEAUT32.70)
2600 * Convert a VT_I4 to a VT_R4.
2604 * pFltOut [O] Destination
2609 HRESULT WINAPI VarR4FromI4(LONG lIn, float *pFltOut)
2611 return _VarR4FromI4(lIn, pFltOut);
2614 /************************************************************************
2615 * VarR4FromR8 (OLEAUT32.71)
2617 * Convert a VT_R8 to a VT_R4.
2621 * pFltOut [O] Destination
2625 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination.
2627 HRESULT WINAPI VarR4FromR8(double dblIn, float *pFltOut)
2629 return _VarR4FromR8(dblIn, pFltOut);
2632 /************************************************************************
2633 * VarR4FromCy (OLEAUT32.72)
2635 * Convert a VT_CY to a VT_R4.
2639 * pFltOut [O] Destination
2644 HRESULT WINAPI VarR4FromCy(CY cyIn, float *pFltOut)
2646 return _VarR4FromCy(cyIn, pFltOut);
2649 /************************************************************************
2650 * VarR4FromDate (OLEAUT32.73)
2652 * Convert a VT_DATE to a VT_R4.
2656 * pFltOut [O] Destination
2660 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination.
2662 HRESULT WINAPI VarR4FromDate(DATE dateIn, float *pFltOut)
2664 return _VarR4FromDate(dateIn, pFltOut);
2667 /************************************************************************
2668 * VarR4FromStr (OLEAUT32.74)
2670 * Convert a VT_BSTR to a VT_R4.
2674 * lcid [I] LCID for the conversion
2675 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
2676 * pFltOut [O] Destination
2680 * Failure: E_INVALIDARG, if strIn or pFltOut is invalid.
2681 * DISP_E_TYPEMISMATCH, if the type cannot be converted
2683 HRESULT WINAPI VarR4FromStr(OLECHAR* strIn, LCID lcid, ULONG dwFlags, float *pFltOut)
2685 return _VarR4FromStr(strIn, lcid, dwFlags, pFltOut);
2688 /************************************************************************
2689 * VarR4FromDisp (OLEAUT32.75)
2691 * Convert a VT_DISPATCH to a VT_R4.
2694 * pdispIn [I] Source
2695 * lcid [I] LCID for conversion
2696 * pFltOut [O] Destination
2700 * Failure: E_INVALIDARG, if the source value is invalid
2701 * DISP_E_OVERFLOW, if the value will not fit in the destination
2702 * DISP_E_TYPEMISMATCH, if the type cannot be converted
2704 HRESULT WINAPI VarR4FromDisp(IDispatch* pdispIn, LCID lcid, float *pFltOut)
2706 return _VarR4FromDisp(pdispIn, lcid, pFltOut);
2709 /************************************************************************
2710 * VarR4FromBool (OLEAUT32.76)
2712 * Convert a VT_BOOL to a VT_R4.
2716 * pFltOut [O] Destination
2721 HRESULT WINAPI VarR4FromBool(VARIANT_BOOL boolIn, float *pFltOut)
2723 return _VarR4FromBool(boolIn, pFltOut);
2726 /************************************************************************
2727 * VarR4FromI1 (OLEAUT32.213)
2729 * Convert a VT_I1 to a VT_R4.
2733 * pFltOut [O] Destination
2737 * Failure: E_INVALIDARG, if the source value is invalid
2738 * DISP_E_OVERFLOW, if the value will not fit in the destination
2739 * DISP_E_TYPEMISMATCH, if the type cannot be converted
2741 HRESULT WINAPI VarR4FromI1(signed char cIn, float *pFltOut)
2743 return _VarR4FromI1(cIn, pFltOut);
2746 /************************************************************************
2747 * VarR4FromUI2 (OLEAUT32.214)
2749 * Convert a VT_UI2 to a VT_R4.
2753 * pFltOut [O] Destination
2757 * Failure: E_INVALIDARG, if the source value is invalid
2758 * DISP_E_OVERFLOW, if the value will not fit in the destination
2759 * DISP_E_TYPEMISMATCH, if the type cannot be converted
2761 HRESULT WINAPI VarR4FromUI2(USHORT usIn, float *pFltOut)
2763 return _VarR4FromUI2(usIn, pFltOut);
2766 /************************************************************************
2767 * VarR4FromUI4 (OLEAUT32.215)
2769 * Convert a VT_UI4 to a VT_R4.
2773 * pFltOut [O] Destination
2777 * Failure: E_INVALIDARG, if the source value is invalid
2778 * DISP_E_OVERFLOW, if the value will not fit in the destination
2779 * DISP_E_TYPEMISMATCH, if the type cannot be converted
2781 HRESULT WINAPI VarR4FromUI4(ULONG ulIn, float *pFltOut)
2783 return _VarR4FromUI4(ulIn, pFltOut);
2786 /************************************************************************
2787 * VarR4FromDec (OLEAUT32.216)
2789 * Convert a VT_DECIMAL to a VT_R4.
2793 * pFltOut [O] Destination
2797 * Failure: E_INVALIDARG, if the source value is invalid.
2799 HRESULT WINAPI VarR4FromDec(DECIMAL* pDecIn, float *pFltOut)
2801 BYTE scale = DEC_SCALE(pDecIn);
2805 if (scale > DEC_MAX_SCALE || DEC_SIGN(pDecIn) & ~DECIMAL_NEG)
2806 return E_INVALIDARG;
2811 if (DEC_SIGN(pDecIn))
2814 if (DEC_HI32(pDecIn))
2816 highPart = (double)DEC_HI32(pDecIn) / (double)divisor;
2822 *pFltOut = (double)DEC_LO64(pDecIn) / (double)divisor + highPart;
2826 /************************************************************************
2827 * VarR4FromI8 (OLEAUT32.360)
2829 * Convert a VT_I8 to a VT_R4.
2833 * pFltOut [O] Destination
2838 HRESULT WINAPI VarR4FromI8(LONG64 llIn, float *pFltOut)
2840 return _VarR4FromI8(llIn, pFltOut);
2843 /************************************************************************
2844 * VarR4FromUI8 (OLEAUT32.361)
2846 * Convert a VT_UI8 to a VT_R4.
2850 * pFltOut [O] Destination
2855 HRESULT WINAPI VarR4FromUI8(ULONG64 ullIn, float *pFltOut)
2857 return _VarR4FromUI8(ullIn, pFltOut);
2860 /************************************************************************
2861 * VarR4CmpR8 (OLEAUT32.316)
2863 * Compare a VT_R4 to a VT_R8.
2866 * fltLeft [I] Source
2867 * dblRight [I] Value to compare
2870 * VARCMP_LT, VARCMP_EQ or VARCMP_GT indicating that fltLeft is less than,
2871 * equal to or greater than dblRight respectively.
2873 HRESULT WINAPI VarR4CmpR8(float fltLeft, double dblRight)
2875 if (fltLeft < dblRight)
2877 else if (fltLeft > dblRight)
2885 /************************************************************************
2886 * VarR8FromUI1 (OLEAUT32.78)
2888 * Convert a VT_UI1 to a VT_R8.
2892 * pDblOut [O] Destination
2897 HRESULT WINAPI VarR8FromUI1(BYTE bIn, double *pDblOut)
2899 return _VarR8FromUI1(bIn, pDblOut);
2902 /************************************************************************
2903 * VarR8FromI2 (OLEAUT32.79)
2905 * Convert a VT_I2 to a VT_R8.
2909 * pDblOut [O] Destination
2914 HRESULT WINAPI VarR8FromI2(SHORT sIn, double *pDblOut)
2916 return _VarR8FromI2(sIn, pDblOut);
2919 /************************************************************************
2920 * VarR8FromI4 (OLEAUT32.80)
2922 * Convert a VT_I4 to a VT_R8.
2926 * pDblOut [O] Destination
2931 HRESULT WINAPI VarR8FromI4(LONG lIn, double *pDblOut)
2933 return _VarR8FromI4(lIn, pDblOut);
2936 /************************************************************************
2937 * VarR8FromR4 (OLEAUT32.81)
2939 * Convert a VT_R4 to a VT_R8.
2943 * pDblOut [O] Destination
2948 HRESULT WINAPI VarR8FromR4(FLOAT fltIn, double *pDblOut)
2950 return _VarR8FromR4(fltIn, pDblOut);
2953 /************************************************************************
2954 * VarR8FromCy (OLEAUT32.82)
2956 * Convert a VT_CY to a VT_R8.
2960 * pDblOut [O] Destination
2965 HRESULT WINAPI VarR8FromCy(CY cyIn, double *pDblOut)
2967 return _VarR8FromCy(cyIn, pDblOut);
2970 /************************************************************************
2971 * VarR8FromDate (OLEAUT32.83)
2973 * Convert a VT_DATE to a VT_R8.
2977 * pDblOut [O] Destination
2982 HRESULT WINAPI VarR8FromDate(DATE dateIn, double *pDblOut)
2984 return _VarR8FromDate(dateIn, pDblOut);
2987 /************************************************************************
2988 * VarR8FromStr (OLEAUT32.84)
2990 * Convert a VT_BSTR to a VT_R8.
2994 * lcid [I] LCID for the conversion
2995 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
2996 * pDblOut [O] Destination
3000 * Failure: E_INVALIDARG, if strIn or pDblOut is invalid.
3001 * DISP_E_TYPEMISMATCH, if the type cannot be converted
3003 HRESULT WINAPI VarR8FromStr(OLECHAR* strIn, LCID lcid, ULONG dwFlags, double *pDblOut)
3005 return _VarR8FromStr(strIn, lcid, dwFlags, pDblOut);
3008 /************************************************************************
3009 * VarR8FromDisp (OLEAUT32.85)
3011 * Convert a VT_DISPATCH to a VT_R8.
3014 * pdispIn [I] Source
3015 * lcid [I] LCID for conversion
3016 * pDblOut [O] Destination
3020 * Failure: E_INVALIDARG, if the source value is invalid
3021 * DISP_E_OVERFLOW, if the value will not fit in the destination
3022 * DISP_E_TYPEMISMATCH, if the type cannot be converted
3024 HRESULT WINAPI VarR8FromDisp(IDispatch* pdispIn, LCID lcid, double *pDblOut)
3026 return _VarR8FromDisp(pdispIn, lcid, pDblOut);
3029 /************************************************************************
3030 * VarR8FromBool (OLEAUT32.86)
3032 * Convert a VT_BOOL to a VT_R8.
3036 * pDblOut [O] Destination
3041 HRESULT WINAPI VarR8FromBool(VARIANT_BOOL boolIn, double *pDblOut)
3043 return _VarR8FromBool(boolIn, pDblOut);
3046 /************************************************************************
3047 * VarR8FromI1 (OLEAUT32.217)
3049 * Convert a VT_I1 to a VT_R8.
3053 * pDblOut [O] Destination
3057 * Failure: E_INVALIDARG, if the source value is invalid
3058 * DISP_E_OVERFLOW, if the value will not fit in the destination
3059 * DISP_E_TYPEMISMATCH, if the type cannot be converted
3061 HRESULT WINAPI VarR8FromI1(signed char cIn, double *pDblOut)
3063 return _VarR8FromI1(cIn, pDblOut);
3066 /************************************************************************
3067 * VarR8FromUI2 (OLEAUT32.218)
3069 * Convert a VT_UI2 to a VT_R8.
3073 * pDblOut [O] Destination
3077 * Failure: E_INVALIDARG, if the source value is invalid
3078 * DISP_E_OVERFLOW, if the value will not fit in the destination
3079 * DISP_E_TYPEMISMATCH, if the type cannot be converted
3081 HRESULT WINAPI VarR8FromUI2(USHORT usIn, double *pDblOut)
3083 return _VarR8FromUI2(usIn, pDblOut);
3086 /************************************************************************
3087 * VarR8FromUI4 (OLEAUT32.219)
3089 * Convert a VT_UI4 to a VT_R8.
3093 * pDblOut [O] Destination
3097 * Failure: E_INVALIDARG, if the source value is invalid
3098 * DISP_E_OVERFLOW, if the value will not fit in the destination
3099 * DISP_E_TYPEMISMATCH, if the type cannot be converted
3101 HRESULT WINAPI VarR8FromUI4(ULONG ulIn, double *pDblOut)
3103 return _VarR8FromUI4(ulIn, pDblOut);
3106 /************************************************************************
3107 * VarR8FromDec (OLEAUT32.220)
3109 * Convert a VT_DECIMAL to a VT_R8.
3113 * pDblOut [O] Destination
3117 * Failure: E_INVALIDARG, if the source value is invalid.
3119 HRESULT WINAPI VarR8FromDec(DECIMAL* pDecIn, double *pDblOut)
3121 BYTE scale = DEC_SCALE(pDecIn);
3122 double divisor = 1.0, highPart;
3124 if (scale > DEC_MAX_SCALE || DEC_SIGN(pDecIn) & ~DECIMAL_NEG)
3125 return E_INVALIDARG;
3130 if (DEC_SIGN(pDecIn))
3133 if (DEC_HI32(pDecIn))
3135 highPart = (double)DEC_HI32(pDecIn) / divisor;
3141 *pDblOut = (double)DEC_LO64(pDecIn) / divisor + highPart;
3145 /************************************************************************
3146 * VarR8FromI8 (OLEAUT32.362)
3148 * Convert a VT_I8 to a VT_R8.
3152 * pDblOut [O] Destination
3157 HRESULT WINAPI VarR8FromI8(LONG64 llIn, double *pDblOut)
3159 return _VarR8FromI8(llIn, pDblOut);
3162 /************************************************************************
3163 * VarR8FromUI8 (OLEAUT32.363)
3165 * Convert a VT_UI8 to a VT_R8.
3169 * pDblOut [O] Destination
3174 HRESULT WINAPI VarR8FromUI8(ULONG64 ullIn, double *pDblOut)
3176 return _VarR8FromUI8(ullIn, pDblOut);
3179 /************************************************************************
3180 * VarR8Pow (OLEAUT32.315)
3182 * Raise a VT_R8 to a power.
3185 * dblLeft [I] Source
3186 * dblPow [I] Power to raise dblLeft by
3187 * pDblOut [O] Destination
3190 * S_OK. pDblOut contains dblLeft to the power of dblRight.
3192 HRESULT WINAPI VarR8Pow(double dblLeft, double dblPow, double *pDblOut)
3194 *pDblOut = pow(dblLeft, dblPow);
3198 /************************************************************************
3199 * VarR8Round (OLEAUT32.317)
3201 * Round a VT_R8 to a given number of decimal points.
3205 * nDig [I] Number of decimal points to round to
3206 * pDblOut [O] Destination for rounded number
3209 * Success: S_OK. pDblOut is rounded to nDig digits.
3210 * Failure: E_INVALIDARG, if cDecimals is less than 0.
3213 * The native version of this function rounds using the internal
3214 * binary representation of the number. Wine uses the dutch rounding
3215 * convention, so therefore small differences can occur in the value returned.
3216 * MSDN says that you should use your own rounding function if you want
3217 * rounding to be predictable in your application.
3219 HRESULT WINAPI VarR8Round(double dblIn, int nDig, double *pDblOut)
3221 double scale, whole, fract;
3224 return E_INVALIDARG;
3226 scale = pow(10.0, nDig);
3229 whole = dblIn < 0 ? ceil(dblIn) : floor(dblIn);
3230 fract = dblIn - whole;
3233 dblIn = whole + 1.0;
3234 else if (fract == 0.5)
3235 dblIn = whole + fmod(whole, 2.0);
3236 else if (fract >= 0.0)
3238 else if (fract == -0.5)
3239 dblIn = whole - fmod(whole, 2.0);
3240 else if (fract > -0.5)
3243 dblIn = whole - 1.0;
3245 *pDblOut = dblIn / scale;
3252 /* Powers of 10 from 0..4 D.P. */
3253 static const int CY_Divisors[5] = { CY_MULTIPLIER/10000, CY_MULTIPLIER/1000,
3254 CY_MULTIPLIER/100, CY_MULTIPLIER/10, CY_MULTIPLIER };
3256 /************************************************************************
3257 * VarCyFromUI1 (OLEAUT32.98)
3259 * Convert a VT_UI1 to a VT_CY.
3263 * pCyOut [O] Destination
3267 * Failure: E_INVALIDARG, if the source value is invalid
3268 * DISP_E_OVERFLOW, if the value will not fit in the destination
3269 * DISP_E_TYPEMISMATCH, if the type cannot be converted
3271 HRESULT WINAPI VarCyFromUI1(BYTE bIn, CY* pCyOut)
3273 return _VarCyFromUI1(bIn, pCyOut);
3276 /************************************************************************
3277 * VarCyFromI2 (OLEAUT32.99)
3279 * Convert a VT_I2 to a VT_CY.
3283 * pCyOut [O] Destination
3287 * Failure: E_INVALIDARG, if the source value is invalid
3288 * DISP_E_OVERFLOW, if the value will not fit in the destination
3289 * DISP_E_TYPEMISMATCH, if the type cannot be converted
3291 HRESULT WINAPI VarCyFromI2(SHORT sIn, CY* pCyOut)
3293 return _VarCyFromI2(sIn, pCyOut);
3296 /************************************************************************
3297 * VarCyFromI4 (OLEAUT32.100)
3299 * Convert a VT_I4 to a VT_CY.
3303 * pCyOut [O] Destination
3307 * Failure: E_INVALIDARG, if the source value is invalid
3308 * DISP_E_OVERFLOW, if the value will not fit in the destination
3309 * DISP_E_TYPEMISMATCH, if the type cannot be converted
3311 HRESULT WINAPI VarCyFromI4(LONG lIn, CY* pCyOut)
3313 return _VarCyFromI4(lIn, pCyOut);
3316 /************************************************************************
3317 * VarCyFromR4 (OLEAUT32.101)
3319 * Convert a VT_R4 to a VT_CY.
3323 * pCyOut [O] Destination
3327 * Failure: E_INVALIDARG, if the source value is invalid
3328 * DISP_E_OVERFLOW, if the value will not fit in the destination
3329 * DISP_E_TYPEMISMATCH, if the type cannot be converted
3331 HRESULT WINAPI VarCyFromR4(FLOAT fltIn, CY* pCyOut)
3333 return _VarCyFromR4(fltIn, pCyOut);
3336 /************************************************************************
3337 * VarCyFromR8 (OLEAUT32.102)
3339 * Convert a VT_R8 to a VT_CY.
3343 * pCyOut [O] Destination
3347 * Failure: E_INVALIDARG, if the source value is invalid
3348 * DISP_E_OVERFLOW, if the value will not fit in the destination
3349 * DISP_E_TYPEMISMATCH, if the type cannot be converted
3351 HRESULT WINAPI VarCyFromR8(double dblIn, CY* pCyOut)
3353 #if defined(__GNUC__) && defined(__i386__)
3354 /* This code gives identical results to Win32 on Intel.
3355 * Here we use fp exceptions to catch overflows when storing the value.
3357 static const unsigned short r8_fpcontrol = 0x137f;
3358 static const double r8_multiplier = CY_MULTIPLIER_F;
3359 unsigned short old_fpcontrol, result_fpstatus;
3361 /* Clear exceptions, save the old fp state and load the new state */
3362 __asm__ __volatile__( "fnclex" );
3363 __asm__ __volatile__( "fstcw %0" : "=m" (old_fpcontrol) : );
3364 __asm__ __volatile__( "fldcw %0" : : "m" (r8_fpcontrol) );
3365 /* Perform the conversion. */
3366 __asm__ __volatile__( "fldl %0" : : "m" (dblIn) );
3367 __asm__ __volatile__( "fmull %0" : : "m" (r8_multiplier) );
3368 __asm__ __volatile__( "fistpll %0" : : "m" (*pCyOut) );
3369 /* Save the resulting fp state, load the old state and clear exceptions */
3370 __asm__ __volatile__( "fstsw %0" : "=m" (result_fpstatus) : );
3371 __asm__ __volatile__( "fnclex" );
3372 __asm__ __volatile__( "fldcw %0" : : "m" (old_fpcontrol) );
3374 if (result_fpstatus & 0x9) /* Overflow | Invalid */
3375 return DISP_E_OVERFLOW;
3378 /* This version produces slightly different results for boundary cases */
3379 if (dblIn < -922337203685477.5807 || dblIn >= 922337203685477.5807)
3380 return DISP_E_OVERFLOW;
3381 dblIn *= CY_MULTIPLIER_F;
3382 OLEAUT32_DutchRound(LONG64, dblIn, pCyOut->int64);
3387 /************************************************************************
3388 * VarCyFromDate (OLEAUT32.103)
3390 * Convert a VT_DATE to a VT_CY.
3394 * pCyOut [O] Destination
3398 * Failure: E_INVALIDARG, if the source value is invalid
3399 * DISP_E_OVERFLOW, if the value will not fit in the destination
3400 * DISP_E_TYPEMISMATCH, if the type cannot be converted
3402 HRESULT WINAPI VarCyFromDate(DATE dateIn, CY* pCyOut)
3404 return _VarCyFromDate(dateIn, pCyOut);
3407 /************************************************************************
3408 * VarCyFromStr (OLEAUT32.104)
3410 * Convert a VT_BSTR to a VT_CY.
3414 * lcid [I] LCID for the conversion
3415 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
3416 * pCyOut [O] Destination
3420 * Failure: E_INVALIDARG, if the source value is invalid
3421 * DISP_E_OVERFLOW, if the value will not fit in the destination
3422 * DISP_E_TYPEMISMATCH, if the type cannot be converted
3424 HRESULT WINAPI VarCyFromStr(OLECHAR* strIn, LCID lcid, ULONG dwFlags, CY* pCyOut)
3426 return _VarCyFromStr(strIn, lcid, dwFlags, pCyOut);
3429 /************************************************************************
3430 * VarCyFromDisp (OLEAUT32.105)
3432 * Convert a VT_DISPATCH to a VT_CY.
3435 * pdispIn [I] Source
3436 * lcid [I] LCID for conversion
3437 * pCyOut [O] Destination
3441 * Failure: E_INVALIDARG, if the source value is invalid
3442 * DISP_E_OVERFLOW, if the value will not fit in the destination
3443 * DISP_E_TYPEMISMATCH, if the type cannot be converted
3445 HRESULT WINAPI VarCyFromDisp(IDispatch* pdispIn, LCID lcid, CY* pCyOut)
3447 return _VarCyFromDisp(pdispIn, lcid, pCyOut);
3450 /************************************************************************
3451 * VarCyFromBool (OLEAUT32.106)
3453 * Convert a VT_BOOL to a VT_CY.
3457 * pCyOut [O] Destination
3461 * Failure: E_INVALIDARG, if the source value is invalid
3462 * DISP_E_OVERFLOW, if the value will not fit in the destination
3463 * DISP_E_TYPEMISMATCH, if the type cannot be converted
3466 * While the sign of the boolean is stored in the currency, the value is
3467 * converted to either 0 or 1.
3469 HRESULT WINAPI VarCyFromBool(VARIANT_BOOL boolIn, CY* pCyOut)
3471 return _VarCyFromBool(boolIn, pCyOut);
3474 /************************************************************************
3475 * VarCyFromI1 (OLEAUT32.225)
3477 * Convert a VT_I1 to a VT_CY.
3481 * pCyOut [O] Destination
3485 * Failure: E_INVALIDARG, if the source value is invalid
3486 * DISP_E_OVERFLOW, if the value will not fit in the destination
3487 * DISP_E_TYPEMISMATCH, if the type cannot be converted
3489 HRESULT WINAPI VarCyFromI1(signed char cIn, CY* pCyOut)
3491 return _VarCyFromI1(cIn, pCyOut);
3494 /************************************************************************
3495 * VarCyFromUI2 (OLEAUT32.226)
3497 * Convert a VT_UI2 to a VT_CY.
3501 * pCyOut [O] Destination
3505 * Failure: E_INVALIDARG, if the source value is invalid
3506 * DISP_E_OVERFLOW, if the value will not fit in the destination
3507 * DISP_E_TYPEMISMATCH, if the type cannot be converted
3509 HRESULT WINAPI VarCyFromUI2(USHORT usIn, CY* pCyOut)
3511 return _VarCyFromUI2(usIn, pCyOut);
3514 /************************************************************************
3515 * VarCyFromUI4 (OLEAUT32.227)
3517 * Convert a VT_UI4 to a VT_CY.
3521 * pCyOut [O] Destination
3525 * Failure: E_INVALIDARG, if the source value is invalid
3526 * DISP_E_OVERFLOW, if the value will not fit in the destination
3527 * DISP_E_TYPEMISMATCH, if the type cannot be converted
3529 HRESULT WINAPI VarCyFromUI4(ULONG ulIn, CY* pCyOut)
3531 return _VarCyFromUI4(ulIn, pCyOut);
3534 /************************************************************************
3535 * VarCyFromDec (OLEAUT32.228)
3537 * Convert a VT_DECIMAL to a VT_CY.
3541 * pCyOut [O] Destination
3545 * Failure: E_INVALIDARG, if the source value is invalid
3546 * DISP_E_OVERFLOW, if the value will not fit in the destination
3547 * DISP_E_TYPEMISMATCH, if the type cannot be converted
3549 HRESULT WINAPI VarCyFromDec(DECIMAL* pdecIn, CY* pCyOut)
3554 hRet = VarDecRound(pdecIn, 4, &rounded);
3556 if (SUCCEEDED(hRet))
3560 if (DEC_HI32(&rounded))
3561 return DISP_E_OVERFLOW;
3563 /* Note: Without the casts this promotes to int64 which loses precision */
3564 d = (double)DEC_LO64(&rounded) / (double)CY_Divisors[DEC_SCALE(&rounded)];
3565 if (DEC_SIGN(&rounded))
3567 return _VarCyFromR8(d, pCyOut);
3572 /************************************************************************
3573 * VarCyFromI8 (OLEAUT32.366)
3575 * Convert a VT_I8 to a VT_CY.
3579 * pCyOut [O] Destination
3583 * Failure: E_INVALIDARG, if the source value is invalid
3584 * DISP_E_OVERFLOW, if the value will not fit in the destination
3585 * DISP_E_TYPEMISMATCH, if the type cannot be converted
3587 HRESULT WINAPI VarCyFromI8(LONG64 llIn, CY* pCyOut)
3589 return _VarCyFromI8(llIn, pCyOut);
3592 /************************************************************************
3593 * VarCyFromUI8 (OLEAUT32.375)
3595 * Convert a VT_UI8 to a VT_CY.
3599 * pCyOut [O] Destination
3603 * Failure: E_INVALIDARG, if the source value is invalid
3604 * DISP_E_OVERFLOW, if the value will not fit in the destination
3605 * DISP_E_TYPEMISMATCH, if the type cannot be converted
3607 HRESULT WINAPI VarCyFromUI8(ULONG64 ullIn, CY* pCyOut)
3609 return _VarCyFromUI8(ullIn, pCyOut);
3612 /************************************************************************
3613 * VarCyAdd (OLEAUT32.299)
3615 * Add one CY to another.
3619 * cyRight [I] Value to add
3620 * pCyOut [O] Destination
3624 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
3626 HRESULT WINAPI VarCyAdd(const CY cyLeft, const CY cyRight, CY* pCyOut)
3629 _VarR8FromCy(cyLeft, &l);
3630 _VarR8FromCy(cyRight, &r);
3632 return _VarCyFromR8(l, pCyOut);
3635 /************************************************************************
3636 * VarCyMul (OLEAUT32.303)
3638 * Multiply one CY by another.
3642 * cyRight [I] Value to multiply by
3643 * pCyOut [O] Destination
3647 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
3649 HRESULT WINAPI VarCyMul(const CY cyLeft, const CY cyRight, CY* pCyOut)
3652 _VarR8FromCy(cyLeft, &l);
3653 _VarR8FromCy(cyRight, &r);
3655 return _VarCyFromR8(l, pCyOut);
3658 /************************************************************************
3659 * VarCyMulI4 (OLEAUT32.304)
3661 * Multiply one CY by a VT_I4.
3665 * lRight [I] Value to multiply by
3666 * pCyOut [O] Destination
3670 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
3672 HRESULT WINAPI VarCyMulI4(const CY cyLeft, LONG lRight, CY* pCyOut)
3676 _VarR8FromCy(cyLeft, &d);
3678 return _VarCyFromR8(d, pCyOut);
3681 /************************************************************************
3682 * VarCySub (OLEAUT32.305)
3684 * Subtract one CY from another.
3688 * cyRight [I] Value to subtract
3689 * pCyOut [O] Destination
3693 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
3695 HRESULT WINAPI VarCySub(const CY cyLeft, const CY cyRight, CY* pCyOut)
3698 _VarR8FromCy(cyLeft, &l);
3699 _VarR8FromCy(cyRight, &r);
3701 return _VarCyFromR8(l, pCyOut);
3704 /************************************************************************
3705 * VarCyAbs (OLEAUT32.306)
3707 * Convert a VT_CY into its absolute value.
3711 * pCyOut [O] Destination
3714 * Success: S_OK. pCyOut contains the absolute value.
3715 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
3717 HRESULT WINAPI VarCyAbs(const CY cyIn, CY* pCyOut)
3719 if (cyIn.s.Hi == (int)0x80000000 && !cyIn.s.Lo)
3720 return DISP_E_OVERFLOW;
3722 pCyOut->int64 = cyIn.int64 < 0 ? -cyIn.int64 : cyIn.int64;
3726 /************************************************************************
3727 * VarCyFix (OLEAUT32.307)
3729 * Return the integer part of a VT_CY.
3733 * pCyOut [O] Destination
3737 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
3739 HRESULT WINAPI VarCyFix(const CY cyIn, CY* pCyOut)
3741 pCyOut->int64 = cyIn.int64 / CY_MULTIPLIER;
3742 pCyOut->int64 *= CY_MULTIPLIER;
3746 /************************************************************************
3747 * VarCyInt (OLEAUT32.308)
3749 * Return the integer part of a VT_CY.
3753 * pCyOut [O] Destination
3757 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
3759 HRESULT WINAPI VarCyInt(const CY cyIn, CY* pCyOut)
3763 _VarR8FromCy(cyIn, &d);
3764 d = floor(d) * CY_MULTIPLIER_F;
3765 OLEAUT32_DutchRound(LONGLONG, d, pCyOut->int64);
3769 /************************************************************************
3770 * VarCyNeg (OLEAUT32.309)
3772 * Change the sign of a VT_CY.
3776 * pCyOut [O] Destination
3780 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
3782 HRESULT WINAPI VarCyNeg(const CY cyIn, CY* pCyOut)
3784 if (cyIn.s.Hi == (int)0x80000000 && !cyIn.s.Lo)
3785 return DISP_E_OVERFLOW;
3787 pCyOut->int64 = -cyIn.int64;
3791 /************************************************************************
3792 * VarCyRound (OLEAUT32.310)
3794 * Change the precision of a VT_CY.
3798 * cDecimals [I] New number of decimals to keep
3799 * pCyOut [O] Destination
3803 * Failure: E_INVALIDARG, if cDecimals is less than 0.
3805 HRESULT WINAPI VarCyRound(const CY cyIn, int cDecimals, CY* pCyOut)
3808 return E_INVALIDARG;
3812 /* Rounding to more precision than we have */
3818 double d, div = CY_Divisors[cDecimals];
3820 _VarR8FromCy(cyIn, &d);
3822 OLEAUT32_DutchRound(LONGLONG, d, pCyOut->int64)
3823 d = (double)pCyOut->int64 / div * CY_MULTIPLIER_F;
3824 OLEAUT32_DutchRound(LONGLONG, d, pCyOut->int64)
3829 /************************************************************************
3830 * VarCyCmp (OLEAUT32.311)
3832 * Compare two VT_CY values.
3836 * cyRight [I] Value to compare
3839 * Success: VARCMP_LT, VARCMP_EQ or VARCMP_GT indicating that the value to
3840 * compare is less, equal or greater than source respectively.
3841 * Failure: DISP_E_OVERFLOW, if overflow occurs during the comparason
3843 HRESULT WINAPI VarCyCmp(const CY cyLeft, const CY cyRight)
3848 /* Subtract right from left, and compare the result to 0 */
3849 hRet = VarCySub(cyLeft, cyRight, &result);
3851 if (SUCCEEDED(hRet))
3853 if (result.int64 < 0)
3854 hRet = (HRESULT)VARCMP_LT;
3855 else if (result.int64 > 0)
3856 hRet = (HRESULT)VARCMP_GT;
3858 hRet = (HRESULT)VARCMP_EQ;
3863 /************************************************************************
3864 * VarCyCmpR8 (OLEAUT32.312)
3866 * Compare a VT_CY to a double
3869 * cyLeft [I] Currency Source
3870 * dblRight [I] double to compare to cyLeft
3873 * Success: VARCMP_LT, VARCMP_EQ or VARCMP_GT indicating that dblRight is
3874 * less than, equal to or greater than cyLeft respectively.
3875 * Failure: DISP_E_OVERFLOW, if overflow occurs during the comparason
3877 HRESULT WINAPI VarCyCmpR8(const CY cyLeft, double dblRight)
3882 hRet = _VarCyFromR8(dblRight, &cyRight);
3884 if (SUCCEEDED(hRet))
3885 hRet = VarCyCmp(cyLeft, cyRight);
3890 /************************************************************************
3891 * VarCyMulI8 (OLEAUT32.329)
3893 * Multiply a VT_CY by a VT_I8.
3897 * llRight [I] Value to multiply by
3898 * pCyOut [O] Destination
3902 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
3904 HRESULT WINAPI VarCyMulI8(const CY cyLeft, LONG64 llRight, CY* pCyOut)
3908 _VarR8FromCy(cyLeft, &d);
3909 d = d * (double)llRight;
3910 return _VarCyFromR8(d, pCyOut);
3916 /************************************************************************
3917 * VarDecFromUI1 (OLEAUT32.190)
3919 * Convert a VT_UI1 to a DECIMAL.
3923 * pDecOut [O] Destination
3928 HRESULT WINAPI VarDecFromUI1(BYTE bIn, DECIMAL* pDecOut)
3930 return _VarDecFromUI1(bIn, pDecOut);
3933 /************************************************************************
3934 * VarDecFromI2 (OLEAUT32.191)
3936 * Convert a VT_I2 to a DECIMAL.
3940 * pDecOut [O] Destination
3945 HRESULT WINAPI VarDecFromI2(SHORT sIn, DECIMAL* pDecOut)
3947 return _VarDecFromI2(sIn, pDecOut);
3950 /************************************************************************
3951 * VarDecFromI4 (OLEAUT32.192)
3953 * Convert a VT_I4 to a DECIMAL.
3957 * pDecOut [O] Destination
3962 HRESULT WINAPI VarDecFromI4(LONG lIn, DECIMAL* pDecOut)
3964 DEC_HI32(pDecOut) = 0;
3965 DEC_MID32(pDecOut) = 0;
3969 DEC_SIGNSCALE(pDecOut) = SIGNSCALE(DECIMAL_NEG,0);
3970 DEC_LO32(pDecOut) = -lIn;
3974 DEC_SIGNSCALE(pDecOut) = SIGNSCALE(DECIMAL_POS,0);
3975 DEC_LO32(pDecOut) = lIn;
3980 /************************************************************************
3981 * VarDecFromR4 (OLEAUT32.193)
3983 * Convert a VT_R4 to a DECIMAL.
3987 * pDecOut [O] Destination
3992 HRESULT WINAPI VarDecFromR4(FLOAT fltIn, DECIMAL* pDecOut)
3996 sprintfW( buff, szFloatFormatW, fltIn );
3997 return _VarDecFromStr(buff, LOCALE_SYSTEM_DEFAULT, 0, pDecOut);
4000 /************************************************************************
4001 * VarDecFromR8 (OLEAUT32.194)
4003 * Convert a VT_R8 to a DECIMAL.
4007 * pDecOut [O] Destination
4012 HRESULT WINAPI VarDecFromR8(double dblIn, DECIMAL* pDecOut)
4016 sprintfW( buff, szDoubleFormatW, dblIn );
4017 return _VarDecFromStr(buff, LOCALE_USER_DEFAULT, 0, pDecOut);
4020 /************************************************************************
4021 * VarDecFromDate (OLEAUT32.195)
4023 * Convert a VT_DATE to a DECIMAL.
4027 * pDecOut [O] Destination
4032 HRESULT WINAPI VarDecFromDate(DATE dateIn, DECIMAL* pDecOut)
4034 return _VarDecFromDate(dateIn, pDecOut);
4037 /************************************************************************
4038 * VarDecFromCy (OLEAUT32.196)
4040 * Convert a VT_CY to a DECIMAL.
4044 * pDecOut [O] Destination
4049 HRESULT WINAPI VarDecFromCy(CY cyIn, DECIMAL* pDecOut)
4051 DEC_HI32(pDecOut) = 0;
4053 /* Note: This assumes 2s complement integer representation */
4054 if (cyIn.s.Hi & 0x80000000)
4056 DEC_SIGNSCALE(pDecOut) = SIGNSCALE(DECIMAL_NEG,4);
4057 DEC_LO64(pDecOut) = -cyIn.int64;
4061 DEC_SIGNSCALE(pDecOut) = SIGNSCALE(DECIMAL_POS,4);
4062 DEC_MID32(pDecOut) = cyIn.s.Hi;
4063 DEC_LO32(pDecOut) = cyIn.s.Lo;
4068 /************************************************************************
4069 * VarDecFromStr (OLEAUT32.197)
4071 * Convert a VT_BSTR to a DECIMAL.
4075 * lcid [I] LCID for the conversion
4076 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
4077 * pDecOut [O] Destination
4081 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
4083 HRESULT WINAPI VarDecFromStr(OLECHAR* strIn, LCID lcid, ULONG dwFlags, DECIMAL* pDecOut)
4085 return _VarDecFromStr(strIn, lcid, dwFlags, pDecOut);
4088 /************************************************************************
4089 * VarDecFromDisp (OLEAUT32.198)
4091 * Convert a VT_DISPATCH to a DECIMAL.
4094 * pdispIn [I] Source
4095 * lcid [I] LCID for conversion
4096 * pDecOut [O] Destination
4100 * Failure: DISP_E_TYPEMISMATCH, if the type cannot be converted
4102 HRESULT WINAPI VarDecFromDisp(IDispatch* pdispIn, LCID lcid, DECIMAL* pDecOut)
4104 return _VarDecFromDisp(pdispIn, lcid, pDecOut);
4107 /************************************************************************
4108 * VarDecFromBool (OLEAUT32.199)
4110 * Convert a VT_BOOL to a DECIMAL.
4114 * pDecOut [O] Destination
4120 * The value is converted to either 0 (if bIn is FALSE) or -1 (TRUE).
4122 HRESULT WINAPI VarDecFromBool(VARIANT_BOOL bIn, DECIMAL* pDecOut)
4124 DEC_HI32(pDecOut) = 0;
4125 DEC_MID32(pDecOut) = 0;
4128 DEC_SIGNSCALE(pDecOut) = SIGNSCALE(DECIMAL_NEG,0);
4129 DEC_LO32(pDecOut) = 1;
4133 DEC_SIGNSCALE(pDecOut) = SIGNSCALE(DECIMAL_POS,0);
4134 DEC_LO32(pDecOut) = 0;
4139 /************************************************************************
4140 * VarDecFromI1 (OLEAUT32.241)
4142 * Convert a VT_I1 to a DECIMAL.
4146 * pDecOut [O] Destination
4151 HRESULT WINAPI VarDecFromI1(signed char cIn, DECIMAL* pDecOut)
4153 return _VarDecFromI1(cIn, pDecOut);
4156 /************************************************************************
4157 * VarDecFromUI2 (OLEAUT32.242)
4159 * Convert a VT_UI2 to a DECIMAL.
4163 * pDecOut [O] Destination
4168 HRESULT WINAPI VarDecFromUI2(USHORT usIn, DECIMAL* pDecOut)
4170 return _VarDecFromUI2(usIn, pDecOut);
4173 /************************************************************************
4174 * VarDecFromUI4 (OLEAUT32.243)
4176 * Convert a VT_UI4 to a DECIMAL.
4180 * pDecOut [O] Destination
4185 HRESULT WINAPI VarDecFromUI4(ULONG ulIn, DECIMAL* pDecOut)
4187 DEC_SIGNSCALE(pDecOut) = SIGNSCALE(DECIMAL_POS,0);
4188 DEC_HI32(pDecOut) = 0;
4189 DEC_MID32(pDecOut) = 0;
4190 DEC_LO32(pDecOut) = ulIn;
4194 /************************************************************************
4195 * VarDecFromI8 (OLEAUT32.374)
4197 * Convert a VT_I8 to a DECIMAL.
4201 * pDecOut [O] Destination
4206 HRESULT WINAPI VarDecFromI8(LONG64 llIn, DECIMAL* pDecOut)
4208 PULARGE_INTEGER pLi = (PULARGE_INTEGER)&llIn;
4210 DEC_HI32(pDecOut) = 0;
4212 /* Note: This assumes 2s complement integer representation */
4213 if (pLi->s.HighPart & 0x80000000)
4215 DEC_SIGNSCALE(pDecOut) = SIGNSCALE(DECIMAL_NEG,0);
4216 DEC_LO64(pDecOut) = -pLi->QuadPart;
4220 DEC_SIGNSCALE(pDecOut) = SIGNSCALE(DECIMAL_POS,0);
4221 DEC_MID32(pDecOut) = pLi->s.HighPart;
4222 DEC_LO32(pDecOut) = pLi->s.LowPart;
4227 /************************************************************************
4228 * VarDecFromUI8 (OLEAUT32.375)
4230 * Convert a VT_UI8 to a DECIMAL.
4234 * pDecOut [O] Destination
4239 HRESULT WINAPI VarDecFromUI8(ULONG64 ullIn, DECIMAL* pDecOut)
4241 DEC_SIGNSCALE(pDecOut) = SIGNSCALE(DECIMAL_POS,0);
4242 DEC_HI32(pDecOut) = 0;
4243 DEC_LO64(pDecOut) = ullIn;
4247 /* Make two DECIMALS the same scale; used by math functions below */
4248 static HRESULT VARIANT_DecScale(const DECIMAL** ppDecLeft,
4249 const DECIMAL** ppDecRight,
4252 static DECIMAL scaleFactor;
4255 HRESULT hRet = S_OK;
4257 if (DEC_SIGN(*ppDecLeft) & ~DECIMAL_NEG || DEC_SIGN(*ppDecRight) & ~DECIMAL_NEG)
4258 return E_INVALIDARG;
4260 DEC_LO32(&scaleFactor) = 10;
4262 i = scaleAmount = DEC_SCALE(*ppDecLeft) - DEC_SCALE(*ppDecRight);
4265 return S_OK; /* Same scale */
4267 if (scaleAmount > 0)
4269 decTemp = *(*ppDecRight); /* Left is bigger - scale the right hand side */
4270 *ppDecRight = pDecOut;
4274 decTemp = *(*ppDecLeft); /* Right is bigger - scale the left hand side */
4275 *ppDecLeft = pDecOut;
4276 i = scaleAmount = -scaleAmount;
4279 if (DEC_SCALE(&decTemp) + scaleAmount > DEC_MAX_SCALE)
4280 return DISP_E_OVERFLOW; /* Can't scale up */
4282 /* Multiply up the value to be scaled by the correct amount */
4283 while (SUCCEEDED(hRet) && i--)
4285 /* Note we are multiplying by a value with a scale of 0, so we dont recurse */
4286 hRet = VarDecMul(&decTemp, &scaleFactor, pDecOut);
4289 DEC_SCALE(pDecOut) += scaleAmount; /* Set the new scale */
4293 /* Add two unsigned 32 bit values with overflow */
4294 static ULONG VARIANT_Add(ULONG ulLeft, ULONG ulRight, ULONG* pulHigh)
4296 ULARGE_INTEGER ul64;
4298 ul64.QuadPart = (ULONG64)ulLeft + (ULONG64)ulRight + (ULONG64)*pulHigh;
4299 *pulHigh = ul64.s.HighPart;
4300 return ul64.s.LowPart;
4303 /* Subtract two unsigned 32 bit values with underflow */
4304 static ULONG VARIANT_Sub(ULONG ulLeft, ULONG ulRight, ULONG* pulHigh)
4307 ULARGE_INTEGER ul64;
4309 ul64.QuadPart = (LONG64)ulLeft - (ULONG64)ulRight;
4310 if (ulLeft < ulRight)
4313 if (ul64.QuadPart > (ULONG64)*pulHigh)
4314 ul64.QuadPart -= (ULONG64)*pulHigh;
4317 ul64.QuadPart -= (ULONG64)*pulHigh;
4321 ul64.s.HighPart = -ul64.s.HighPart ;
4323 *pulHigh = ul64.s.HighPart;
4324 return ul64.s.LowPart;
4327 /* Multiply two unsigned 32 bit values with overflow */
4328 static ULONG VARIANT_Mul(ULONG ulLeft, ULONG ulRight, ULONG* pulHigh)
4330 ULARGE_INTEGER ul64;
4332 ul64.QuadPart = (ULONG64)ulLeft * (ULONG64)ulRight + (ULONG64)*pulHigh;
4333 *pulHigh = ul64.s.HighPart;
4334 return ul64.s.LowPart;
4337 /* Compare two decimals that have the same scale */
4338 static inline int VARIANT_DecCmp(const DECIMAL *pDecLeft, const DECIMAL *pDecRight)
4340 if ( DEC_HI32(pDecLeft) < DEC_HI32(pDecRight) ||
4341 (DEC_HI32(pDecLeft) <= DEC_HI32(pDecRight) && DEC_LO64(pDecLeft) < DEC_LO64(pDecRight)))
4343 else if (DEC_HI32(pDecLeft) == DEC_HI32(pDecRight) && DEC_LO64(pDecLeft) == DEC_LO64(pDecRight))
4348 /************************************************************************
4349 * VarDecAdd (OLEAUT32.177)
4351 * Add one DECIMAL to another.
4354 * pDecLeft [I] Source
4355 * pDecRight [I] Value to add
4356 * pDecOut [O] Destination
4360 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
4362 HRESULT WINAPI VarDecAdd(const DECIMAL* pDecLeft, const DECIMAL* pDecRight, DECIMAL* pDecOut)
4367 hRet = VARIANT_DecScale(&pDecLeft, &pDecRight, &scaled);
4369 if (SUCCEEDED(hRet))
4371 /* Our decimals now have the same scale, we can add them as 96 bit integers */
4373 BYTE sign = DECIMAL_POS;
4375 /* Correct for the sign of the result */
4376 if (DEC_SIGN(pDecLeft) && DEC_SIGN(pDecRight))
4378 /* -x + -y : Negative */
4380 goto VarDecAdd_AsPositive;
4382 else if (DEC_SIGN(pDecLeft) && !DEC_SIGN(pDecRight))
4384 int cmp = VARIANT_DecCmp(pDecLeft, pDecRight);
4386 /* -x + y : Negative if x > y */
4390 VarDecAdd_AsNegative:
4391 DEC_LO32(pDecOut) = VARIANT_Sub(DEC_LO32(pDecLeft), DEC_LO32(pDecRight), &overflow);
4392 DEC_MID32(pDecOut) = VARIANT_Sub(DEC_MID32(pDecLeft), DEC_MID32(pDecRight), &overflow);
4393 DEC_HI32(pDecOut) = VARIANT_Sub(DEC_HI32(pDecLeft), DEC_HI32(pDecRight), &overflow);
4397 VarDecAdd_AsInvertedNegative:
4398 DEC_LO32(pDecOut) = VARIANT_Sub(DEC_LO32(pDecRight), DEC_LO32(pDecLeft), &overflow);
4399 DEC_MID32(pDecOut) = VARIANT_Sub(DEC_MID32(pDecRight), DEC_MID32(pDecLeft), &overflow);
4400 DEC_HI32(pDecOut) = VARIANT_Sub(DEC_HI32(pDecRight), DEC_HI32(pDecLeft), &overflow);
4403 else if (!DEC_SIGN(pDecLeft) && DEC_SIGN(pDecRight))
4405 int cmp = VARIANT_DecCmp(pDecLeft, pDecRight);
4407 /* x + -y : Negative if x <= y */
4411 goto VarDecAdd_AsInvertedNegative;
4413 goto VarDecAdd_AsNegative;
4417 /* x + y : Positive */
4418 VarDecAdd_AsPositive:
4419 DEC_LO32(pDecOut) = VARIANT_Add(DEC_LO32(pDecLeft), DEC_LO32(pDecRight), &overflow);
4420 DEC_MID32(pDecOut) = VARIANT_Add(DEC_MID32(pDecLeft), DEC_MID32(pDecRight), &overflow);
4421 DEC_HI32(pDecOut) = VARIANT_Add(DEC_HI32(pDecLeft), DEC_HI32(pDecRight), &overflow);
4425 return DISP_E_OVERFLOW; /* overflowed */
4427 DEC_SCALE(pDecOut) = DEC_SCALE(pDecLeft);
4428 DEC_SIGN(pDecOut) = sign;
4433 /************************************************************************
4434 * VarDecDiv (OLEAUT32.178)
4436 * Divide one DECIMAL by another.
4439 * pDecLeft [I] Source
4440 * pDecRight [I] Value to divide by
4441 * pDecOut [O] Destination
4445 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
4447 HRESULT WINAPI VarDecDiv(const DECIMAL* pDecLeft, const DECIMAL* pDecRight, DECIMAL* pDecOut)
4449 FIXME("(%p,%p,%p)-stub!\n",pDecLeft,pDecRight,pDecOut);
4450 return DISP_E_OVERFLOW;
4453 /************************************************************************
4454 * VarDecMul (OLEAUT32.179)
4456 * Multiply one DECIMAL by another.
4459 * pDecLeft [I] Source
4460 * pDecRight [I] Value to multiply by
4461 * pDecOut [O] Destination
4465 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
4467 HRESULT WINAPI VarDecMul(const DECIMAL* pDecLeft, const DECIMAL* pDecRight, DECIMAL* pDecOut)
4469 /* FIXME: This only allows multiplying by a fixed integer <= 0xffffffff */
4471 if (!DEC_SCALE(pDecLeft) || !DEC_SCALE(pDecRight))
4473 /* At least one term is an integer */
4474 const DECIMAL* pDecInteger = DEC_SCALE(pDecLeft) ? pDecRight : pDecLeft;
4475 const DECIMAL* pDecOperand = DEC_SCALE(pDecLeft) ? pDecLeft : pDecRight;
4476 HRESULT hRet = S_OK;
4477 unsigned int multiplier = DEC_LO32(pDecInteger);
4480 if (DEC_HI32(pDecInteger) || DEC_MID32(pDecInteger))
4482 FIXME("(%p,%p,%p) semi-stub!\n",pDecLeft,pDecRight,pDecOut);
4483 return DISP_E_OVERFLOW;
4486 DEC_LO32(pDecOut) = VARIANT_Mul(DEC_LO32(pDecOperand), multiplier, &overflow);
4487 DEC_MID32(pDecOut) = VARIANT_Mul(DEC_MID32(pDecOperand), multiplier, &overflow);
4488 DEC_HI32(pDecOut) = VARIANT_Mul(DEC_HI32(pDecOperand), multiplier, &overflow);
4491 hRet = DISP_E_OVERFLOW;
4494 BYTE sign = DECIMAL_POS;
4496 if (DEC_SIGN(pDecLeft) != DEC_SIGN(pDecRight))
4497 sign = DECIMAL_NEG; /* pos * neg => negative */
4498 DEC_SIGN(pDecOut) = sign;
4499 DEC_SCALE(pDecOut) = DEC_SCALE(pDecOperand);
4503 FIXME("(%p,%p,%p) semi-stub!\n",pDecLeft,pDecRight,pDecOut);
4504 return DISP_E_OVERFLOW;
4507 /************************************************************************
4508 * VarDecSub (OLEAUT32.181)
4510 * Subtract one DECIMAL from another.
4513 * pDecLeft [I] Source
4514 * pDecRight [I] DECIMAL to subtract from pDecLeft
4515 * pDecOut [O] Destination
4518 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
4520 HRESULT WINAPI VarDecSub(const DECIMAL* pDecLeft, const DECIMAL* pDecRight, DECIMAL* pDecOut)
4524 /* Implement as addition of the negative */
4525 VarDecNeg(pDecRight, &decRight);
4526 return VarDecAdd(pDecLeft, &decRight, pDecOut);
4529 /************************************************************************
4530 * VarDecAbs (OLEAUT32.182)
4532 * Convert a DECIMAL into its absolute value.
4536 * pDecOut [O] Destination
4539 * S_OK. This function does not fail.
4541 HRESULT WINAPI VarDecAbs(const DECIMAL* pDecIn, DECIMAL* pDecOut)
4544 DEC_SIGN(pDecOut) &= ~DECIMAL_NEG;
4548 /************************************************************************
4549 * VarDecFix (OLEAUT32.187)
4551 * Return the integer portion of a DECIMAL.
4555 * pDecOut [O] Destination
4559 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
4561 HRESULT WINAPI VarDecFix(const DECIMAL* pDecIn, DECIMAL* pDecOut)
4563 if (DEC_SIGN(pDecOut) & ~DECIMAL_NEG)
4564 return E_INVALIDARG;
4566 if (!DEC_SCALE(pDecIn))
4568 *pDecOut = *pDecIn; /* Already an integer */
4572 FIXME("semi-stub!\n");
4573 return DISP_E_OVERFLOW;
4576 /************************************************************************
4577 * VarDecInt (OLEAUT32.188)
4579 * Return the integer portion of a DECIMAL.
4583 * pDecOut [O] Destination
4587 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
4589 HRESULT WINAPI VarDecInt(const DECIMAL* pDecIn, DECIMAL* pDecOut)
4591 if (DEC_SIGN(pDecOut) & ~DECIMAL_NEG)
4592 return E_INVALIDARG;
4594 if (!DEC_SCALE(pDecIn))
4596 *pDecOut = *pDecIn; /* Already an integer */
4600 FIXME("semi-stub!\n");
4601 return DISP_E_OVERFLOW;
4604 /************************************************************************
4605 * VarDecNeg (OLEAUT32.189)
4607 * Change the sign of a DECIMAL.
4611 * pDecOut [O] Destination
4614 * S_OK. This function does not fail.
4616 HRESULT WINAPI VarDecNeg(const DECIMAL* pDecIn, DECIMAL* pDecOut)
4619 DEC_SIGN(pDecOut) ^= DECIMAL_NEG;
4623 /************************************************************************
4624 * VarDecRound (OLEAUT32.203)
4626 * Change the precision of a DECIMAL.
4630 * cDecimals [I] New number of decimals to keep
4631 * pDecOut [O] Destination
4634 * Success: S_OK. pDecOut contains the rounded value.
4635 * Failure: E_INVALIDARG if any argument is invalid.
4637 HRESULT WINAPI VarDecRound(const DECIMAL* pDecIn, int cDecimals, DECIMAL* pDecOut)
4639 if (cDecimals < 0 || (DEC_SIGN(pDecIn) & ~DECIMAL_NEG) || DEC_SCALE(pDecIn) > DEC_MAX_SCALE)
4640 return E_INVALIDARG;
4642 if (cDecimals >= DEC_SCALE(pDecIn))
4644 *pDecOut = *pDecIn; /* More precision than we have */
4648 FIXME("semi-stub!\n");
4650 return DISP_E_OVERFLOW;
4653 /************************************************************************
4654 * VarDecCmp (OLEAUT32.204)
4656 * Compare two DECIMAL values.
4659 * pDecLeft [I] Source
4660 * pDecRight [I] Value to compare
4663 * Success: VARCMP_LT, VARCMP_EQ or VARCMP_GT indicating that pDecLeft
4664 * is less than, equal to or greater than pDecRight respectively.
4665 * Failure: DISP_E_OVERFLOW, if overflow occurs during the comparason
4667 HRESULT WINAPI VarDecCmp(const DECIMAL* pDecLeft, const DECIMAL* pDecRight)
4672 /* Subtract right from left, and compare the result to 0 */
4673 hRet = VarDecSub(pDecLeft, pDecRight, &result);
4675 if (SUCCEEDED(hRet))
4677 int non_zero = DEC_HI32(&result) | DEC_MID32(&result) | DEC_LO32(&result);
4679 if ((DEC_SIGN(&result) & DECIMAL_NEG) && non_zero)
4680 hRet = (HRESULT)VARCMP_LT;
4682 hRet = (HRESULT)VARCMP_GT;
4684 hRet = (HRESULT)VARCMP_EQ;
4689 /************************************************************************
4690 * VarDecCmpR8 (OLEAUT32.298)
4692 * Compare a DECIMAL to a double
4695 * pDecLeft [I] DECIMAL Source
4696 * dblRight [I] double to compare to pDecLeft
4699 * Success: VARCMP_LT, VARCMP_EQ or VARCMP_GT indicating that dblRight
4700 * is less than, equal to or greater than pDecLeft respectively.
4701 * Failure: DISP_E_OVERFLOW, if overflow occurs during the comparason
4703 HRESULT WINAPI VarDecCmpR8(const DECIMAL* pDecLeft, double dblRight)
4708 hRet = VarDecFromR8(dblRight, &decRight);
4710 if (SUCCEEDED(hRet))
4711 hRet = VarDecCmp(pDecLeft, &decRight);
4719 /************************************************************************
4720 * VarBoolFromUI1 (OLEAUT32.118)
4722 * Convert a VT_UI1 to a VT_BOOL.
4726 * pBoolOut [O] Destination
4731 HRESULT WINAPI VarBoolFromUI1(BYTE bIn, VARIANT_BOOL *pBoolOut)
4733 return _VarBoolFromUI1(bIn, pBoolOut);
4736 /************************************************************************
4737 * VarBoolFromI2 (OLEAUT32.119)
4739 * Convert a VT_I2 to a VT_BOOL.
4743 * pBoolOut [O] Destination
4748 HRESULT WINAPI VarBoolFromI2(SHORT sIn, VARIANT_BOOL *pBoolOut)
4750 return _VarBoolFromI2(sIn, pBoolOut);
4753 /************************************************************************
4754 * VarBoolFromI4 (OLEAUT32.120)
4756 * Convert a VT_I4 to a VT_BOOL.
4760 * pBoolOut [O] Destination
4765 HRESULT WINAPI VarBoolFromI4(LONG lIn, VARIANT_BOOL *pBoolOut)
4767 return _VarBoolFromI4(lIn, pBoolOut);
4770 /************************************************************************
4771 * VarBoolFromR4 (OLEAUT32.121)
4773 * Convert a VT_R4 to a VT_BOOL.
4777 * pBoolOut [O] Destination
4782 HRESULT WINAPI VarBoolFromR4(FLOAT fltIn, VARIANT_BOOL *pBoolOut)
4784 return _VarBoolFromR4(fltIn, pBoolOut);
4787 /************************************************************************
4788 * VarBoolFromR8 (OLEAUT32.122)
4790 * Convert a VT_R8 to a VT_BOOL.
4794 * pBoolOut [O] Destination
4799 HRESULT WINAPI VarBoolFromR8(double dblIn, VARIANT_BOOL *pBoolOut)
4801 return _VarBoolFromR8(dblIn, pBoolOut);
4804 /************************************************************************
4805 * VarBoolFromDate (OLEAUT32.123)
4807 * Convert a VT_DATE to a VT_BOOL.
4811 * pBoolOut [O] Destination
4816 HRESULT WINAPI VarBoolFromDate(DATE dateIn, VARIANT_BOOL *pBoolOut)
4818 return _VarBoolFromDate(dateIn, pBoolOut);
4821 /************************************************************************
4822 * VarBoolFromCy (OLEAUT32.124)
4824 * Convert a VT_CY to a VT_BOOL.
4828 * pBoolOut [O] Destination
4833 HRESULT WINAPI VarBoolFromCy(CY cyIn, VARIANT_BOOL *pBoolOut)
4835 return _VarBoolFromCy(cyIn, pBoolOut);
4838 static BOOL VARIANT_GetLocalisedText(LANGID langId, DWORD dwId, WCHAR *lpszDest)
4842 hrsrc = FindResourceExW( OLEAUT32_hModule, (LPWSTR)RT_STRING,
4843 (LPCWSTR)((dwId >> 4) + 1), langId );
4846 HGLOBAL hmem = LoadResource( OLEAUT32_hModule, hrsrc );
4853 p = LockResource( hmem );
4854 for (i = 0; i < (dwId & 0x0f); i++) p += *p + 1;
4856 memcpy( lpszDest, p + 1, *p * sizeof(WCHAR) );
4857 lpszDest[*p] = '\0';
4858 TRACE("got %s for LANGID %08x\n", debugstr_w(lpszDest), langId);
4865 /************************************************************************
4866 * VarBoolFromStr (OLEAUT32.125)
4868 * Convert a VT_BSTR to a VT_BOOL.
4872 * lcid [I] LCID for the conversion
4873 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
4874 * pBoolOut [O] Destination
4878 * Failure: E_INVALIDARG, if pBoolOut is invalid.
4879 * DISP_E_TYPEMISMATCH, if the type cannot be converted
4882 * - strIn will be recognised if it contains "#TRUE#" or "#FALSE#". Additionally,
4883 * it may contain (in any case mapping) the text "true" or "false".
4884 * - If dwFlags includes VAR_LOCALBOOL, then the text may also match the
4885 * localised text of "True" or "False" in the language specified by lcid.
4886 * - If none of these matches occur, the string is treated as a numeric string
4887 * and the boolean pBoolOut will be set according to whether the number is zero
4888 * or not. The dwFlags parameter is passed to VarR8FromStr() for this conversion.
4889 * - If the text is not numeric and does not match any of the above, then
4890 * DISP_E_TYPEMISMATCH is returned.
4892 HRESULT WINAPI VarBoolFromStr(OLECHAR* strIn, LCID lcid, ULONG dwFlags, VARIANT_BOOL *pBoolOut)
4894 /* Any VB/VBA programmers out there should recognise these strings... */
4895 static const WCHAR szFalse[] = { '#','F','A','L','S','E','#','\0' };
4896 static const WCHAR szTrue[] = { '#','T','R','U','E','#','\0' };
4898 LANGID langId = MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT);
4899 HRESULT hRes = S_OK;
4901 if (!strIn || !pBoolOut)
4902 return DISP_E_TYPEMISMATCH;
4904 /* Check if we should be comparing against localised text */
4905 if (dwFlags & VAR_LOCALBOOL)
4907 /* Convert our LCID into a usable value */
4908 lcid = ConvertDefaultLocale(lcid);
4910 langId = LANGIDFROMLCID(lcid);
4912 if (PRIMARYLANGID(langId) == LANG_NEUTRAL)
4913 langId = MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT);
4915 /* Note: Native oleaut32 always copies strIn and maps halfwidth characters.
4916 * I don't think this is needed unless any of the localised text strings
4917 * contain characters that can be so mapped. In the event that this is
4918 * true for a given language (possibly some Asian languages), then strIn
4919 * should be mapped here _only_ if langId is an Id for which this can occur.
4923 /* Note that if we are not comparing against localised strings, langId
4924 * will have its default value of LANG_ENGLISH. This allows us to mimic
4925 * the native behaviour of always checking against English strings even
4926 * after we've checked for localised ones.
4928 VarBoolFromStr_CheckLocalised:
4929 if (VARIANT_GetLocalisedText(langId, IDS_TRUE, szBuff))
4931 /* Compare against localised strings, ignoring case */
4932 if (!strcmpiW(strIn, szBuff))
4934 *pBoolOut = VARIANT_TRUE; /* Matched localised 'true' text */
4937 VARIANT_GetLocalisedText(langId, IDS_FALSE, szBuff);
4938 if (!strcmpiW(strIn, szBuff))
4940 *pBoolOut = VARIANT_FALSE; /* Matched localised 'false' text */
4945 if (langId != MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT))
4947 /* We have checked the localised text, now check English */
4948 langId = MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT);
4949 goto VarBoolFromStr_CheckLocalised;
4952 /* All checks against localised text have failed, try #TRUE#/#FALSE# */
4953 if (!strcmpW(strIn, szFalse))
4954 *pBoolOut = VARIANT_FALSE;
4955 else if (!strcmpW(strIn, szTrue))
4956 *pBoolOut = VARIANT_TRUE;
4961 /* If this string is a number, convert it as one */
4962 hRes = _VarR8FromStr(strIn, lcid, dwFlags, &d);
4963 if (SUCCEEDED(hRes))
4964 hRes = _VarBoolFromR8(d, pBoolOut);
4969 /************************************************************************
4970 * VarBoolFromDisp (OLEAUT32.126)
4972 * Convert a VT_DISPATCH to a VT_BOOL.
4975 * pdispIn [I] Source
4976 * lcid [I] LCID for conversion
4977 * pBoolOut [O] Destination
4981 * Failure: E_INVALIDARG, if the source value is invalid
4982 * DISP_E_OVERFLOW, if the value will not fit in the destination
4983 * DISP_E_TYPEMISMATCH, if the type cannot be converted
4985 HRESULT WINAPI VarBoolFromDisp(IDispatch* pdispIn, LCID lcid, VARIANT_BOOL *pBoolOut)
4987 return _VarBoolFromDisp(pdispIn, lcid, pBoolOut);
4990 /************************************************************************
4991 * VarBoolFromI1 (OLEAUT32.233)
4993 * Convert a VT_I1 to a VT_BOOL.
4997 * pBoolOut [O] Destination
5002 HRESULT WINAPI VarBoolFromI1(signed char cIn, VARIANT_BOOL *pBoolOut)
5004 return _VarBoolFromI1(cIn, pBoolOut);
5007 /************************************************************************
5008 * VarBoolFromUI2 (OLEAUT32.234)
5010 * Convert a VT_UI2 to a VT_BOOL.
5014 * pBoolOut [O] Destination
5019 HRESULT WINAPI VarBoolFromUI2(USHORT usIn, VARIANT_BOOL *pBoolOut)
5021 return _VarBoolFromUI2(usIn, pBoolOut);
5024 /************************************************************************
5025 * VarBoolFromUI4 (OLEAUT32.235)
5027 * Convert a VT_UI4 to a VT_BOOL.
5031 * pBoolOut [O] Destination
5036 HRESULT WINAPI VarBoolFromUI4(ULONG ulIn, VARIANT_BOOL *pBoolOut)
5038 return _VarBoolFromUI4(ulIn, pBoolOut);
5041 /************************************************************************
5042 * VarBoolFromDec (OLEAUT32.236)
5044 * Convert a VT_DECIMAL to a VT_BOOL.
5048 * pBoolOut [O] Destination
5052 * Failure: E_INVALIDARG, if pDecIn is invalid.
5054 HRESULT WINAPI VarBoolFromDec(DECIMAL* pDecIn, VARIANT_BOOL *pBoolOut)
5056 if (DEC_SCALE(pDecIn) > DEC_MAX_SCALE || (DEC_SIGN(pDecIn) & ~DECIMAL_NEG))
5057 return E_INVALIDARG;
5059 if (DEC_HI32(pDecIn) || DEC_MID32(pDecIn) || DEC_LO32(pDecIn))
5060 *pBoolOut = VARIANT_TRUE;
5062 *pBoolOut = VARIANT_FALSE;
5066 /************************************************************************
5067 * VarBoolFromI8 (OLEAUT32.370)
5069 * Convert a VT_I8 to a VT_BOOL.
5073 * pBoolOut [O] Destination
5078 HRESULT WINAPI VarBoolFromI8(LONG64 llIn, VARIANT_BOOL *pBoolOut)
5080 return _VarBoolFromI8(llIn, pBoolOut);
5083 /************************************************************************
5084 * VarBoolFromUI8 (OLEAUT32.371)
5086 * Convert a VT_UI8 to a VT_BOOL.
5090 * pBoolOut [O] Destination
5095 HRESULT WINAPI VarBoolFromUI8(ULONG64 ullIn, VARIANT_BOOL *pBoolOut)
5097 return _VarBoolFromUI8(ullIn, pBoolOut);
5103 /* Write a number from a UI8 and sign */
5104 static WCHAR *VARIANT_WriteNumber(ULONG64 ulVal, WCHAR* szOut)
5108 WCHAR ulNextDigit = ulVal % 10;
5110 *szOut-- = '0' + ulNextDigit;
5111 ulVal = (ulVal - ulNextDigit) / 10;
5118 /* Create a (possibly localised) BSTR from a UI8 and sign */
5119 static BSTR VARIANT_MakeBstr(LCID lcid, DWORD dwFlags, WCHAR *szOut)
5121 WCHAR szConverted[256];
5123 if (dwFlags & VAR_NEGATIVE)
5126 if (dwFlags & LOCALE_USE_NLS)
5128 /* Format the number for the locale */
5129 szConverted[0] = '\0';
5130 GetNumberFormatW(lcid,
5131 dwFlags & LOCALE_NOUSEROVERRIDE,
5132 szOut, NULL, szConverted, sizeof(szConverted)/sizeof(WCHAR));
5133 szOut = szConverted;
5135 return SysAllocStringByteLen((LPCSTR)szOut, strlenW(szOut) * sizeof(WCHAR));
5138 /* Create a (possibly localised) BSTR from a UI8 and sign */
5139 static HRESULT VARIANT_BstrFromUInt(ULONG64 ulVal, LCID lcid, DWORD dwFlags, BSTR *pbstrOut)
5141 WCHAR szBuff[64], *szOut = szBuff + sizeof(szBuff)/sizeof(WCHAR) - 1;
5144 return E_INVALIDARG;
5146 /* Create the basic number string */
5148 szOut = VARIANT_WriteNumber(ulVal, szOut);
5150 *pbstrOut = VARIANT_MakeBstr(lcid, dwFlags, szOut);
5151 TRACE("returning %s\n", debugstr_w(*pbstrOut));
5152 return *pbstrOut ? S_OK : E_OUTOFMEMORY;
5155 /******************************************************************************
5156 * VarBstrFromUI1 (OLEAUT32.108)
5158 * Convert a VT_UI1 to a VT_BSTR.
5162 * lcid [I] LCID for the conversion
5163 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
5164 * pbstrOut [O] Destination
5168 * Failure: E_INVALIDARG, if pbstrOut is invalid.
5169 * E_OUTOFMEMORY, if memory allocation fails.
5171 HRESULT WINAPI VarBstrFromUI1(BYTE bIn, LCID lcid, ULONG dwFlags, BSTR* pbstrOut)
5173 return VARIANT_BstrFromUInt(bIn, lcid, dwFlags, pbstrOut);
5176 /******************************************************************************
5177 * VarBstrFromI2 (OLEAUT32.109)
5179 * Convert a VT_I2 to a VT_BSTR.
5183 * lcid [I] LCID for the conversion
5184 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
5185 * pbstrOut [O] Destination
5189 * Failure: E_INVALIDARG, if pbstrOut is invalid.
5190 * E_OUTOFMEMORY, if memory allocation fails.
5192 HRESULT WINAPI VarBstrFromI2(short sIn, LCID lcid, ULONG dwFlags, BSTR* pbstrOut)
5199 dwFlags |= VAR_NEGATIVE;
5201 return VARIANT_BstrFromUInt(ul64, lcid, dwFlags, pbstrOut);
5204 /******************************************************************************
5205 * VarBstrFromI4 (OLEAUT32.110)
5207 * Convert a VT_I4 to a VT_BSTR.
5211 * lcid [I] LCID for the conversion
5212 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
5213 * pbstrOut [O] Destination
5217 * Failure: E_INVALIDARG, if pbstrOut is invalid.
5218 * E_OUTOFMEMORY, if memory allocation fails.
5220 HRESULT WINAPI VarBstrFromI4(LONG lIn, LCID lcid, ULONG dwFlags, BSTR* pbstrOut)
5227 dwFlags |= VAR_NEGATIVE;
5229 return VARIANT_BstrFromUInt(ul64, lcid, dwFlags, pbstrOut);
5232 static HRESULT VARIANT_BstrFromReal(DOUBLE dblIn, LCID lcid, ULONG dwFlags,
5233 BSTR* pbstrOut, LPCWSTR lpszFormat)
5238 return E_INVALIDARG;
5240 sprintfW( buff, lpszFormat, dblIn );
5241 TRACE("created string %s\n", debugstr_w(buff));
5242 if (dwFlags & LOCALE_USE_NLS)
5246 /* Format the number for the locale */
5248 GetNumberFormatW(lcid, dwFlags & LOCALE_NOUSEROVERRIDE,
5249 buff, NULL, numbuff, sizeof(numbuff) / sizeof(WCHAR));
5250 TRACE("created NLS string %s\n", debugstr_w(numbuff));
5251 *pbstrOut = SysAllocString(numbuff);
5254 *pbstrOut = SysAllocString(buff);
5255 return *pbstrOut ? S_OK : E_OUTOFMEMORY;
5258 /******************************************************************************
5259 * VarBstrFromR4 (OLEAUT32.111)
5261 * Convert a VT_R4 to a VT_BSTR.
5265 * lcid [I] LCID for the conversion
5266 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
5267 * pbstrOut [O] Destination
5271 * Failure: E_INVALIDARG, if pbstrOut is invalid.
5272 * E_OUTOFMEMORY, if memory allocation fails.
5274 HRESULT WINAPI VarBstrFromR4(FLOAT fltIn, LCID lcid, ULONG dwFlags, BSTR* pbstrOut)
5276 return VARIANT_BstrFromReal(fltIn, lcid, dwFlags, pbstrOut, szFloatFormatW);
5279 /******************************************************************************
5280 * VarBstrFromR8 (OLEAUT32.112)
5282 * Convert a VT_R8 to a VT_BSTR.
5286 * lcid [I] LCID for the conversion
5287 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
5288 * pbstrOut [O] Destination
5292 * Failure: E_INVALIDARG, if pbstrOut is invalid.
5293 * E_OUTOFMEMORY, if memory allocation fails.
5295 HRESULT WINAPI VarBstrFromR8(double dblIn, LCID lcid, ULONG dwFlags, BSTR* pbstrOut)
5297 return VARIANT_BstrFromReal(dblIn, lcid, dwFlags, pbstrOut, szDoubleFormatW);
5300 /******************************************************************************
5301 * VarBstrFromCy [OLEAUT32.113]
5303 * Convert a VT_CY to a VT_BSTR.
5307 * lcid [I] LCID for the conversion
5308 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
5309 * pbstrOut [O] Destination
5313 * Failure: E_INVALIDARG, if pbstrOut is invalid.
5314 * E_OUTOFMEMORY, if memory allocation fails.
5316 HRESULT WINAPI VarBstrFromCy(CY cyIn, LCID lcid, ULONG dwFlags, BSTR *pbstrOut)
5322 return E_INVALIDARG;
5324 VarR8FromCy(cyIn, &dblVal);
5325 sprintfW(buff, szDoubleFormatW, dblVal);
5327 if (dwFlags & LOCALE_USE_NLS)
5331 /* Format the currency for the locale */
5333 GetCurrencyFormatW(lcid, dwFlags & LOCALE_NOUSEROVERRIDE,
5334 buff, NULL, cybuff, sizeof(cybuff) / sizeof(WCHAR));
5335 *pbstrOut = SysAllocString(cybuff);
5338 *pbstrOut = SysAllocString(buff);
5340 return *pbstrOut ? S_OK : E_OUTOFMEMORY;
5343 /******************************************************************************
5344 * VarBstrFromDate [OLEAUT32.114]
5346 * Convert a VT_DATE to a VT_BSTR.
5350 * lcid [I] LCID for the conversion
5351 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
5352 * pbstrOut [O] Destination
5356 * Failure: E_INVALIDARG, if pbstrOut or dateIn is invalid.
5357 * E_OUTOFMEMORY, if memory allocation fails.
5359 HRESULT WINAPI VarBstrFromDate(DATE dateIn, LCID lcid, ULONG dwFlags, BSTR* pbstrOut)
5362 DWORD dwFormatFlags = dwFlags & LOCALE_NOUSEROVERRIDE;
5363 WCHAR date[128], *time;
5365 TRACE("(%g,0x%08lx,0x%08lx,%p)\n", dateIn, lcid, dwFlags, pbstrOut);
5367 if (!pbstrOut || !VariantTimeToSystemTime(dateIn, &st))
5368 return E_INVALIDARG;
5372 if (dwFlags & VAR_CALENDAR_THAI)
5373 st.wYear += 553; /* Use the Thai buddhist calendar year */
5374 else if (dwFlags & (VAR_CALENDAR_HIJRI|VAR_CALENDAR_GREGORIAN))
5375 FIXME("VAR_CALENDAR_HIJRI/VAR_CALENDAR_GREGORIAN not handled\n");
5377 if (dwFlags & LOCALE_USE_NLS)
5378 dwFlags &= ~(VAR_TIMEVALUEONLY|VAR_DATEVALUEONLY);
5381 double whole = dateIn < 0 ? ceil(dateIn) : floor(dateIn);
5382 double partial = dateIn - whole;
5385 dwFlags |= VAR_TIMEVALUEONLY;
5386 else if (partial < 1e-12)
5387 dwFlags |= VAR_DATEVALUEONLY;
5390 if (dwFlags & VAR_TIMEVALUEONLY)
5393 if (!GetDateFormatW(lcid, dwFormatFlags|DATE_SHORTDATE, &st, NULL, date,
5394 sizeof(date)/sizeof(WCHAR)))
5395 return E_INVALIDARG;
5397 if (!(dwFlags & VAR_DATEVALUEONLY))
5399 time = date + strlenW(date);
5402 if (!GetTimeFormatW(lcid, dwFormatFlags, &st, NULL, time,
5403 sizeof(date)/sizeof(WCHAR)-(time-date)))
5404 return E_INVALIDARG;
5407 *pbstrOut = SysAllocString(date);
5409 TRACE("returning %s\n", debugstr_w(*pbstrOut));
5410 return *pbstrOut ? S_OK : E_OUTOFMEMORY;
5413 /******************************************************************************
5414 * VarBstrFromBool (OLEAUT32.116)
5416 * Convert a VT_BOOL to a VT_BSTR.
5420 * lcid [I] LCID for the conversion
5421 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
5422 * pbstrOut [O] Destination
5426 * Failure: E_INVALIDARG, if pbstrOut is invalid.
5427 * E_OUTOFMEMORY, if memory allocation fails.
5430 * If dwFlags includes VARIANT_LOCALBOOL, this function converts to the
5431 * localised text of "True" or "False". To convert a bool into a
5432 * numeric string of "0" or "-1", use VariantChangeTypeTypeEx().
5434 HRESULT WINAPI VarBstrFromBool(VARIANT_BOOL boolIn, LCID lcid, ULONG dwFlags, BSTR* pbstrOut)
5437 DWORD dwResId = IDS_TRUE;
5440 TRACE("%d,0x%08lx,0x%08lx,%p\n", boolIn, lcid, dwFlags, pbstrOut);
5443 return E_INVALIDARG;
5445 /* VAR_BOOLONOFF and VAR_BOOLYESNO are internal flags used
5446 * for variant formatting */
5447 switch (dwFlags & (VAR_LOCALBOOL|VAR_BOOLONOFF|VAR_BOOLYESNO))
5458 lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),SORT_DEFAULT);
5461 lcid = ConvertDefaultLocale(lcid);
5462 langId = LANGIDFROMLCID(lcid);
5463 if (PRIMARYLANGID(langId) == LANG_NEUTRAL)
5464 langId = MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT);
5466 if (boolIn == VARIANT_FALSE)
5467 dwResId++; /* Use negative form */
5469 VarBstrFromBool_GetLocalised:
5470 if (VARIANT_GetLocalisedText(langId, dwResId, szBuff))
5472 *pbstrOut = SysAllocString(szBuff);
5473 return *pbstrOut ? S_OK : E_OUTOFMEMORY;
5476 if (langId != MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT))
5478 langId = MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT);
5479 goto VarBstrFromBool_GetLocalised;
5482 /* Should never get here */
5483 WARN("Failed to load bool text!\n");
5484 return E_OUTOFMEMORY;
5487 /******************************************************************************
5488 * VarBstrFromI1 (OLEAUT32.229)
5490 * Convert a VT_I1 to a VT_BSTR.
5494 * lcid [I] LCID for the conversion
5495 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
5496 * pbstrOut [O] Destination
5500 * Failure: E_INVALIDARG, if pbstrOut is invalid.
5501 * E_OUTOFMEMORY, if memory allocation fails.
5503 HRESULT WINAPI VarBstrFromI1(signed char cIn, LCID lcid, ULONG dwFlags, BSTR* pbstrOut)
5510 dwFlags |= VAR_NEGATIVE;
5512 return VARIANT_BstrFromUInt(ul64, lcid, dwFlags, pbstrOut);
5515 /******************************************************************************
5516 * VarBstrFromUI2 (OLEAUT32.230)
5518 * Convert a VT_UI2 to a VT_BSTR.
5522 * lcid [I] LCID for the conversion
5523 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
5524 * pbstrOut [O] Destination
5528 * Failure: E_INVALIDARG, if pbstrOut is invalid.
5529 * E_OUTOFMEMORY, if memory allocation fails.
5531 HRESULT WINAPI VarBstrFromUI2(USHORT usIn, LCID lcid, ULONG dwFlags, BSTR* pbstrOut)
5533 return VARIANT_BstrFromUInt(usIn, lcid, dwFlags, pbstrOut);
5536 /******************************************************************************
5537 * VarBstrFromUI4 (OLEAUT32.231)
5539 * Convert a VT_UI4 to a VT_BSTR.
5543 * lcid [I] LCID for the conversion
5544 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
5545 * pbstrOut [O] Destination
5549 * Failure: E_INVALIDARG, if pbstrOut is invalid.
5550 * E_OUTOFMEMORY, if memory allocation fails.
5552 HRESULT WINAPI VarBstrFromUI4(ULONG ulIn, LCID lcid, ULONG dwFlags, BSTR* pbstrOut)
5554 return VARIANT_BstrFromUInt(ulIn, lcid, dwFlags, pbstrOut);
5557 /******************************************************************************
5558 * VarBstrFromDec (OLEAUT32.232)
5560 * Convert a VT_DECIMAL to a VT_BSTR.
5564 * lcid [I] LCID for the conversion
5565 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
5566 * pbstrOut [O] Destination
5570 * Failure: E_INVALIDARG, if pbstrOut is invalid.
5571 * E_OUTOFMEMORY, if memory allocation fails.
5573 HRESULT WINAPI VarBstrFromDec(DECIMAL* pDecIn, LCID lcid, ULONG dwFlags, BSTR* pbstrOut)
5576 return E_INVALIDARG;
5578 if (!DEC_SCALE(pDecIn) && !DEC_HI32(pDecIn))
5580 WCHAR szBuff[256], *szOut = szBuff + sizeof(szBuff)/sizeof(WCHAR) - 1;
5582 /* Create the basic number string */
5584 szOut = VARIANT_WriteNumber(DEC_LO64(pDecIn), szOut);
5585 if (DEC_SIGN(pDecIn))
5586 dwFlags |= VAR_NEGATIVE;
5588 *pbstrOut = VARIANT_MakeBstr(lcid, dwFlags, szOut);
5589 TRACE("returning %s\n", debugstr_w(*pbstrOut));
5590 return *pbstrOut ? S_OK : E_OUTOFMEMORY;
5592 FIXME("semi-stub\n");
5593 return E_INVALIDARG;
5596 /************************************************************************
5597 * VarBstrFromI8 (OLEAUT32.370)
5599 * Convert a VT_I8 to a VT_BSTR.
5603 * lcid [I] LCID for the conversion
5604 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
5605 * pbstrOut [O] Destination
5609 * Failure: E_INVALIDARG, if pbstrOut is invalid.
5610 * E_OUTOFMEMORY, if memory allocation fails.
5612 HRESULT WINAPI VarBstrFromI8(LONG64 llIn, LCID lcid, ULONG dwFlags, BSTR* pbstrOut)
5614 ULONG64 ul64 = llIn;
5619 dwFlags |= VAR_NEGATIVE;
5621 return VARIANT_BstrFromUInt(ul64, lcid, dwFlags, pbstrOut);
5624 /************************************************************************
5625 * VarBstrFromUI8 (OLEAUT32.371)
5627 * Convert a VT_UI8 to a VT_BSTR.
5631 * lcid [I] LCID for the conversion
5632 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
5633 * pbstrOut [O] Destination
5637 * Failure: E_INVALIDARG, if pbstrOut is invalid.
5638 * E_OUTOFMEMORY, if memory allocation fails.
5640 HRESULT WINAPI VarBstrFromUI8(ULONG64 ullIn, LCID lcid, ULONG dwFlags, BSTR* pbstrOut)
5642 return VARIANT_BstrFromUInt(ullIn, lcid, dwFlags, pbstrOut);
5645 /**********************************************************************
5646 * VarBstrCat (OLEAUT32.313)
5648 * Concatenate two BSTR values.
5651 * pbstrLeft [I] Source
5652 * pbstrRight [I] Value to concatenate
5653 * pbstrOut [O] Destination
5657 * Failure: E_INVALIDARG, if pbstrOut is invalid.
5658 * E_OUTOFMEMORY, if memory allocation fails.
5660 HRESULT WINAPI VarBstrCat(BSTR pbstrLeft, BSTR pbstrRight, BSTR *pbstrOut)
5665 return E_INVALIDARG;
5667 len = pbstrLeft ? strlenW(pbstrLeft) : 0;
5669 len += strlenW(pbstrRight);
5671 *pbstrOut = SysAllocStringLen(NULL, len);
5673 return E_OUTOFMEMORY;
5675 (*pbstrOut)[0] = '\0';
5678 strcpyW(*pbstrOut, pbstrLeft);
5681 strcatW(*pbstrOut, pbstrRight);
5686 /**********************************************************************
5687 * VarBstrCmp (OLEAUT32.314)
5689 * Compare two BSTR values.
5692 * pbstrLeft [I] Source
5693 * pbstrRight [I] Value to compare
5694 * lcid [I] LCID for the comparason
5695 * dwFlags [I] Flags to pass directly to CompareStringW().
5698 * VARCMP_LT, VARCMP_EQ or VARCMP_GT indicating that pbstrLeft is less
5699 * than, equal to or greater than pbstrRight respectively.
5700 * VARCMP_NULL is returned if either string is NULL, unless both are NULL
5701 * in which case VARCMP_EQ is returned.
5703 HRESULT WINAPI VarBstrCmp(BSTR pbstrLeft, BSTR pbstrRight, LCID lcid, DWORD dwFlags)
5707 if (!pbstrRight || !*pbstrRight)
5711 else if (!pbstrRight)
5718 return CompareStringW(lcid, dwFlags, pbstrLeft, -1, pbstrRight, -1) - 1;
5725 /******************************************************************************
5726 * VarDateFromUI1 (OLEAUT32.88)
5728 * Convert a VT_UI1 to a VT_DATE.
5732 * pdateOut [O] Destination
5737 HRESULT WINAPI VarDateFromUI1(BYTE bIn, DATE* pdateOut)
5739 return _VarDateFromUI1(bIn, pdateOut);
5742 /******************************************************************************
5743 * VarDateFromI2 (OLEAUT32.89)
5745 * Convert a VT_I2 to a VT_DATE.
5749 * pdateOut [O] Destination
5754 HRESULT WINAPI VarDateFromI2(short sIn, DATE* pdateOut)
5756 return _VarDateFromI2(sIn, pdateOut);
5759 /******************************************************************************
5760 * VarDateFromI4 (OLEAUT32.90)
5762 * Convert a VT_I4 to a VT_DATE.
5766 * pdateOut [O] Destination
5771 HRESULT WINAPI VarDateFromI4(LONG lIn, DATE* pdateOut)
5773 return _VarDateFromI4(lIn, pdateOut);
5776 /******************************************************************************
5777 * VarDateFromR4 (OLEAUT32.91)
5779 * Convert a VT_R4 to a VT_DATE.
5783 * pdateOut [O] Destination
5788 HRESULT WINAPI VarDateFromR4(FLOAT fltIn, DATE* pdateOut)
5790 return _VarDateFromR4(fltIn, pdateOut);
5793 /******************************************************************************
5794 * VarDateFromR8 (OLEAUT32.92)
5796 * Convert a VT_R8 to a VT_DATE.
5800 * pdateOut [O] Destination
5805 HRESULT WINAPI VarDateFromR8(double dblIn, DATE* pdateOut)
5807 return _VarDateFromR8(dblIn, pdateOut);
5810 /**********************************************************************
5811 * VarDateFromDisp (OLEAUT32.95)
5813 * Convert a VT_DISPATCH to a VT_DATE.
5816 * pdispIn [I] Source
5817 * lcid [I] LCID for conversion
5818 * pdateOut [O] Destination
5822 * Failure: E_INVALIDARG, if the source value is invalid
5823 * DISP_E_OVERFLOW, if the value will not fit in the destination
5824 * DISP_E_TYPEMISMATCH, if the type cannot be converted
5826 HRESULT WINAPI VarDateFromDisp(IDispatch* pdispIn, LCID lcid, DATE* pdateOut)
5828 return _VarDateFromDisp(pdispIn, lcid, pdateOut);
5831 /******************************************************************************
5832 * VarDateFromBool (OLEAUT32.96)
5834 * Convert a VT_BOOL to a VT_DATE.
5838 * pdateOut [O] Destination
5843 HRESULT WINAPI VarDateFromBool(VARIANT_BOOL boolIn, DATE* pdateOut)
5845 return _VarDateFromBool(boolIn, pdateOut);
5848 /**********************************************************************
5849 * VarDateFromCy (OLEAUT32.93)
5851 * Convert a VT_CY to a VT_DATE.
5855 * pdateOut [O] Destination
5860 HRESULT WINAPI VarDateFromCy(CY cyIn, DATE* pdateOut)
5862 return _VarDateFromCy(cyIn, pdateOut);
5865 /* Date string parsing */
5866 #define DP_TIMESEP 0x01 /* Time seperator ( _must_ remain 0x1, used as a bitmask) */
5867 #define DP_DATESEP 0x02 /* Date seperator */
5868 #define DP_MONTH 0x04 /* Month name */
5869 #define DP_AM 0x08 /* AM */
5870 #define DP_PM 0x10 /* PM */
5872 typedef struct tagDATEPARSE
5874 DWORD dwCount; /* Number of fields found so far (maximum 6) */
5875 DWORD dwParseFlags; /* Global parse flags (DP_ Flags above) */
5876 DWORD dwFlags[6]; /* Flags for each field */
5877 DWORD dwValues[6]; /* Value of each field */
5880 #define TIMEFLAG(i) ((dp.dwFlags[i] & DP_TIMESEP) << i)
5882 #define IsLeapYear(y) (((y % 4) == 0) && (((y % 100) != 0) || ((y % 400) == 0)))
5884 /* Determine if a day is valid in a given month of a given year */
5885 static BOOL VARIANT_IsValidMonthDay(DWORD day, DWORD month, DWORD year)
5887 static const BYTE days[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
5889 if (day && month && month < 13)
5891 if (day <= days[month] || (month == 2 && day == 29 && IsLeapYear(year)))
5897 /* Possible orders for 3 numbers making up a date */
5898 #define ORDER_MDY 0x01
5899 #define ORDER_YMD 0x02
5900 #define ORDER_YDM 0x04
5901 #define ORDER_DMY 0x08
5902 #define ORDER_MYD 0x10 /* Synthetic order, used only for funky 2 digit dates */
5904 /* Determine a date for a particular locale, from 3 numbers */
5905 static inline HRESULT VARIANT_MakeDate(DATEPARSE *dp, DWORD iDate,
5906 DWORD offset, SYSTEMTIME *st)
5908 DWORD dwAllOrders, dwTry, dwCount = 0, v1, v2, v3;
5912 v1 = 30; /* Default to (Variant) 0 date part */
5915 goto VARIANT_MakeDate_OK;
5918 v1 = dp->dwValues[offset + 0];
5919 v2 = dp->dwValues[offset + 1];
5920 if (dp->dwCount == 2)
5923 GetSystemTime(¤t);
5927 v3 = dp->dwValues[offset + 2];
5929 TRACE("(%ld,%ld,%ld,%ld,%ld)\n", v1, v2, v3, iDate, offset);
5931 /* If one number must be a month (Because a month name was given), then only
5932 * consider orders with the month in that position.
5933 * If we took the current year as 'v3', then only allow a year in that position.
5935 if (dp->dwFlags[offset + 0] & DP_MONTH)
5937 dwAllOrders = ORDER_MDY;
5939 else if (dp->dwFlags[offset + 1] & DP_MONTH)
5941 dwAllOrders = ORDER_DMY;
5942 if (dp->dwCount > 2)
5943 dwAllOrders |= ORDER_YMD;
5945 else if (dp->dwCount > 2 && dp->dwFlags[offset + 2] & DP_MONTH)
5947 dwAllOrders = ORDER_YDM;
5951 dwAllOrders = ORDER_MDY|ORDER_DMY;
5952 if (dp->dwCount > 2)
5953 dwAllOrders |= (ORDER_YMD|ORDER_YDM);
5956 VARIANT_MakeDate_Start:
5957 TRACE("dwAllOrders is 0x%08lx\n", dwAllOrders);
5965 /* First: Try the order given by iDate */
5968 case 0: dwTry = dwAllOrders & ORDER_MDY; break;
5969 case 1: dwTry = dwAllOrders & ORDER_DMY; break;
5970 default: dwTry = dwAllOrders & ORDER_YMD; break;
5973 else if (dwCount == 1)
5975 /* Second: Try all the orders compatable with iDate */
5978 case 0: dwTry = dwAllOrders & ~(ORDER_DMY|ORDER_YDM); break;
5979 case 1: dwTry = dwAllOrders & ~(ORDER_MDY|ORDER_YMD|ORDER_MYD); break;
5980 default: dwTry = dwAllOrders & ~(ORDER_DMY|ORDER_YDM); break;
5985 /* Finally: Try any remaining orders */
5986 dwTry = dwAllOrders;
5989 TRACE("Attempt %ld, dwTry is 0x%08lx\n", dwCount, dwTry);
5995 #define DATE_SWAP(x,y) do { dwTemp = x; x = y; y = dwTemp; } while (0)
5997 if (dwTry & ORDER_MDY)
5999 if (VARIANT_IsValidMonthDay(v2,v1,v3))
6002 goto VARIANT_MakeDate_OK;
6004 dwAllOrders &= ~ORDER_MDY;
6006 if (dwTry & ORDER_YMD)
6008 if (VARIANT_IsValidMonthDay(v3,v2,v1))
6011 goto VARIANT_MakeDate_OK;
6013 dwAllOrders &= ~ORDER_YMD;
6015 if (dwTry & ORDER_YDM)
6017 if (VARIANT_IsValidMonthDay(v2,v3,v1))
6021 goto VARIANT_MakeDate_OK;
6023 dwAllOrders &= ~ORDER_YDM;
6025 if (dwTry & ORDER_DMY)
6027 if (VARIANT_IsValidMonthDay(v1,v2,v3))
6028 goto VARIANT_MakeDate_OK;
6029 dwAllOrders &= ~ORDER_DMY;
6031 if (dwTry & ORDER_MYD)
6033 /* Only occurs if we are trying a 2 year date as M/Y not D/M */
6034 if (VARIANT_IsValidMonthDay(v3,v1,v2))
6038 goto VARIANT_MakeDate_OK;
6040 dwAllOrders &= ~ORDER_MYD;
6044 if (dp->dwCount == 2)
6046 /* We couldn't make a date as D/M or M/D, so try M/Y or Y/M */
6047 v3 = 1; /* 1st of the month */
6048 dwAllOrders = ORDER_YMD|ORDER_MYD;
6049 dp->dwCount = 0; /* Don't return to this code path again */
6051 goto VARIANT_MakeDate_Start;
6054 /* No valid dates were able to be constructed */
6055 return DISP_E_TYPEMISMATCH;
6057 VARIANT_MakeDate_OK:
6059 /* Check that the time part is ok */
6060 if (st->wHour > 23 || st->wMinute > 59 || st->wSecond > 59)
6061 return DISP_E_TYPEMISMATCH;
6063 TRACE("Time %d %d %d\n", st->wHour, st->wMinute, st->wSecond);
6064 if (st->wHour < 12 && (dp->dwParseFlags & DP_PM))
6066 else if (st->wHour == 12 && (dp->dwParseFlags & DP_AM))
6068 TRACE("Time %d %d %d\n", st->wHour, st->wMinute, st->wSecond);
6072 /* FIXME: For 2 digit dates, I'm not sure if 30 is hard coded or not. It may
6073 * be retrieved from:
6074 * HKCU\Control Panel\International\Calendars\TwoDigitYearMax
6075 * But Wine doesn't have/use that key as at the time of writing.
6077 st->wYear = v3 < 30 ? 2000 + v3 : v3 < 100 ? 1900 + v3 : v3;
6078 TRACE("Returning date %ld/%ld/%d\n", v1, v2, st->wYear);
6082 /******************************************************************************
6083 * VarDateFromStr [OLEAUT32.94]
6085 * Convert a VT_BSTR to at VT_DATE.
6088 * strIn [I] String to convert
6089 * lcid [I] Locale identifier for the conversion
6090 * dwFlags [I] Flags affecting the conversion (VAR_ flags from "oleauto.h")
6091 * pdateOut [O] Destination for the converted value
6094 * Success: S_OK. pdateOut contains the converted value.
6095 * FAILURE: An HRESULT error code indicating the prolem.
6098 * Any date format that can be created using the date formats from lcid
6099 * (Either from kernel Nls functions, variant conversion or formatting) is a
6100 * valid input to this function. In addition, a few more esoteric formats are
6101 * also supported for compatability with the native version. The date is
6102 * interpreted according to the date settings in the control panel, unless
6103 * the date is invalid in that format, in which the most compatable format
6104 * that produces a valid date will be used.
6106 HRESULT WINAPI VarDateFromStr(OLECHAR* strIn, LCID lcid, ULONG dwFlags, DATE* pdateOut)
6108 static const USHORT ParseDateTokens[] =
6110 LOCALE_SMONTHNAME1, LOCALE_SMONTHNAME2, LOCALE_SMONTHNAME3, LOCALE_SMONTHNAME4,
6111 LOCALE_SMONTHNAME5, LOCALE_SMONTHNAME6, LOCALE_SMONTHNAME7, LOCALE_SMONTHNAME8,
6112 LOCALE_SMONTHNAME9, LOCALE_SMONTHNAME10, LOCALE_SMONTHNAME11, LOCALE_SMONTHNAME12,
6113 LOCALE_SMONTHNAME13,
6114 LOCALE_SABBREVMONTHNAME1, LOCALE_SABBREVMONTHNAME2, LOCALE_SABBREVMONTHNAME3,
6115 LOCALE_SABBREVMONTHNAME4, LOCALE_SABBREVMONTHNAME5, LOCALE_SABBREVMONTHNAME6,
6116 LOCALE_SABBREVMONTHNAME7, LOCALE_SABBREVMONTHNAME8, LOCALE_SABBREVMONTHNAME9,
6117 LOCALE_SABBREVMONTHNAME10, LOCALE_SABBREVMONTHNAME11, LOCALE_SABBREVMONTHNAME12,
6118 LOCALE_SABBREVMONTHNAME13,
6119 LOCALE_SDAYNAME1, LOCALE_SDAYNAME2, LOCALE_SDAYNAME3, LOCALE_SDAYNAME4,
6120 LOCALE_SDAYNAME5, LOCALE_SDAYNAME6, LOCALE_SDAYNAME7,
6121 LOCALE_SABBREVDAYNAME1, LOCALE_SABBREVDAYNAME2, LOCALE_SABBREVDAYNAME3,
6122 LOCALE_SABBREVDAYNAME4, LOCALE_SABBREVDAYNAME5, LOCALE_SABBREVDAYNAME6,
6123 LOCALE_SABBREVDAYNAME7,
6124 LOCALE_S1159, LOCALE_S2359
6126 static const BYTE ParseDateMonths[] =
6128 1,2,3,4,5,6,7,8,9,10,11,12,13,
6129 1,2,3,4,5,6,7,8,9,10,11,12,13
6132 BSTR tokens[sizeof(ParseDateTokens)/sizeof(ParseDateTokens[0])];
6134 DWORD dwDateSeps = 0, iDate = 0;
6135 HRESULT hRet = S_OK;
6137 if ((dwFlags & (VAR_TIMEVALUEONLY|VAR_DATEVALUEONLY)) ==
6138 (VAR_TIMEVALUEONLY|VAR_DATEVALUEONLY))
6139 return E_INVALIDARG;
6142 return DISP_E_TYPEMISMATCH;
6146 TRACE("(%s,0x%08lx,0x%08lx,%p)\n", debugstr_w(strIn), lcid, dwFlags, pdateOut);
6148 memset(&dp, 0, sizeof(dp));
6150 GetLocaleInfoW(lcid, LOCALE_IDATE|LOCALE_RETURN_NUMBER|(dwFlags & LOCALE_NOUSEROVERRIDE),
6151 (LPWSTR)&iDate, sizeof(iDate)/sizeof(WCHAR));
6152 TRACE("iDate is %ld\n", iDate);
6154 /* Get the month/day/am/pm tokens for this locale */
6155 for (i = 0; i < sizeof(tokens)/sizeof(tokens[0]); i++)
6158 LCTYPE lctype = ParseDateTokens[i] | (dwFlags & LOCALE_NOUSEROVERRIDE);
6160 /* FIXME: Alternate calendars - should use GetCalendarInfo() and/or
6161 * GetAltMonthNames(). We should really cache these strings too.
6164 GetLocaleInfoW(lcid, lctype, buff, sizeof(buff)/sizeof(WCHAR));
6165 tokens[i] = SysAllocString(buff);
6166 TRACE("token %d is %s\n", i, debugstr_w(tokens[i]));
6169 /* Parse the string into our structure */
6175 if (isdigitW(*strIn))
6177 dp.dwValues[dp.dwCount] = strtoulW(strIn, &strIn, 10);
6181 else if (isalpha(*strIn))
6183 BOOL bFound = FALSE;
6185 for (i = 0; i < sizeof(tokens)/sizeof(tokens[0]); i++)
6187 DWORD dwLen = strlenW(tokens[i]);
6188 if (dwLen && !strncmpiW(strIn, tokens[i], dwLen))
6192 dp.dwValues[dp.dwCount] = ParseDateMonths[i];
6193 dp.dwFlags[dp.dwCount] |= (DP_MONTH|DP_DATESEP);
6198 if (!dp.dwCount || dp.dwParseFlags & (DP_AM|DP_PM))
6199 hRet = DISP_E_TYPEMISMATCH;
6202 dp.dwFlags[dp.dwCount - 1] |= (i == 40 ? DP_AM : DP_PM);
6203 dp.dwParseFlags |= (i == 40 ? DP_AM : DP_PM);
6206 strIn += (dwLen - 1);
6214 if ((*strIn == 'a' || *strIn == 'A' || *strIn == 'p' || *strIn == 'P') &&
6215 (dp.dwCount && !(dp.dwParseFlags & (DP_AM|DP_PM))))
6217 /* Special case - 'a' and 'p' are recognised as short for am/pm */
6218 if (*strIn == 'a' || *strIn == 'A')
6220 dp.dwFlags[dp.dwCount - 1] |= DP_AM;
6221 dp.dwParseFlags |= DP_AM;
6225 dp.dwFlags[dp.dwCount - 1] |= DP_PM;
6226 dp.dwParseFlags |= DP_PM;
6232 TRACE("No matching token for %s\n", debugstr_w(strIn));
6233 hRet = DISP_E_TYPEMISMATCH;
6238 else if (*strIn == ':' || *strIn == '.')
6240 if (!dp.dwCount || !strIn[1])
6241 hRet = DISP_E_TYPEMISMATCH;
6243 dp.dwFlags[dp.dwCount - 1] |= DP_TIMESEP;
6245 else if (*strIn == '-' || *strIn == '/')
6248 if (dwDateSeps > 2 || !dp.dwCount || !strIn[1])
6249 hRet = DISP_E_TYPEMISMATCH;
6251 dp.dwFlags[dp.dwCount - 1] |= DP_DATESEP;
6253 else if (*strIn == ',' || isspaceW(*strIn))
6255 if (*strIn == ',' && !strIn[1])
6256 hRet = DISP_E_TYPEMISMATCH;
6260 hRet = DISP_E_TYPEMISMATCH;
6265 if (!dp.dwCount || dp.dwCount > 6 ||
6266 (dp.dwCount == 1 && !(dp.dwParseFlags & (DP_AM|DP_PM))))
6267 hRet = DISP_E_TYPEMISMATCH;
6269 if (SUCCEEDED(hRet))
6272 DWORD dwOffset = 0; /* Start of date fields in dp.dwValues */
6274 st.wDayOfWeek = st.wHour = st.wMinute = st.wSecond = st.wMilliseconds = 0;
6276 /* Figure out which numbers correspond to which fields.
6278 * This switch statement works based on the fact that native interprets any
6279 * fields that are not joined with a time seperator ('.' or ':') as date
6280 * fields. Thus we construct a value from 0-32 where each set bit indicates
6281 * a time field. This encapsulates the hundreds of permutations of 2-6 fields.
6282 * For valid permutations, we set dwOffset to point to the first date field
6283 * and shorten dp.dwCount by the number of time fields found. The real
6284 * magic here occurs in VARIANT_MakeDate() above, where we determine what
6285 * each date number must represent in the context of iDate.
6287 TRACE("0x%08lx\n", TIMEFLAG(0)|TIMEFLAG(1)|TIMEFLAG(2)|TIMEFLAG(3)|TIMEFLAG(4));
6289 switch (TIMEFLAG(0)|TIMEFLAG(1)|TIMEFLAG(2)|TIMEFLAG(3)|TIMEFLAG(4))
6291 case 0x1: /* TT TTDD TTDDD */
6292 if (dp.dwCount > 3 &&
6293 ((dp.dwFlags[2] & (DP_AM|DP_PM)) || (dp.dwFlags[3] & (DP_AM|DP_PM)) ||
6294 (dp.dwFlags[4] & (DP_AM|DP_PM))))
6295 hRet = DISP_E_TYPEMISMATCH;
6296 else if (dp.dwCount != 2 && dp.dwCount != 4 && dp.dwCount != 5)
6297 hRet = DISP_E_TYPEMISMATCH;
6298 st.wHour = dp.dwValues[0];
6299 st.wMinute = dp.dwValues[1];
6304 case 0x3: /* TTT TTTDD TTTDDD */
6305 if (dp.dwCount > 4 &&
6306 ((dp.dwFlags[3] & (DP_AM|DP_PM)) || (dp.dwFlags[4] & (DP_AM|DP_PM)) ||
6307 (dp.dwFlags[5] & (DP_AM|DP_PM))))
6308 hRet = DISP_E_TYPEMISMATCH;
6309 else if (dp.dwCount != 3 && dp.dwCount != 5 && dp.dwCount != 6)
6310 hRet = DISP_E_TYPEMISMATCH;
6311 st.wHour = dp.dwValues[0];
6312 st.wMinute = dp.dwValues[1];
6313 st.wSecond = dp.dwValues[2];
6318 case 0x4: /* DDTT */
6319 if (dp.dwCount != 4 ||
6320 (dp.dwFlags[0] & (DP_AM|DP_PM)) || (dp.dwFlags[1] & (DP_AM|DP_PM)))
6321 hRet = DISP_E_TYPEMISMATCH;
6323 st.wHour = dp.dwValues[2];
6324 st.wMinute = dp.dwValues[3];
6328 case 0x0: /* T DD DDD TDDD TDDD */
6329 if (dp.dwCount == 1 && (dp.dwParseFlags & (DP_AM|DP_PM)))
6331 st.wHour = dp.dwValues[0]; /* T */
6335 else if (dp.dwCount > 4 || (dp.dwCount < 3 && dp.dwParseFlags & (DP_AM|DP_PM)))
6337 hRet = DISP_E_TYPEMISMATCH;
6339 else if (dp.dwCount == 3)
6341 if (dp.dwFlags[0] & (DP_AM|DP_PM)) /* TDD */
6344 st.wHour = dp.dwValues[0];
6348 if (dp.dwFlags[2] & (DP_AM|DP_PM)) /* DDT */
6351 st.wHour = dp.dwValues[2];
6354 else if (dp.dwParseFlags & (DP_AM|DP_PM))
6355 hRet = DISP_E_TYPEMISMATCH;
6357 else if (dp.dwCount == 4)
6360 if (dp.dwFlags[0] & (DP_AM|DP_PM)) /* TDDD */
6362 st.wHour = dp.dwValues[0];
6365 else if (dp.dwFlags[3] & (DP_AM|DP_PM)) /* DDDT */
6367 st.wHour = dp.dwValues[3];
6370 hRet = DISP_E_TYPEMISMATCH;
6373 /* .. fall through .. */
6375 case 0x8: /* DDDTT */
6376 if ((dp.dwCount == 2 && (dp.dwParseFlags & (DP_AM|DP_PM))) ||
6377 (dp.dwCount == 5 && ((dp.dwFlags[0] & (DP_AM|DP_PM)) ||
6378 (dp.dwFlags[1] & (DP_AM|DP_PM)) || (dp.dwFlags[2] & (DP_AM|DP_PM)))) ||
6379 dp.dwCount == 4 || dp.dwCount == 6)
6380 hRet = DISP_E_TYPEMISMATCH;
6381 st.wHour = dp.dwValues[3];
6382 st.wMinute = dp.dwValues[4];
6383 if (dp.dwCount == 5)
6387 case 0xC: /* DDTTT */
6388 if (dp.dwCount != 5 ||
6389 (dp.dwFlags[0] & (DP_AM|DP_PM)) || (dp.dwFlags[1] & (DP_AM|DP_PM)))
6390 hRet = DISP_E_TYPEMISMATCH;
6391 st.wHour = dp.dwValues[2];
6392 st.wMinute = dp.dwValues[3];
6393 st.wSecond = dp.dwValues[4];
6397 case 0x18: /* DDDTTT */
6398 if ((dp.dwFlags[0] & (DP_AM|DP_PM)) || (dp.dwFlags[1] & (DP_AM|DP_PM)) ||
6399 (dp.dwFlags[2] & (DP_AM|DP_PM)))
6400 hRet = DISP_E_TYPEMISMATCH;
6401 st.wHour = dp.dwValues[3];
6402 st.wMinute = dp.dwValues[4];
6403 st.wSecond = dp.dwValues[5];
6408 hRet = DISP_E_TYPEMISMATCH;
6412 if (SUCCEEDED(hRet))
6414 hRet = VARIANT_MakeDate(&dp, iDate, dwOffset, &st);
6416 if (dwFlags & VAR_TIMEVALUEONLY)
6422 else if (dwFlags & VAR_DATEVALUEONLY)
6423 st.wHour = st.wMinute = st.wSecond = 0;
6425 /* Finally, convert the value to a VT_DATE */
6426 if (SUCCEEDED(hRet))
6427 hRet = SystemTimeToVariantTime(&st, pdateOut) ? S_OK : DISP_E_TYPEMISMATCH;
6431 for (i = 0; i < sizeof(tokens)/sizeof(tokens[0]); i++)
6432 SysFreeString(tokens[i]);
6436 /******************************************************************************
6437 * VarDateFromI1 (OLEAUT32.221)
6439 * Convert a VT_I1 to a VT_DATE.
6443 * pdateOut [O] Destination
6448 HRESULT WINAPI VarDateFromI1(signed char cIn, DATE* pdateOut)
6450 return _VarDateFromI1(cIn, pdateOut);
6453 /******************************************************************************
6454 * VarDateFromUI2 (OLEAUT32.222)
6456 * Convert a VT_UI2 to a VT_DATE.
6460 * pdateOut [O] Destination
6465 HRESULT WINAPI VarDateFromUI2(USHORT uiIn, DATE* pdateOut)
6467 return _VarDateFromUI2(uiIn, pdateOut);
6470 /******************************************************************************
6471 * VarDateFromUI4 (OLEAUT32.223)
6473 * Convert a VT_UI4 to a VT_DATE.
6477 * pdateOut [O] Destination
6482 HRESULT WINAPI VarDateFromUI4(ULONG ulIn, DATE* pdateOut)
6484 return _VarDateFromUI4(ulIn, pdateOut);
6487 /**********************************************************************
6488 * VarDateFromDec (OLEAUT32.224)
6490 * Convert a VT_DECIMAL to a VT_DATE.
6494 * pdateOut [O] Destination
6499 HRESULT WINAPI VarDateFromDec(DECIMAL *pdecIn, DATE* pdateOut)
6501 return _VarDateFromDec(pdecIn, pdateOut);
6504 /******************************************************************************
6505 * VarDateFromI8 (OLEAUT32.364)
6507 * Convert a VT_I8 to a VT_DATE.
6511 * pdateOut [O] Destination
6515 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
6517 HRESULT WINAPI VarDateFromI8(LONG64 llIn, DATE* pdateOut)
6519 return _VarDateFromI8(llIn, pdateOut);
6522 /******************************************************************************
6523 * VarDateFromUI8 (OLEAUT32.365)
6525 * Convert a VT_UI8 to a VT_DATE.
6529 * pdateOut [O] Destination
6533 * Failure: DISP_E_OVERFLOW, if the value will not fit in the destination
6535 HRESULT WINAPI VarDateFromUI8(ULONG64 ullIn, DATE* pdateOut)
6537 return _VarDateFromUI8(ullIn, pdateOut);