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
24 #define NONAMELESSUNION
32 #include "propvarutil.h"
34 #include "wine/debug.h"
35 #include "wine/unicode.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(propsys);
39 static HRESULT PROPVAR_ConvertFILETIME(PROPVARIANT *ppropvarDest,
40 REFPROPVARIANT propvarSrc, VARTYPE vt)
44 FileTimeToSystemTime(&propvarSrc->u.filetime, &time);
50 static const char format[] = "%04d/%02d/%02d:%02d:%02d:%02d.%03d";
52 ppropvarDest->u.pszVal = HeapAlloc(GetProcessHeap(), 0,
53 lstrlenA(format) + 1);
54 if (!ppropvarDest->u.pszVal)
57 sprintf(ppropvarDest->u.pszVal, format, time.wYear, time.wMonth,
58 time.wDay, time.wHour, time.wMinute,
59 time.wSecond, time.wMilliseconds);
65 FIXME("Unhandled target type: %d\n", vt);
71 static HRESULT PROPVAR_ConvertNumber(REFPROPVARIANT pv, int dest_bits,
72 int dest_signed, LONGLONG *res)
104 *res = pv->u.hVal.QuadPart;
108 *res = pv->u.uhVal.QuadPart;
115 FIXME("unhandled vt %d\n", pv->vt);
119 if (*res < 0 && src_signed != dest_signed)
120 return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW);
126 if (*res >= ((LONGLONG)1 << (dest_bits-1)) ||
127 *res < ((LONGLONG)-1 << (dest_bits-1)))
128 return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW);
132 if ((ULONGLONG)(*res) >= ((ULONGLONG)1 << dest_bits))
133 return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW);
140 HRESULT WINAPI PropVariantToInt16(REFPROPVARIANT propvarIn, SHORT *ret)
145 TRACE("%p,%p\n", propvarIn, ret);
147 hr = PROPVAR_ConvertNumber(propvarIn, 16, 1, &res);
148 if (SUCCEEDED(hr)) *ret = (SHORT)res;
152 HRESULT WINAPI PropVariantToInt32(REFPROPVARIANT propvarIn, LONG *ret)
157 TRACE("%p,%p\n", propvarIn, ret);
159 hr = PROPVAR_ConvertNumber(propvarIn, 32, 1, &res);
160 if (SUCCEEDED(hr)) *ret = (LONG)res;
164 HRESULT WINAPI PropVariantToInt64(REFPROPVARIANT propvarIn, LONGLONG *ret)
169 TRACE("%p,%p\n", propvarIn, ret);
171 hr = PROPVAR_ConvertNumber(propvarIn, 64, 1, &res);
172 if (SUCCEEDED(hr)) *ret = (LONGLONG)res;
176 HRESULT WINAPI PropVariantToUInt16(REFPROPVARIANT propvarIn, USHORT *ret)
181 TRACE("%p,%p\n", propvarIn, ret);
183 hr = PROPVAR_ConvertNumber(propvarIn, 16, 0, &res);
184 if (SUCCEEDED(hr)) *ret = (USHORT)res;
188 HRESULT WINAPI PropVariantToUInt32(REFPROPVARIANT propvarIn, ULONG *ret)
193 TRACE("%p,%p\n", propvarIn, ret);
195 hr = PROPVAR_ConvertNumber(propvarIn, 32, 0, &res);
196 if (SUCCEEDED(hr)) *ret = (ULONG)res;
200 HRESULT WINAPI PropVariantToUInt64(REFPROPVARIANT propvarIn, ULONGLONG *ret)
205 TRACE("%p,%p\n", propvarIn, ret);
207 hr = PROPVAR_ConvertNumber(propvarIn, 64, 0, &res);
208 if (SUCCEEDED(hr)) *ret = (ULONGLONG)res;
212 /******************************************************************
213 * PropVariantChangeType (PROPSYS.@)
215 HRESULT WINAPI PropVariantChangeType(PROPVARIANT *ppropvarDest, REFPROPVARIANT propvarSrc,
216 PROPVAR_CHANGE_FLAGS flags, VARTYPE vt)
220 FIXME("(%p, %p, %d, %d, %d): semi-stub!\n", ppropvarDest, propvarSrc,
221 propvarSrc->vt, flags, vt);
228 hr = PropVariantToInt16(propvarSrc, &res);
231 ppropvarDest->vt = VT_I2;
232 ppropvarDest->u.iVal = res;
239 hr = PropVariantToUInt16(propvarSrc, &res);
242 ppropvarDest->vt = VT_UI2;
243 ppropvarDest->u.uiVal = res;
250 hr = PropVariantToInt32(propvarSrc, &res);
253 ppropvarDest->vt = VT_I4;
254 ppropvarDest->u.lVal = res;
261 hr = PropVariantToUInt32(propvarSrc, &res);
264 ppropvarDest->vt = VT_UI4;
265 ppropvarDest->u.ulVal = res;
272 hr = PropVariantToInt64(propvarSrc, &res);
275 ppropvarDest->vt = VT_I8;
276 ppropvarDest->u.hVal.QuadPart = res;
283 hr = PropVariantToUInt64(propvarSrc, &res);
286 ppropvarDest->vt = VT_UI8;
287 ppropvarDest->u.uhVal.QuadPart = res;
293 switch (propvarSrc->vt)
296 return PROPVAR_ConvertFILETIME(ppropvarDest, propvarSrc, vt);
298 FIXME("Unhandled source type: %d\n", propvarSrc->vt);
304 static void PROPVAR_GUIDToWSTR(REFGUID guid, WCHAR *str)
306 static const WCHAR format[] = {'{','%','0','8','X','-','%','0','4','X','-','%','0','4','X',
307 '-','%','0','2','X','%','0','2','X','-','%','0','2','X','%','0','2','X','%','0','2','X',
308 '%','0','2','X','%','0','2','X','%','0','2','X','}',0};
310 sprintfW(str, format, guid->Data1, guid->Data2, guid->Data3,
311 guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
312 guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]);
315 HRESULT WINAPI InitPropVariantFromGUIDAsString(REFGUID guid, PROPVARIANT *ppropvar)
317 TRACE("(%p %p)\n", guid, ppropvar);
322 ppropvar->vt = VT_LPWSTR;
323 ppropvar->u.pwszVal = CoTaskMemAlloc(39*sizeof(WCHAR));
324 if(!ppropvar->u.pwszVal)
325 return E_OUTOFMEMORY;
327 PROPVAR_GUIDToWSTR(guid, ppropvar->u.pwszVal);
331 HRESULT WINAPI InitVariantFromGUIDAsString(REFGUID guid, VARIANT *pvar)
333 TRACE("(%p %p)\n", guid, pvar);
336 FIXME("guid == NULL\n");
340 V_VT(pvar) = VT_BSTR;
341 V_BSTR(pvar) = SysAllocStringLen(NULL, 38);
343 return E_OUTOFMEMORY;
345 PROPVAR_GUIDToWSTR(guid, V_BSTR(pvar));
349 HRESULT WINAPI InitPropVariantFromBuffer(const VOID *pv, UINT cb, PROPVARIANT *ppropvar)
351 TRACE("(%p %u %p)\n", pv, cb, ppropvar);
353 ppropvar->u.caub.pElems = CoTaskMemAlloc(cb);
354 if(!ppropvar->u.caub.pElems)
355 return E_OUTOFMEMORY;
357 ppropvar->vt = VT_VECTOR|VT_UI1;
358 ppropvar->u.caub.cElems = cb;
359 memcpy(ppropvar->u.caub.pElems, pv, cb);
363 HRESULT WINAPI InitVariantFromBuffer(const VOID *pv, UINT cb, VARIANT *pvar)
369 TRACE("(%p %u %p)\n", pv, cb, pvar);
371 arr = SafeArrayCreateVector(VT_UI1, 0, cb);
373 return E_OUTOFMEMORY;
375 hres = SafeArrayAccessData(arr, &data);
377 SafeArrayDestroy(arr);
381 memcpy(data, pv, cb);
383 hres = SafeArrayUnaccessData(arr);
385 SafeArrayDestroy(arr);
389 V_VT(pvar) = VT_ARRAY|VT_UI1;
394 static inline DWORD PROPVAR_HexToNum(const WCHAR *hex)
398 if(hex[0]>='0' && hex[0]<='9')
400 else if(hex[0]>='a' && hex[0]<='f')
402 else if(hex[0]>='A' && hex[0]<='F')
408 if(hex[1]>='0' && hex[1]<='9')
409 return ret + hex[1]-'0';
410 else if(hex[1]>='a' && hex[1]<='f')
411 return ret + hex[1]-'a'+10;
412 else if(hex[1]>='A' && hex[1]<='F')
413 return ret + hex[1]-'A'+10;
418 static inline HRESULT PROPVAR_WCHARToGUID(const WCHAR *str, int len, GUID *guid)
423 memset(guid, 0, sizeof(GUID));
425 if(len!=38 || str[0]!='{' || str[9]!='-' || str[14]!='-'
426 || str[19]!='-' || str[24]!='-' || str[37]!='}') {
427 WARN("Error parsing %s\n", debugstr_w(str));
432 for(i=0; i<4 && val!=-1; i++) {
433 val = PROPVAR_HexToNum(p);
434 guid->Data1 = (guid->Data1<<8) + val;
438 for(i=0; i<2 && val!=-1; i++) {
439 val = PROPVAR_HexToNum(p);
440 guid->Data2 = (guid->Data2<<8) + val;
444 for(i=0; i<2 && val!=-1; i++) {
445 val = PROPVAR_HexToNum(p);
446 guid->Data3 = (guid->Data3<<8) + val;
450 for(i=0; i<8 && val!=-1; i++) {
454 val = guid->Data4[i] = PROPVAR_HexToNum(p);
459 WARN("Error parsing %s\n", debugstr_w(str));
460 memset(guid, 0, sizeof(GUID));
466 HRESULT WINAPI PropVariantToGUID(const PROPVARIANT *ppropvar, GUID *guid)
468 TRACE("%p %p)\n", ppropvar, guid);
470 switch(ppropvar->vt) {
472 return PROPVAR_WCHARToGUID(ppropvar->u.bstrVal, SysStringLen(ppropvar->u.bstrVal), guid);
474 return PROPVAR_WCHARToGUID(ppropvar->u.pwszVal, strlenW(ppropvar->u.pwszVal), guid);
477 FIXME("unsupported vt: %d\n", ppropvar->vt);
482 HRESULT WINAPI VariantToGUID(const VARIANT *pvar, GUID *guid)
484 TRACE("(%p %p)\n", pvar, guid);
488 HRESULT hres = PROPVAR_WCHARToGUID(V_BSTR(pvar), SysStringLen(V_BSTR(pvar)), guid);
489 if(hres == E_INVALIDARG)
495 FIXME("unsupported vt: %d\n", V_VT(pvar));
500 static int isemptyornull(const PROPVARIANT *propvar)
502 if (propvar->vt == VT_EMPTY || propvar->vt == VT_NULL)
504 if ((propvar->vt & VT_ARRAY) == VT_ARRAY)
507 for (i=0; i<propvar->u.parray->cDims; i++)
509 if (propvar->u.parray->rgsabound[i].cElements != 0)
512 return i == propvar->u.parray->cDims;
514 /* FIXME: vectors, byrefs, errors? */
518 INT WINAPI PropVariantCompareEx(REFPROPVARIANT propvar1, REFPROPVARIANT propvar2,
519 PROPVAR_COMPARE_UNIT unit, PROPVAR_COMPARE_FLAGS flags)
521 const PROPVARIANT *propvar2_converted;
522 PROPVARIANT propvar2_static;
526 TRACE("%p,%p,%x,%x\n", propvar1, propvar2, unit, flags);
528 if (isemptyornull(propvar1))
530 if (isemptyornull(propvar2))
532 return (flags & PVCF_TREATEMPTYASGREATERTHAN) ? 1 : -1;
535 if (isemptyornull(propvar2))
536 return (flags & PVCF_TREATEMPTYASGREATERTHAN) ? -1 : 1;
538 if (propvar1->vt != propvar2->vt)
540 hr = PropVariantChangeType(&propvar2_static, propvar2, 0, propvar1->vt);
545 propvar2_converted = &propvar2_static;
548 propvar2_converted = propvar2;
550 #define CMP_INT_VALUE(var) do { \
551 if (propvar1->u.var > propvar2_converted->u.var) \
553 else if (propvar1->u.var < propvar2_converted->u.var) \
559 switch (propvar1->vt)
571 CMP_INT_VALUE(uiVal);
577 CMP_INT_VALUE(uiVal);
580 CMP_INT_VALUE(hVal.QuadPart);
583 CMP_INT_VALUE(uhVal.QuadPart);
586 /* FIXME: Use string flags. */
587 res = lstrcmpW(propvar1->u.bstrVal, propvar2->u.bstrVal);
590 FIXME("vartype %d not handled\n", propvar1->vt);
595 if (propvar2_converted == &propvar2_static)
596 PropVariantClear(&propvar2_static);