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
->pszVal
= HeapAlloc(GetProcessHeap(), 0, 64);
51 if (!ppropvarDest
->pszVal
)
54 sprintf( ppropvarDest
->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
->hVal
.QuadPart
;
105 *res
= pv
->uhVal
.QuadPart
;
114 *res
= strtoll(pv
->pszVal
, &end
, 0);
115 if (pv
->pszVal
== end
)
116 return DISP_E_TYPEMISMATCH
;
117 src_signed
= *res
< 0;
124 *res
= wcstol(pv
->pwszVal
, &end
, 0);
125 if (pv
->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
)
251 TRACE("%p,%p\n", propvarIn
, ret
);
255 switch (propvarIn
->vt
)
258 *ret
= propvarIn
->boolVal
== VARIANT_TRUE
;
263 if (!propvarIn
->pwszVal
)
264 return DISP_E_TYPEMISMATCH
;
266 if (!lstrcmpiW(propvarIn
->pwszVal
, L
"true") || !lstrcmpW(propvarIn
->pwszVal
, L
"#TRUE#"))
272 if (!lstrcmpiW(propvarIn
->pwszVal
, L
"false") || !lstrcmpW(propvarIn
->pwszVal
, L
"#FALSE#"))
280 if (!propvarIn
->pszVal
)
281 return DISP_E_TYPEMISMATCH
;
283 if (!lstrcmpiA(propvarIn
->pszVal
, "true") || !lstrcmpA(propvarIn
->pszVal
, "#TRUE#"))
289 if (!lstrcmpiA(propvarIn
->pszVal
, "false") || !lstrcmpA(propvarIn
->pszVal
, "#FALSE#"))
297 hr
= PROPVAR_ConvertNumber(propvarIn
, 64, TRUE
, &res
);
302 HRESULT WINAPI
PropVariantToBuffer(REFPROPVARIANT propvarIn
, void *ret
, UINT cb
)
306 TRACE("(%p, %p, %d)\n", propvarIn
, ret
, cb
);
308 switch(propvarIn
->vt
)
310 case VT_VECTOR
|VT_UI1
:
311 if(cb
> propvarIn
->caub
.cElems
)
313 memcpy(ret
, propvarIn
->caub
.pElems
, cb
);
315 case VT_ARRAY
|VT_UI1
:
316 FIXME("Unsupported type: VT_ARRAY|VT_UI1\n");
320 WARN("Unexpected type: %x\n", propvarIn
->vt
);
328 HRESULT WINAPI
PropVariantToString(REFPROPVARIANT propvarIn
, PWSTR ret
, UINT cch
)
331 WCHAR
*stringW
= NULL
;
333 TRACE("(%p, %p, %d)\n", propvarIn
, ret
, cch
);
340 hr
= PropVariantToStringAlloc(propvarIn
, &stringW
);
343 if(lstrlenW(stringW
) >= cch
)
344 hr
= STRSAFE_E_INSUFFICIENT_BUFFER
;
345 lstrcpynW(ret
, stringW
, cch
);
346 CoTaskMemFree(stringW
);
352 HRESULT WINAPI
PropVariantToStringAlloc(REFPROPVARIANT propvarIn
, WCHAR
**ret
)
357 TRACE("%p,%p semi-stub\n", propvarIn
, ret
);
359 switch(propvarIn
->vt
)
363 res
= CoTaskMemAlloc(1*sizeof(WCHAR
));
368 if(propvarIn
->pszVal
)
372 len
= MultiByteToWideChar(CP_ACP
, 0, propvarIn
->pszVal
, -1, NULL
, 0);
373 res
= CoTaskMemAlloc(len
*sizeof(WCHAR
));
375 return E_OUTOFMEMORY
;
377 MultiByteToWideChar(CP_ACP
, 0, propvarIn
->pszVal
, -1, res
, len
);
383 if (propvarIn
->pwszVal
)
385 DWORD size
= (lstrlenW(propvarIn
->pwszVal
) + 1) * sizeof(WCHAR
);
386 res
= CoTaskMemAlloc(size
);
387 if(!res
) return E_OUTOFMEMORY
;
388 memcpy(res
, propvarIn
->pwszVal
, size
);
393 FIXME("Unsupported conversion (%d)\n", propvarIn
->vt
);
403 PCWSTR WINAPI
PropVariantToStringWithDefault(REFPROPVARIANT propvarIn
, LPCWSTR pszDefault
)
405 if (propvarIn
->vt
== VT_BSTR
)
407 if (propvarIn
->bstrVal
== NULL
)
410 return propvarIn
->bstrVal
;
413 if (propvarIn
->vt
== VT_LPWSTR
&& propvarIn
->pwszVal
!= NULL
)
414 return propvarIn
->pwszVal
;
420 /******************************************************************
421 * PropVariantChangeType (PROPSYS.@)
423 HRESULT WINAPI
PropVariantChangeType(PROPVARIANT
*ppropvarDest
, REFPROPVARIANT propvarSrc
,
424 PROPVAR_CHANGE_FLAGS flags
, VARTYPE vt
)
428 FIXME("(%p, %p, %d, %d, %d): semi-stub!\n", ppropvarDest
, propvarSrc
,
429 propvarSrc
->vt
, flags
, vt
);
431 if (vt
== propvarSrc
->vt
)
432 return PropVariantCopy(ppropvarDest
, propvarSrc
);
434 if (propvarSrc
->vt
== VT_FILETIME
)
435 return PROPVAR_ConvertFILETIME(&propvarSrc
->filetime
, ppropvarDest
, vt
);
443 hr
= PROPVAR_ConvertNumber(propvarSrc
, 8, TRUE
, &res
);
446 ppropvarDest
->vt
= VT_I1
;
447 ppropvarDest
->cVal
= (char)res
;
456 hr
= PROPVAR_ConvertNumber(propvarSrc
, 8, FALSE
, &res
);
459 ppropvarDest
->vt
= VT_UI1
;
460 ppropvarDest
->bVal
= (UCHAR
)res
;
468 hr
= PropVariantToInt16(propvarSrc
, &res
);
471 ppropvarDest
->vt
= VT_I2
;
472 ppropvarDest
->iVal
= res
;
479 hr
= PropVariantToUInt16(propvarSrc
, &res
);
482 ppropvarDest
->vt
= VT_UI2
;
483 ppropvarDest
->uiVal
= res
;
490 hr
= PropVariantToInt32(propvarSrc
, &res
);
493 ppropvarDest
->vt
= VT_I4
;
494 ppropvarDest
->lVal
= res
;
501 hr
= PropVariantToUInt32(propvarSrc
, &res
);
504 ppropvarDest
->vt
= VT_UI4
;
505 ppropvarDest
->ulVal
= res
;
512 hr
= PropVariantToInt64(propvarSrc
, &res
);
515 ppropvarDest
->vt
= VT_I8
;
516 ppropvarDest
->hVal
.QuadPart
= res
;
523 hr
= PropVariantToUInt64(propvarSrc
, &res
);
526 ppropvarDest
->vt
= VT_UI8
;
527 ppropvarDest
->uhVal
.QuadPart
= res
;
536 hr
= PropVariantToStringAlloc(propvarSrc
, &res
);
539 ppropvarDest
->vt
= VT_LPWSTR
;
540 ppropvarDest
->pwszVal
= res
;
548 hr
= PropVariantToStringAlloc(propvarSrc
, &resW
);
554 len
= WideCharToMultiByte(CP_ACP
, 0, resW
, -1, NULL
, 0, NULL
, NULL
);
555 res
= CoTaskMemAlloc(len
);
558 WideCharToMultiByte(CP_ACP
, 0, resW
, -1, res
, len
, NULL
, NULL
);
559 ppropvarDest
->vt
= VT_LPSTR
;
560 ppropvarDest
->pszVal
= res
;
571 FIXME("Unhandled dest type: %d\n", vt
);
576 static void PROPVAR_GUIDToWSTR(REFGUID guid
, WCHAR
*str
)
578 swprintf(str
, 39, L
"{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}", guid
->Data1
,
579 guid
->Data2
, guid
->Data3
, guid
->Data4
[0], guid
->Data4
[1], guid
->Data4
[2],
580 guid
->Data4
[3], guid
->Data4
[4], guid
->Data4
[5], guid
->Data4
[6], guid
->Data4
[7]);
583 HRESULT WINAPI
InitPropVariantFromGUIDAsString(REFGUID guid
, PROPVARIANT
*ppropvar
)
585 TRACE("(%p %p)\n", guid
, ppropvar
);
590 ppropvar
->vt
= VT_LPWSTR
;
591 ppropvar
->pwszVal
= CoTaskMemAlloc(39*sizeof(WCHAR
));
592 if(!ppropvar
->pwszVal
)
593 return E_OUTOFMEMORY
;
595 PROPVAR_GUIDToWSTR(guid
, ppropvar
->pwszVal
);
599 HRESULT WINAPI
InitVariantFromGUIDAsString(REFGUID guid
, VARIANT
*pvar
)
601 TRACE("(%p %p)\n", guid
, pvar
);
604 FIXME("guid == NULL\n");
608 V_VT(pvar
) = VT_BSTR
;
609 V_BSTR(pvar
) = SysAllocStringLen(NULL
, 38);
611 return E_OUTOFMEMORY
;
613 PROPVAR_GUIDToWSTR(guid
, V_BSTR(pvar
));
617 HRESULT WINAPI
InitPropVariantFromBuffer(const VOID
*pv
, UINT cb
, PROPVARIANT
*ppropvar
)
619 TRACE("(%p %u %p)\n", pv
, cb
, ppropvar
);
621 ppropvar
->caub
.pElems
= CoTaskMemAlloc(cb
);
622 if(!ppropvar
->caub
.pElems
)
623 return E_OUTOFMEMORY
;
625 ppropvar
->vt
= VT_VECTOR
|VT_UI1
;
626 ppropvar
->caub
.cElems
= cb
;
627 memcpy(ppropvar
->caub
.pElems
, pv
, cb
);
631 HRESULT WINAPI
InitPropVariantFromCLSID(REFCLSID clsid
, PROPVARIANT
*ppropvar
)
633 TRACE("(%s %p)\n", debugstr_guid(clsid
), ppropvar
);
635 ppropvar
->puuid
= CoTaskMemAlloc(sizeof(*ppropvar
->puuid
));
637 return E_OUTOFMEMORY
;
639 ppropvar
->vt
= VT_CLSID
;
640 memcpy(ppropvar
->puuid
, clsid
, sizeof(*ppropvar
->puuid
));
644 HRESULT WINAPI
InitVariantFromBuffer(const VOID
*pv
, UINT cb
, VARIANT
*pvar
)
650 TRACE("(%p %u %p)\n", pv
, cb
, pvar
);
652 arr
= SafeArrayCreateVector(VT_UI1
, 0, cb
);
654 return E_OUTOFMEMORY
;
656 hres
= SafeArrayAccessData(arr
, &data
);
658 SafeArrayDestroy(arr
);
662 memcpy(data
, pv
, cb
);
664 hres
= SafeArrayUnaccessData(arr
);
666 SafeArrayDestroy(arr
);
670 V_VT(pvar
) = VT_ARRAY
|VT_UI1
;
675 static inline DWORD
PROPVAR_HexToNum(const WCHAR
*hex
)
679 if(hex
[0]>='0' && hex
[0]<='9')
681 else if(hex
[0]>='a' && hex
[0]<='f')
683 else if(hex
[0]>='A' && hex
[0]<='F')
689 if(hex
[1]>='0' && hex
[1]<='9')
690 return ret
+ hex
[1]-'0';
691 else if(hex
[1]>='a' && hex
[1]<='f')
692 return ret
+ hex
[1]-'a'+10;
693 else if(hex
[1]>='A' && hex
[1]<='F')
694 return ret
+ hex
[1]-'A'+10;
699 static inline HRESULT
PROPVAR_WCHARToGUID(const WCHAR
*str
, int len
, GUID
*guid
)
704 memset(guid
, 0, sizeof(GUID
));
706 if(len
!=38 || str
[0]!='{' || str
[9]!='-' || str
[14]!='-'
707 || str
[19]!='-' || str
[24]!='-' || str
[37]!='}') {
708 WARN("Error parsing %s\n", debugstr_w(str
));
713 for(i
=0; i
<4 && val
!=-1; i
++) {
714 val
= PROPVAR_HexToNum(p
);
715 guid
->Data1
= (guid
->Data1
<<8) + val
;
719 for(i
=0; i
<2 && val
!=-1; i
++) {
720 val
= PROPVAR_HexToNum(p
);
721 guid
->Data2
= (guid
->Data2
<<8) + val
;
725 for(i
=0; i
<2 && val
!=-1; i
++) {
726 val
= PROPVAR_HexToNum(p
);
727 guid
->Data3
= (guid
->Data3
<<8) + val
;
731 for(i
=0; i
<8 && val
!=-1; i
++) {
735 val
= guid
->Data4
[i
] = PROPVAR_HexToNum(p
);
740 WARN("Error parsing %s\n", debugstr_w(str
));
741 memset(guid
, 0, sizeof(GUID
));
747 HRESULT WINAPI
PropVariantToGUID(const PROPVARIANT
*ppropvar
, GUID
*guid
)
749 TRACE("%p %p)\n", ppropvar
, guid
);
751 switch(ppropvar
->vt
) {
753 return PROPVAR_WCHARToGUID(ppropvar
->bstrVal
, SysStringLen(ppropvar
->bstrVal
), guid
);
755 return PROPVAR_WCHARToGUID(ppropvar
->pwszVal
, lstrlenW(ppropvar
->pwszVal
), guid
);
757 memcpy(guid
, ppropvar
->puuid
, sizeof(*ppropvar
->puuid
));
761 FIXME("unsupported vt: %d\n", ppropvar
->vt
);
766 HRESULT WINAPI
VariantToGUID(const VARIANT
*pvar
, GUID
*guid
)
768 TRACE("(%p %p)\n", pvar
, guid
);
772 HRESULT hres
= PROPVAR_WCHARToGUID(V_BSTR(pvar
), SysStringLen(V_BSTR(pvar
)), guid
);
773 if(hres
== E_INVALIDARG
)
779 FIXME("unsupported vt: %d\n", V_VT(pvar
));
784 static BOOL
isemptyornull(const PROPVARIANT
*propvar
)
786 if (propvar
->vt
== VT_EMPTY
|| propvar
->vt
== VT_NULL
)
788 if ((propvar
->vt
& VT_ARRAY
) == VT_ARRAY
)
791 for (i
=0; i
<propvar
->parray
->cDims
; i
++)
793 if (propvar
->parray
->rgsabound
[i
].cElements
!= 0)
796 return i
== propvar
->parray
->cDims
;
798 if (propvar
->vt
== VT_CLSID
)
799 return !propvar
->puuid
;
801 /* FIXME: vectors, byrefs, errors? */
805 INT WINAPI
PropVariantCompareEx(REFPROPVARIANT propvar1
, REFPROPVARIANT propvar2
,
806 PROPVAR_COMPARE_UNIT unit
, PROPVAR_COMPARE_FLAGS flags
)
808 const PROPVARIANT
*propvar2_converted
;
809 PROPVARIANT propvar2_static
;
813 TRACE("%p,%p,%x,%x\n", propvar1
, propvar2
, unit
, flags
);
815 if (isemptyornull(propvar1
))
817 if (isemptyornull(propvar2
))
819 return (flags
& PVCF_TREATEMPTYASGREATERTHAN
) ? 1 : -1;
822 if (isemptyornull(propvar2
))
823 return (flags
& PVCF_TREATEMPTYASGREATERTHAN
) ? -1 : 1;
825 if (propvar1
->vt
!= propvar2
->vt
)
827 hr
= PropVariantChangeType(&propvar2_static
, propvar2
, 0, propvar1
->vt
);
832 propvar2_converted
= &propvar2_static
;
835 propvar2_converted
= propvar2
;
837 #define CMP_NUM_VALUE(var) do { \
838 if (propvar1->var > propvar2_converted->var) \
840 else if (propvar1->var < propvar2_converted->var) \
846 switch (propvar1
->vt
)
858 CMP_NUM_VALUE(uiVal
);
864 CMP_NUM_VALUE(uiVal
);
867 CMP_NUM_VALUE(hVal
.QuadPart
);
870 CMP_NUM_VALUE(uhVal
.QuadPart
);
873 CMP_NUM_VALUE(fltVal
);
876 CMP_NUM_VALUE(dblVal
);
880 /* FIXME: Use other string flags. */
881 if (flags
& (PVCF_USESTRCMPI
| PVCF_USESTRCMPIC
))
882 res
= lstrcmpiW(propvar1
->bstrVal
, propvar2_converted
->bstrVal
);
884 res
= lstrcmpW(propvar1
->bstrVal
, propvar2_converted
->bstrVal
);
887 /* FIXME: Use other string flags. */
888 if (flags
& (PVCF_USESTRCMPI
| PVCF_USESTRCMPIC
))
889 res
= lstrcmpiA(propvar1
->pszVal
, propvar2_converted
->pszVal
);
891 res
= lstrcmpA(propvar1
->pszVal
, propvar2_converted
->pszVal
);
894 res
= memcmp(propvar1
->puuid
, propvar2
->puuid
, sizeof(*propvar1
->puuid
));
895 if (res
) res
= res
> 0 ? 1 : -1;
898 FIXME("vartype %#x not handled\n", propvar1
->vt
);
903 if (propvar2_converted
== &propvar2_static
)
904 PropVariantClear(&propvar2_static
);