2 * PropVariant implementation
4 * Copyright 2008 James Hawkins for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #define NONAMELESSUNION
34 #include "propvarutil.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(propsys
);
41 static HRESULT
PROPVAR_ConvertFILETIME(const FILETIME
*ft
, PROPVARIANT
*ppropvarDest
, VARTYPE vt
)
45 FileTimeToSystemTime(ft
, &time
);
50 ppropvarDest
->u
.pszVal
= HeapAlloc(GetProcessHeap(), 0, 64);
51 if (!ppropvarDest
->u
.pszVal
)
54 sprintf( ppropvarDest
->u
.pszVal
, "%04d/%02d/%02d:%02d:%02d:%02d.%03d",
55 time
.wYear
, time
.wMonth
, time
.wDay
,
56 time
.wHour
, time
.wMinute
, time
.wSecond
,
62 FIXME("Unhandled target type: %d\n", vt
);
68 static HRESULT
PROPVAR_ConvertNumber(REFPROPVARIANT pv
, int dest_bits
,
69 BOOL dest_signed
, LONGLONG
*res
)
101 *res
= pv
->u
.hVal
.QuadPart
;
105 *res
= pv
->u
.uhVal
.QuadPart
;
114 *res
= strtoll(pv
->u
.pszVal
, &end
, 0);
115 if (pv
->u
.pszVal
== end
)
116 return DISP_E_TYPEMISMATCH
;
117 src_signed
= *res
< 0;
124 *res
= wcstol(pv
->u
.pwszVal
, &end
, 0);
125 if (pv
->u
.pwszVal
== end
)
126 return DISP_E_TYPEMISMATCH
;
127 src_signed
= *res
< 0;
137 FIXME("unhandled vt %d\n", pv
->vt
);
141 if (*res
< 0 && src_signed
!= dest_signed
)
142 return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW
);
148 if (*res
>= ((LONGLONG
)1 << (dest_bits
-1)) ||
149 *res
< ((LONGLONG
)-1 << (dest_bits
-1)))
150 return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW
);
154 if ((ULONGLONG
)(*res
) >= ((ULONGLONG
)1 << dest_bits
))
155 return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW
);
162 HRESULT WINAPI
PropVariantToDouble(REFPROPVARIANT propvarIn
, double *ret
)
167 TRACE("(%p, %p)\n", propvarIn
, ret
);
169 hr
= PROPVAR_ConvertNumber(propvarIn
, 64, TRUE
, &res
);
170 if (SUCCEEDED(hr
)) *ret
= (double)res
;
174 HRESULT WINAPI
PropVariantToInt16(REFPROPVARIANT propvarIn
, SHORT
*ret
)
179 TRACE("%p,%p\n", propvarIn
, ret
);
181 hr
= PROPVAR_ConvertNumber(propvarIn
, 16, TRUE
, &res
);
182 if (SUCCEEDED(hr
)) *ret
= (SHORT
)res
;
186 HRESULT WINAPI
PropVariantToInt32(REFPROPVARIANT propvarIn
, LONG
*ret
)
191 TRACE("%p,%p\n", propvarIn
, ret
);
193 hr
= PROPVAR_ConvertNumber(propvarIn
, 32, TRUE
, &res
);
194 if (SUCCEEDED(hr
)) *ret
= (LONG
)res
;
198 HRESULT WINAPI
PropVariantToInt64(REFPROPVARIANT propvarIn
, LONGLONG
*ret
)
203 TRACE("%p,%p\n", propvarIn
, ret
);
205 hr
= PROPVAR_ConvertNumber(propvarIn
, 64, TRUE
, &res
);
206 if (SUCCEEDED(hr
)) *ret
= res
;
210 HRESULT WINAPI
PropVariantToUInt16(REFPROPVARIANT propvarIn
, USHORT
*ret
)
215 TRACE("%p,%p\n", propvarIn
, ret
);
217 hr
= PROPVAR_ConvertNumber(propvarIn
, 16, FALSE
, &res
);
218 if (SUCCEEDED(hr
)) *ret
= (USHORT
)res
;
222 HRESULT WINAPI
PropVariantToUInt32(REFPROPVARIANT propvarIn
, ULONG
*ret
)
227 TRACE("%p,%p\n", propvarIn
, ret
);
229 hr
= PROPVAR_ConvertNumber(propvarIn
, 32, FALSE
, &res
);
230 if (SUCCEEDED(hr
)) *ret
= (ULONG
)res
;
234 HRESULT WINAPI
PropVariantToUInt64(REFPROPVARIANT propvarIn
, ULONGLONG
*ret
)
239 TRACE("%p,%p\n", propvarIn
, ret
);
241 hr
= PROPVAR_ConvertNumber(propvarIn
, 64, FALSE
, &res
);
242 if (SUCCEEDED(hr
)) *ret
= (ULONGLONG
)res
;
246 HRESULT WINAPI
PropVariantToBoolean(REFPROPVARIANT propvarIn
, BOOL
*ret
)
248 static const WCHAR trueW
[] = {'t','r','u','e',0};
249 static const WCHAR falseW
[] = {'f','a','l','s','e',0};
250 static const WCHAR true2W
[] = {'#','T','R','U','E','#',0};
251 static const WCHAR false2W
[] = {'#','F','A','L','S','E','#',0};
255 TRACE("%p,%p\n", propvarIn
, ret
);
259 switch (propvarIn
->vt
)
262 *ret
= propvarIn
->u
.boolVal
== VARIANT_TRUE
;
267 if (!propvarIn
->u
.pwszVal
)
268 return DISP_E_TYPEMISMATCH
;
270 if (!lstrcmpiW(propvarIn
->u
.pwszVal
, trueW
) || !lstrcmpW(propvarIn
->u
.pwszVal
, true2W
))
276 if (!lstrcmpiW(propvarIn
->u
.pwszVal
, falseW
) || !lstrcmpW(propvarIn
->u
.pwszVal
, false2W
))
284 if (!propvarIn
->u
.pszVal
)
285 return DISP_E_TYPEMISMATCH
;
287 if (!lstrcmpiA(propvarIn
->u
.pszVal
, "true") || !lstrcmpA(propvarIn
->u
.pszVal
, "#TRUE#"))
293 if (!lstrcmpiA(propvarIn
->u
.pszVal
, "false") || !lstrcmpA(propvarIn
->u
.pszVal
, "#FALSE#"))
301 hr
= PROPVAR_ConvertNumber(propvarIn
, 64, TRUE
, &res
);
306 HRESULT WINAPI
PropVariantToBuffer(REFPROPVARIANT propvarIn
, void *ret
, UINT cb
)
310 TRACE("(%p, %p, %d)\n", propvarIn
, ret
, cb
);
312 switch(propvarIn
->vt
)
314 case VT_VECTOR
|VT_UI1
:
315 if(cb
> propvarIn
->u
.caub
.cElems
)
317 memcpy(ret
, propvarIn
->u
.caub
.pElems
, cb
);
319 case VT_ARRAY
|VT_UI1
:
320 FIXME("Unsupported type: VT_ARRAY|VT_UI1\n");
324 WARN("Unexpected type: %x\n", propvarIn
->vt
);
332 HRESULT WINAPI
PropVariantToString(REFPROPVARIANT propvarIn
, PWSTR ret
, UINT cch
)
335 WCHAR
*stringW
= NULL
;
337 TRACE("(%p, %p, %d)\n", propvarIn
, ret
, cch
);
344 hr
= PropVariantToStringAlloc(propvarIn
, &stringW
);
347 if(lstrlenW(stringW
) >= cch
)
348 hr
= STRSAFE_E_INSUFFICIENT_BUFFER
;
349 lstrcpynW(ret
, stringW
, cch
);
350 CoTaskMemFree(stringW
);
356 HRESULT WINAPI
PropVariantToStringAlloc(REFPROPVARIANT propvarIn
, WCHAR
**ret
)
361 TRACE("%p,%p semi-stub\n", propvarIn
, ret
);
363 switch(propvarIn
->vt
)
367 res
= CoTaskMemAlloc(1*sizeof(WCHAR
));
372 if(propvarIn
->u
.pszVal
)
376 len
= MultiByteToWideChar(CP_ACP
, 0, propvarIn
->u
.pszVal
, -1, NULL
, 0);
377 res
= CoTaskMemAlloc(len
*sizeof(WCHAR
));
379 return E_OUTOFMEMORY
;
381 MultiByteToWideChar(CP_ACP
, 0, propvarIn
->u
.pszVal
, -1, res
, len
);
387 if (propvarIn
->u
.pwszVal
)
389 DWORD size
= (lstrlenW(propvarIn
->u
.pwszVal
) + 1) * sizeof(WCHAR
);
390 res
= CoTaskMemAlloc(size
);
391 if(!res
) return E_OUTOFMEMORY
;
392 memcpy(res
, propvarIn
->u
.pwszVal
, size
);
397 FIXME("Unsupported conversion (%d)\n", propvarIn
->vt
);
407 PCWSTR WINAPI
PropVariantToStringWithDefault(REFPROPVARIANT propvarIn
, LPCWSTR pszDefault
)
409 static const WCHAR str_empty
[] = {0};
410 if (propvarIn
->vt
== VT_BSTR
)
412 if (propvarIn
->u
.bstrVal
== NULL
)
415 return propvarIn
->u
.bstrVal
;
418 if (propvarIn
->vt
== VT_LPWSTR
&& propvarIn
->u
.pwszVal
!= NULL
)
419 return propvarIn
->u
.pwszVal
;
425 /******************************************************************
426 * PropVariantChangeType (PROPSYS.@)
428 HRESULT WINAPI
PropVariantChangeType(PROPVARIANT
*ppropvarDest
, REFPROPVARIANT propvarSrc
,
429 PROPVAR_CHANGE_FLAGS flags
, VARTYPE vt
)
433 FIXME("(%p, %p, %d, %d, %d): semi-stub!\n", ppropvarDest
, propvarSrc
,
434 propvarSrc
->vt
, flags
, vt
);
436 if (vt
== propvarSrc
->vt
)
437 return PropVariantCopy(ppropvarDest
, propvarSrc
);
439 if (propvarSrc
->vt
== VT_FILETIME
)
440 return PROPVAR_ConvertFILETIME(&propvarSrc
->u
.filetime
, ppropvarDest
, vt
);
448 hr
= PROPVAR_ConvertNumber(propvarSrc
, 8, TRUE
, &res
);
451 ppropvarDest
->vt
= VT_I1
;
452 ppropvarDest
->u
.cVal
= (char)res
;
461 hr
= PROPVAR_ConvertNumber(propvarSrc
, 8, FALSE
, &res
);
464 ppropvarDest
->vt
= VT_UI1
;
465 ppropvarDest
->u
.bVal
= (UCHAR
)res
;
473 hr
= PropVariantToInt16(propvarSrc
, &res
);
476 ppropvarDest
->vt
= VT_I2
;
477 ppropvarDest
->u
.iVal
= res
;
484 hr
= PropVariantToUInt16(propvarSrc
, &res
);
487 ppropvarDest
->vt
= VT_UI2
;
488 ppropvarDest
->u
.uiVal
= res
;
495 hr
= PropVariantToInt32(propvarSrc
, &res
);
498 ppropvarDest
->vt
= VT_I4
;
499 ppropvarDest
->u
.lVal
= res
;
506 hr
= PropVariantToUInt32(propvarSrc
, &res
);
509 ppropvarDest
->vt
= VT_UI4
;
510 ppropvarDest
->u
.ulVal
= res
;
517 hr
= PropVariantToInt64(propvarSrc
, &res
);
520 ppropvarDest
->vt
= VT_I8
;
521 ppropvarDest
->u
.hVal
.QuadPart
= res
;
528 hr
= PropVariantToUInt64(propvarSrc
, &res
);
531 ppropvarDest
->vt
= VT_UI8
;
532 ppropvarDest
->u
.uhVal
.QuadPart
= res
;
541 hr
= PropVariantToStringAlloc(propvarSrc
, &res
);
544 ppropvarDest
->vt
= VT_LPWSTR
;
545 ppropvarDest
->u
.pwszVal
= res
;
553 hr
= PropVariantToStringAlloc(propvarSrc
, &resW
);
559 len
= WideCharToMultiByte(CP_ACP
, 0, resW
, -1, NULL
, 0, NULL
, NULL
);
560 res
= CoTaskMemAlloc(len
);
563 WideCharToMultiByte(CP_ACP
, 0, resW
, -1, res
, len
, NULL
, NULL
);
564 ppropvarDest
->vt
= VT_LPSTR
;
565 ppropvarDest
->u
.pszVal
= res
;
576 FIXME("Unhandled dest type: %d\n", vt
);
581 static void PROPVAR_GUIDToWSTR(REFGUID guid
, WCHAR
*str
)
583 static const WCHAR format
[] = {'{','%','0','8','X','-','%','0','4','X','-','%','0','4','X',
584 '-','%','0','2','X','%','0','2','X','-','%','0','2','X','%','0','2','X','%','0','2','X',
585 '%','0','2','X','%','0','2','X','%','0','2','X','}',0};
587 swprintf(str
, 39, format
, guid
->Data1
, guid
->Data2
, guid
->Data3
,
588 guid
->Data4
[0], guid
->Data4
[1], guid
->Data4
[2], guid
->Data4
[3],
589 guid
->Data4
[4], guid
->Data4
[5], guid
->Data4
[6], guid
->Data4
[7]);
592 HRESULT WINAPI
InitPropVariantFromGUIDAsString(REFGUID guid
, PROPVARIANT
*ppropvar
)
594 TRACE("(%p %p)\n", guid
, ppropvar
);
599 ppropvar
->vt
= VT_LPWSTR
;
600 ppropvar
->u
.pwszVal
= CoTaskMemAlloc(39*sizeof(WCHAR
));
601 if(!ppropvar
->u
.pwszVal
)
602 return E_OUTOFMEMORY
;
604 PROPVAR_GUIDToWSTR(guid
, ppropvar
->u
.pwszVal
);
608 HRESULT WINAPI
InitVariantFromGUIDAsString(REFGUID guid
, VARIANT
*pvar
)
610 TRACE("(%p %p)\n", guid
, pvar
);
613 FIXME("guid == NULL\n");
617 V_VT(pvar
) = VT_BSTR
;
618 V_BSTR(pvar
) = SysAllocStringLen(NULL
, 38);
620 return E_OUTOFMEMORY
;
622 PROPVAR_GUIDToWSTR(guid
, V_BSTR(pvar
));
626 HRESULT WINAPI
InitPropVariantFromBuffer(const VOID
*pv
, UINT cb
, PROPVARIANT
*ppropvar
)
628 TRACE("(%p %u %p)\n", pv
, cb
, ppropvar
);
630 ppropvar
->u
.caub
.pElems
= CoTaskMemAlloc(cb
);
631 if(!ppropvar
->u
.caub
.pElems
)
632 return E_OUTOFMEMORY
;
634 ppropvar
->vt
= VT_VECTOR
|VT_UI1
;
635 ppropvar
->u
.caub
.cElems
= cb
;
636 memcpy(ppropvar
->u
.caub
.pElems
, pv
, cb
);
640 HRESULT WINAPI
InitPropVariantFromCLSID(REFCLSID clsid
, PROPVARIANT
*ppropvar
)
642 TRACE("(%s %p)\n", debugstr_guid(clsid
), ppropvar
);
644 ppropvar
->u
.puuid
= CoTaskMemAlloc(sizeof(*ppropvar
->u
.puuid
));
645 if(!ppropvar
->u
.puuid
)
646 return E_OUTOFMEMORY
;
648 ppropvar
->vt
= VT_CLSID
;
649 memcpy(ppropvar
->u
.puuid
, clsid
, sizeof(*ppropvar
->u
.puuid
));
653 HRESULT WINAPI
InitVariantFromBuffer(const VOID
*pv
, UINT cb
, VARIANT
*pvar
)
659 TRACE("(%p %u %p)\n", pv
, cb
, pvar
);
661 arr
= SafeArrayCreateVector(VT_UI1
, 0, cb
);
663 return E_OUTOFMEMORY
;
665 hres
= SafeArrayAccessData(arr
, &data
);
667 SafeArrayDestroy(arr
);
671 memcpy(data
, pv
, cb
);
673 hres
= SafeArrayUnaccessData(arr
);
675 SafeArrayDestroy(arr
);
679 V_VT(pvar
) = VT_ARRAY
|VT_UI1
;
684 static inline DWORD
PROPVAR_HexToNum(const WCHAR
*hex
)
688 if(hex
[0]>='0' && hex
[0]<='9')
690 else if(hex
[0]>='a' && hex
[0]<='f')
692 else if(hex
[0]>='A' && hex
[0]<='F')
698 if(hex
[1]>='0' && hex
[1]<='9')
699 return ret
+ hex
[1]-'0';
700 else if(hex
[1]>='a' && hex
[1]<='f')
701 return ret
+ hex
[1]-'a'+10;
702 else if(hex
[1]>='A' && hex
[1]<='F')
703 return ret
+ hex
[1]-'A'+10;
708 static inline HRESULT
PROPVAR_WCHARToGUID(const WCHAR
*str
, int len
, GUID
*guid
)
713 memset(guid
, 0, sizeof(GUID
));
715 if(len
!=38 || str
[0]!='{' || str
[9]!='-' || str
[14]!='-'
716 || str
[19]!='-' || str
[24]!='-' || str
[37]!='}') {
717 WARN("Error parsing %s\n", debugstr_w(str
));
722 for(i
=0; i
<4 && val
!=-1; i
++) {
723 val
= PROPVAR_HexToNum(p
);
724 guid
->Data1
= (guid
->Data1
<<8) + val
;
728 for(i
=0; i
<2 && val
!=-1; i
++) {
729 val
= PROPVAR_HexToNum(p
);
730 guid
->Data2
= (guid
->Data2
<<8) + val
;
734 for(i
=0; i
<2 && val
!=-1; i
++) {
735 val
= PROPVAR_HexToNum(p
);
736 guid
->Data3
= (guid
->Data3
<<8) + val
;
740 for(i
=0; i
<8 && val
!=-1; i
++) {
744 val
= guid
->Data4
[i
] = PROPVAR_HexToNum(p
);
749 WARN("Error parsing %s\n", debugstr_w(str
));
750 memset(guid
, 0, sizeof(GUID
));
756 HRESULT WINAPI
PropVariantToGUID(const PROPVARIANT
*ppropvar
, GUID
*guid
)
758 TRACE("%p %p)\n", ppropvar
, guid
);
760 switch(ppropvar
->vt
) {
762 return PROPVAR_WCHARToGUID(ppropvar
->u
.bstrVal
, SysStringLen(ppropvar
->u
.bstrVal
), guid
);
764 return PROPVAR_WCHARToGUID(ppropvar
->u
.pwszVal
, lstrlenW(ppropvar
->u
.pwszVal
), guid
);
766 memcpy(guid
, ppropvar
->u
.puuid
, sizeof(*ppropvar
->u
.puuid
));
770 FIXME("unsupported vt: %d\n", ppropvar
->vt
);
775 HRESULT WINAPI
VariantToGUID(const VARIANT
*pvar
, GUID
*guid
)
777 TRACE("(%p %p)\n", pvar
, guid
);
781 HRESULT hres
= PROPVAR_WCHARToGUID(V_BSTR(pvar
), SysStringLen(V_BSTR(pvar
)), guid
);
782 if(hres
== E_INVALIDARG
)
788 FIXME("unsupported vt: %d\n", V_VT(pvar
));
793 static BOOL
isemptyornull(const PROPVARIANT
*propvar
)
795 if (propvar
->vt
== VT_EMPTY
|| propvar
->vt
== VT_NULL
)
797 if ((propvar
->vt
& VT_ARRAY
) == VT_ARRAY
)
800 for (i
=0; i
<propvar
->u
.parray
->cDims
; i
++)
802 if (propvar
->u
.parray
->rgsabound
[i
].cElements
!= 0)
805 return i
== propvar
->u
.parray
->cDims
;
807 if (propvar
->vt
== VT_CLSID
)
808 return !propvar
->u
.puuid
;
810 /* FIXME: vectors, byrefs, errors? */
814 INT WINAPI
PropVariantCompareEx(REFPROPVARIANT propvar1
, REFPROPVARIANT propvar2
,
815 PROPVAR_COMPARE_UNIT unit
, PROPVAR_COMPARE_FLAGS flags
)
817 const PROPVARIANT
*propvar2_converted
;
818 PROPVARIANT propvar2_static
;
822 TRACE("%p,%p,%x,%x\n", propvar1
, propvar2
, unit
, flags
);
824 if (isemptyornull(propvar1
))
826 if (isemptyornull(propvar2
))
828 return (flags
& PVCF_TREATEMPTYASGREATERTHAN
) ? 1 : -1;
831 if (isemptyornull(propvar2
))
832 return (flags
& PVCF_TREATEMPTYASGREATERTHAN
) ? -1 : 1;
834 if (propvar1
->vt
!= propvar2
->vt
)
836 hr
= PropVariantChangeType(&propvar2_static
, propvar2
, 0, propvar1
->vt
);
841 propvar2_converted
= &propvar2_static
;
844 propvar2_converted
= propvar2
;
846 #define CMP_NUM_VALUE(var) do { \
847 if (propvar1->u.var > propvar2_converted->u.var) \
849 else if (propvar1->u.var < propvar2_converted->u.var) \
855 switch (propvar1
->vt
)
867 CMP_NUM_VALUE(uiVal
);
873 CMP_NUM_VALUE(uiVal
);
876 CMP_NUM_VALUE(hVal
.QuadPart
);
879 CMP_NUM_VALUE(uhVal
.QuadPart
);
882 CMP_NUM_VALUE(fltVal
);
885 CMP_NUM_VALUE(dblVal
);
889 /* FIXME: Use other string flags. */
890 if (flags
& (PVCF_USESTRCMPI
| PVCF_USESTRCMPIC
))
891 res
= lstrcmpiW(propvar1
->u
.bstrVal
, propvar2_converted
->u
.bstrVal
);
893 res
= lstrcmpW(propvar1
->u
.bstrVal
, propvar2_converted
->u
.bstrVal
);
896 /* FIXME: Use other string flags. */
897 if (flags
& (PVCF_USESTRCMPI
| PVCF_USESTRCMPIC
))
898 res
= lstrcmpiA(propvar1
->u
.pszVal
, propvar2_converted
->u
.pszVal
);
900 res
= lstrcmpA(propvar1
->u
.pszVal
, propvar2_converted
->u
.pszVal
);
903 res
= memcmp(propvar1
->u
.puuid
, propvar2
->u
.puuid
, sizeof(*propvar1
->u
.puuid
));
904 if (res
) res
= res
> 0 ? 1 : -1;
907 FIXME("vartype %#x not handled\n", propvar1
->vt
);
912 if (propvar2_converted
== &propvar2_static
)
913 PropVariantClear(&propvar2_static
);