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
22 #include "wine/port.h"
28 #define NONAMELESSUNION
36 #include "propvarutil.h"
38 #include "wine/debug.h"
39 #include "wine/unicode.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(propsys
);
43 static HRESULT
PROPVAR_ConvertFILETIME(const FILETIME
*ft
, PROPVARIANT
*ppropvarDest
, VARTYPE vt
)
47 FileTimeToSystemTime(ft
, &time
);
52 ppropvarDest
->u
.pszVal
= HeapAlloc(GetProcessHeap(), 0, 64);
53 if (!ppropvarDest
->u
.pszVal
)
56 sprintf( ppropvarDest
->u
.pszVal
, "%04d/%02d/%02d:%02d:%02d:%02d.%03d",
57 time
.wYear
, time
.wMonth
, time
.wDay
,
58 time
.wHour
, time
.wMinute
, time
.wSecond
,
64 FIXME("Unhandled target type: %d\n", vt
);
70 static HRESULT
PROPVAR_ConvertNumber(REFPROPVARIANT pv
, int dest_bits
,
71 BOOL dest_signed
, LONGLONG
*res
)
103 *res
= pv
->u
.hVal
.QuadPart
;
107 *res
= pv
->u
.uhVal
.QuadPart
;
116 *res
= strtoll(pv
->u
.pszVal
, &end
, 0);
117 if (pv
->u
.pszVal
== end
)
118 return DISP_E_TYPEMISMATCH
;
119 src_signed
= *res
< 0;
126 *res
= strtolW(pv
->u
.pwszVal
, &end
, 0);
127 if (pv
->u
.pwszVal
== end
)
128 return DISP_E_TYPEMISMATCH
;
129 src_signed
= *res
< 0;
133 FIXME("unhandled vt %d\n", pv
->vt
);
137 if (*res
< 0 && src_signed
!= dest_signed
)
138 return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW
);
144 if (*res
>= ((LONGLONG
)1 << (dest_bits
-1)) ||
145 *res
< ((LONGLONG
)-1 << (dest_bits
-1)))
146 return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW
);
150 if ((ULONGLONG
)(*res
) >= ((ULONGLONG
)1 << dest_bits
))
151 return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW
);
158 HRESULT WINAPI
PropVariantToInt16(REFPROPVARIANT propvarIn
, SHORT
*ret
)
163 TRACE("%p,%p\n", propvarIn
, ret
);
165 hr
= PROPVAR_ConvertNumber(propvarIn
, 16, TRUE
, &res
);
166 if (SUCCEEDED(hr
)) *ret
= (SHORT
)res
;
170 HRESULT WINAPI
PropVariantToInt32(REFPROPVARIANT propvarIn
, LONG
*ret
)
175 TRACE("%p,%p\n", propvarIn
, ret
);
177 hr
= PROPVAR_ConvertNumber(propvarIn
, 32, TRUE
, &res
);
178 if (SUCCEEDED(hr
)) *ret
= (LONG
)res
;
182 HRESULT WINAPI
PropVariantToInt64(REFPROPVARIANT propvarIn
, LONGLONG
*ret
)
187 TRACE("%p,%p\n", propvarIn
, ret
);
189 hr
= PROPVAR_ConvertNumber(propvarIn
, 64, TRUE
, &res
);
190 if (SUCCEEDED(hr
)) *ret
= (LONGLONG
)res
;
194 HRESULT WINAPI
PropVariantToUInt16(REFPROPVARIANT propvarIn
, USHORT
*ret
)
199 TRACE("%p,%p\n", propvarIn
, ret
);
201 hr
= PROPVAR_ConvertNumber(propvarIn
, 16, FALSE
, &res
);
202 if (SUCCEEDED(hr
)) *ret
= (USHORT
)res
;
206 HRESULT WINAPI
PropVariantToUInt32(REFPROPVARIANT propvarIn
, ULONG
*ret
)
211 TRACE("%p,%p\n", propvarIn
, ret
);
213 hr
= PROPVAR_ConvertNumber(propvarIn
, 32, FALSE
, &res
);
214 if (SUCCEEDED(hr
)) *ret
= (ULONG
)res
;
218 HRESULT WINAPI
PropVariantToUInt64(REFPROPVARIANT propvarIn
, ULONGLONG
*ret
)
223 TRACE("%p,%p\n", propvarIn
, ret
);
225 hr
= PROPVAR_ConvertNumber(propvarIn
, 64, FALSE
, &res
);
226 if (SUCCEEDED(hr
)) *ret
= (ULONGLONG
)res
;
230 HRESULT WINAPI
PropVariantToBoolean(REFPROPVARIANT propvarIn
, BOOL
*ret
)
232 static const WCHAR trueW
[] = {'t','r','u','e',0};
233 static const WCHAR falseW
[] = {'f','a','l','s','e',0};
234 static const WCHAR true2W
[] = {'#','T','R','U','E','#',0};
235 static const WCHAR false2W
[] = {'#','F','A','L','S','E','#',0};
239 TRACE("%p,%p\n", propvarIn
, ret
);
243 switch (propvarIn
->vt
)
246 *ret
= propvarIn
->u
.boolVal
== VARIANT_TRUE
;
251 if (!propvarIn
->u
.pwszVal
)
252 return DISP_E_TYPEMISMATCH
;
254 if (!lstrcmpiW(propvarIn
->u
.pwszVal
, trueW
) || !lstrcmpW(propvarIn
->u
.pwszVal
, true2W
))
260 if (!lstrcmpiW(propvarIn
->u
.pwszVal
, falseW
) || !lstrcmpW(propvarIn
->u
.pwszVal
, false2W
))
268 if (!propvarIn
->u
.pszVal
)
269 return DISP_E_TYPEMISMATCH
;
271 if (!lstrcmpiA(propvarIn
->u
.pszVal
, "true") || !lstrcmpA(propvarIn
->u
.pszVal
, "#TRUE#"))
277 if (!lstrcmpiA(propvarIn
->u
.pszVal
, "false") || !lstrcmpA(propvarIn
->u
.pszVal
, "#FALSE#"))
285 hr
= PROPVAR_ConvertNumber(propvarIn
, 64, TRUE
, &res
);
290 HRESULT WINAPI
PropVariantToStringAlloc(REFPROPVARIANT propvarIn
, WCHAR
**ret
)
295 TRACE("%p,%p semi-stub\n", propvarIn
, ret
);
297 switch(propvarIn
->vt
)
300 res
= CoTaskMemAlloc(1*sizeof(WCHAR
));
305 if(propvarIn
->u
.pszVal
)
309 len
= MultiByteToWideChar(CP_ACP
, 0, propvarIn
->u
.pszVal
, -1, NULL
, 0);
310 res
= CoTaskMemAlloc(len
*sizeof(WCHAR
));
312 return E_OUTOFMEMORY
;
314 MultiByteToWideChar(CP_ACP
, 0, propvarIn
->u
.pszVal
, -1, res
, len
);
320 if (propvarIn
->u
.pwszVal
)
322 DWORD size
= (lstrlenW(propvarIn
->u
.pwszVal
) + 1) * sizeof(WCHAR
);
323 res
= CoTaskMemAlloc(size
);
324 if(!res
) return E_OUTOFMEMORY
;
325 memcpy(res
, propvarIn
->u
.pwszVal
, size
);
330 FIXME("Unsupported conversion (%d)\n", propvarIn
->vt
);
340 /******************************************************************
341 * PropVariantChangeType (PROPSYS.@)
343 HRESULT WINAPI
PropVariantChangeType(PROPVARIANT
*ppropvarDest
, REFPROPVARIANT propvarSrc
,
344 PROPVAR_CHANGE_FLAGS flags
, VARTYPE vt
)
348 FIXME("(%p, %p, %d, %d, %d): semi-stub!\n", ppropvarDest
, propvarSrc
,
349 propvarSrc
->vt
, flags
, vt
);
351 if (vt
== propvarSrc
->vt
)
352 return PropVariantCopy(ppropvarDest
, propvarSrc
);
354 if (propvarSrc
->vt
== VT_FILETIME
)
355 return PROPVAR_ConvertFILETIME(&propvarSrc
->u
.filetime
, ppropvarDest
, vt
);
363 hr
= PROPVAR_ConvertNumber(propvarSrc
, 8, TRUE
, &res
);
366 ppropvarDest
->vt
= VT_I1
;
367 ppropvarDest
->u
.cVal
= (char)res
;
376 hr
= PROPVAR_ConvertNumber(propvarSrc
, 8, FALSE
, &res
);
379 ppropvarDest
->vt
= VT_UI1
;
380 ppropvarDest
->u
.bVal
= (UCHAR
)res
;
388 hr
= PropVariantToInt16(propvarSrc
, &res
);
391 ppropvarDest
->vt
= VT_I2
;
392 ppropvarDest
->u
.iVal
= res
;
399 hr
= PropVariantToUInt16(propvarSrc
, &res
);
402 ppropvarDest
->vt
= VT_UI2
;
403 ppropvarDest
->u
.uiVal
= res
;
410 hr
= PropVariantToInt32(propvarSrc
, &res
);
413 ppropvarDest
->vt
= VT_I4
;
414 ppropvarDest
->u
.lVal
= res
;
421 hr
= PropVariantToUInt32(propvarSrc
, &res
);
424 ppropvarDest
->vt
= VT_UI4
;
425 ppropvarDest
->u
.ulVal
= res
;
432 hr
= PropVariantToInt64(propvarSrc
, &res
);
435 ppropvarDest
->vt
= VT_I8
;
436 ppropvarDest
->u
.hVal
.QuadPart
= res
;
443 hr
= PropVariantToUInt64(propvarSrc
, &res
);
446 ppropvarDest
->vt
= VT_UI8
;
447 ppropvarDest
->u
.uhVal
.QuadPart
= res
;
456 hr
= PropVariantToStringAlloc(propvarSrc
, &res
);
459 ppropvarDest
->vt
= VT_LPWSTR
;
460 ppropvarDest
->u
.pwszVal
= res
;
468 hr
= PropVariantToStringAlloc(propvarSrc
, &resW
);
474 len
= WideCharToMultiByte(CP_ACP
, 0, resW
, -1, NULL
, 0, NULL
, NULL
);
475 res
= CoTaskMemAlloc(len
);
478 WideCharToMultiByte(CP_ACP
, 0, resW
, -1, res
, len
, NULL
, NULL
);
479 ppropvarDest
->vt
= VT_LPSTR
;
480 ppropvarDest
->u
.pszVal
= res
;
491 FIXME("Unhandled dest type: %d\n", vt
);
496 static void PROPVAR_GUIDToWSTR(REFGUID guid
, WCHAR
*str
)
498 static const WCHAR format
[] = {'{','%','0','8','X','-','%','0','4','X','-','%','0','4','X',
499 '-','%','0','2','X','%','0','2','X','-','%','0','2','X','%','0','2','X','%','0','2','X',
500 '%','0','2','X','%','0','2','X','%','0','2','X','}',0};
502 sprintfW(str
, format
, guid
->Data1
, guid
->Data2
, guid
->Data3
,
503 guid
->Data4
[0], guid
->Data4
[1], guid
->Data4
[2], guid
->Data4
[3],
504 guid
->Data4
[4], guid
->Data4
[5], guid
->Data4
[6], guid
->Data4
[7]);
507 HRESULT WINAPI
InitPropVariantFromGUIDAsString(REFGUID guid
, PROPVARIANT
*ppropvar
)
509 TRACE("(%p %p)\n", guid
, ppropvar
);
514 ppropvar
->vt
= VT_LPWSTR
;
515 ppropvar
->u
.pwszVal
= CoTaskMemAlloc(39*sizeof(WCHAR
));
516 if(!ppropvar
->u
.pwszVal
)
517 return E_OUTOFMEMORY
;
519 PROPVAR_GUIDToWSTR(guid
, ppropvar
->u
.pwszVal
);
523 HRESULT WINAPI
InitVariantFromGUIDAsString(REFGUID guid
, VARIANT
*pvar
)
525 TRACE("(%p %p)\n", guid
, pvar
);
528 FIXME("guid == NULL\n");
532 V_VT(pvar
) = VT_BSTR
;
533 V_BSTR(pvar
) = SysAllocStringLen(NULL
, 38);
535 return E_OUTOFMEMORY
;
537 PROPVAR_GUIDToWSTR(guid
, V_BSTR(pvar
));
541 HRESULT WINAPI
InitPropVariantFromBuffer(const VOID
*pv
, UINT cb
, PROPVARIANT
*ppropvar
)
543 TRACE("(%p %u %p)\n", pv
, cb
, ppropvar
);
545 ppropvar
->u
.caub
.pElems
= CoTaskMemAlloc(cb
);
546 if(!ppropvar
->u
.caub
.pElems
)
547 return E_OUTOFMEMORY
;
549 ppropvar
->vt
= VT_VECTOR
|VT_UI1
;
550 ppropvar
->u
.caub
.cElems
= cb
;
551 memcpy(ppropvar
->u
.caub
.pElems
, pv
, cb
);
555 HRESULT WINAPI
InitVariantFromBuffer(const VOID
*pv
, UINT cb
, VARIANT
*pvar
)
561 TRACE("(%p %u %p)\n", pv
, cb
, pvar
);
563 arr
= SafeArrayCreateVector(VT_UI1
, 0, cb
);
565 return E_OUTOFMEMORY
;
567 hres
= SafeArrayAccessData(arr
, &data
);
569 SafeArrayDestroy(arr
);
573 memcpy(data
, pv
, cb
);
575 hres
= SafeArrayUnaccessData(arr
);
577 SafeArrayDestroy(arr
);
581 V_VT(pvar
) = VT_ARRAY
|VT_UI1
;
586 static inline DWORD
PROPVAR_HexToNum(const WCHAR
*hex
)
590 if(hex
[0]>='0' && hex
[0]<='9')
592 else if(hex
[0]>='a' && hex
[0]<='f')
594 else if(hex
[0]>='A' && hex
[0]<='F')
600 if(hex
[1]>='0' && hex
[1]<='9')
601 return ret
+ hex
[1]-'0';
602 else if(hex
[1]>='a' && hex
[1]<='f')
603 return ret
+ hex
[1]-'a'+10;
604 else if(hex
[1]>='A' && hex
[1]<='F')
605 return ret
+ hex
[1]-'A'+10;
610 static inline HRESULT
PROPVAR_WCHARToGUID(const WCHAR
*str
, int len
, GUID
*guid
)
615 memset(guid
, 0, sizeof(GUID
));
617 if(len
!=38 || str
[0]!='{' || str
[9]!='-' || str
[14]!='-'
618 || str
[19]!='-' || str
[24]!='-' || str
[37]!='}') {
619 WARN("Error parsing %s\n", debugstr_w(str
));
624 for(i
=0; i
<4 && val
!=-1; i
++) {
625 val
= PROPVAR_HexToNum(p
);
626 guid
->Data1
= (guid
->Data1
<<8) + val
;
630 for(i
=0; i
<2 && val
!=-1; i
++) {
631 val
= PROPVAR_HexToNum(p
);
632 guid
->Data2
= (guid
->Data2
<<8) + val
;
636 for(i
=0; i
<2 && val
!=-1; i
++) {
637 val
= PROPVAR_HexToNum(p
);
638 guid
->Data3
= (guid
->Data3
<<8) + val
;
642 for(i
=0; i
<8 && val
!=-1; i
++) {
646 val
= guid
->Data4
[i
] = PROPVAR_HexToNum(p
);
651 WARN("Error parsing %s\n", debugstr_w(str
));
652 memset(guid
, 0, sizeof(GUID
));
658 HRESULT WINAPI
PropVariantToGUID(const PROPVARIANT
*ppropvar
, GUID
*guid
)
660 TRACE("%p %p)\n", ppropvar
, guid
);
662 switch(ppropvar
->vt
) {
664 return PROPVAR_WCHARToGUID(ppropvar
->u
.bstrVal
, SysStringLen(ppropvar
->u
.bstrVal
), guid
);
666 return PROPVAR_WCHARToGUID(ppropvar
->u
.pwszVal
, strlenW(ppropvar
->u
.pwszVal
), guid
);
669 FIXME("unsupported vt: %d\n", ppropvar
->vt
);
674 HRESULT WINAPI
VariantToGUID(const VARIANT
*pvar
, GUID
*guid
)
676 TRACE("(%p %p)\n", pvar
, guid
);
680 HRESULT hres
= PROPVAR_WCHARToGUID(V_BSTR(pvar
), SysStringLen(V_BSTR(pvar
)), guid
);
681 if(hres
== E_INVALIDARG
)
687 FIXME("unsupported vt: %d\n", V_VT(pvar
));
692 static BOOL
isemptyornull(const PROPVARIANT
*propvar
)
694 if (propvar
->vt
== VT_EMPTY
|| propvar
->vt
== VT_NULL
)
696 if ((propvar
->vt
& VT_ARRAY
) == VT_ARRAY
)
699 for (i
=0; i
<propvar
->u
.parray
->cDims
; i
++)
701 if (propvar
->u
.parray
->rgsabound
[i
].cElements
!= 0)
704 return i
== propvar
->u
.parray
->cDims
;
706 /* FIXME: vectors, byrefs, errors? */
710 INT WINAPI
PropVariantCompareEx(REFPROPVARIANT propvar1
, REFPROPVARIANT propvar2
,
711 PROPVAR_COMPARE_UNIT unit
, PROPVAR_COMPARE_FLAGS flags
)
713 const PROPVARIANT
*propvar2_converted
;
714 PROPVARIANT propvar2_static
;
718 TRACE("%p,%p,%x,%x\n", propvar1
, propvar2
, unit
, flags
);
720 if (isemptyornull(propvar1
))
722 if (isemptyornull(propvar2
))
724 return (flags
& PVCF_TREATEMPTYASGREATERTHAN
) ? 1 : -1;
727 if (isemptyornull(propvar2
))
728 return (flags
& PVCF_TREATEMPTYASGREATERTHAN
) ? -1 : 1;
730 if (propvar1
->vt
!= propvar2
->vt
)
732 hr
= PropVariantChangeType(&propvar2_static
, propvar2
, 0, propvar1
->vt
);
737 propvar2_converted
= &propvar2_static
;
740 propvar2_converted
= propvar2
;
742 #define CMP_INT_VALUE(var) do { \
743 if (propvar1->u.var > propvar2_converted->u.var) \
745 else if (propvar1->u.var < propvar2_converted->u.var) \
751 switch (propvar1
->vt
)
763 CMP_INT_VALUE(uiVal
);
769 CMP_INT_VALUE(uiVal
);
772 CMP_INT_VALUE(hVal
.QuadPart
);
775 CMP_INT_VALUE(uhVal
.QuadPart
);
779 /* FIXME: Use other string flags. */
780 if (flags
& (PVCF_USESTRCMPI
| PVCF_USESTRCMPIC
))
781 res
= lstrcmpiW(propvar1
->u
.bstrVal
, propvar2_converted
->u
.bstrVal
);
783 res
= lstrcmpW(propvar1
->u
.bstrVal
, propvar2_converted
->u
.bstrVal
);
786 /* FIXME: Use other string flags. */
787 if (flags
& (PVCF_USESTRCMPI
| PVCF_USESTRCMPIC
))
788 res
= lstrcmpiA(propvar1
->u
.pszVal
, propvar2_converted
->u
.pszVal
);
790 res
= lstrcmpA(propvar1
->u
.pszVal
, propvar2_converted
->u
.pszVal
);
793 FIXME("vartype %d not handled\n", propvar1
->vt
);
798 if (propvar2_converted
== &propvar2_static
)
799 PropVariantClear(&propvar2_static
);