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 /******************************************************************
72 * PropVariantChangeType (PROPSYS.@)
74 HRESULT WINAPI
PropVariantChangeType(PROPVARIANT
*ppropvarDest
, REFPROPVARIANT propvarSrc
,
75 PROPVAR_CHANGE_FLAGS flags
, VARTYPE vt
)
77 FIXME("(%p, %p, %d, %d, %d): semi-stub!\n", ppropvarDest
, propvarSrc
,
78 propvarSrc
->vt
, flags
, vt
);
80 switch (propvarSrc
->vt
)
83 return PROPVAR_ConvertFILETIME(ppropvarDest
, propvarSrc
, vt
);
85 FIXME("Unhandled source type: %d\n", propvarSrc
->vt
);
91 static void PROPVAR_GUIDToWSTR(REFGUID guid
, WCHAR
*str
)
93 static const WCHAR format
[] = {'{','%','0','8','X','-','%','0','4','X','-','%','0','4','X',
94 '-','%','0','2','X','%','0','2','X','-','%','0','2','X','%','0','2','X','%','0','2','X',
95 '%','0','2','X','%','0','2','X','%','0','2','X','}',0};
97 sprintfW(str
, format
, guid
->Data1
, guid
->Data2
, guid
->Data3
,
98 guid
->Data4
[0], guid
->Data4
[1], guid
->Data4
[2], guid
->Data4
[3],
99 guid
->Data4
[4], guid
->Data4
[5], guid
->Data4
[6], guid
->Data4
[7]);
102 HRESULT WINAPI
InitPropVariantFromGUIDAsString(REFGUID guid
, PROPVARIANT
*ppropvar
)
104 TRACE("(%p %p)\n", guid
, ppropvar
);
109 ppropvar
->vt
= VT_LPWSTR
;
110 ppropvar
->u
.pwszVal
= CoTaskMemAlloc(39*sizeof(WCHAR
));
111 if(!ppropvar
->u
.pwszVal
)
112 return E_OUTOFMEMORY
;
114 PROPVAR_GUIDToWSTR(guid
, ppropvar
->u
.pwszVal
);
118 HRESULT WINAPI
InitVariantFromGUIDAsString(REFGUID guid
, VARIANT
*pvar
)
120 TRACE("(%p %p)\n", guid
, pvar
);
123 FIXME("guid == NULL\n");
127 V_VT(pvar
) = VT_BSTR
;
128 V_BSTR(pvar
) = SysAllocStringLen(NULL
, 38);
130 return E_OUTOFMEMORY
;
132 PROPVAR_GUIDToWSTR(guid
, V_BSTR(pvar
));
136 HRESULT WINAPI
InitPropVariantFromBuffer(const VOID
*pv
, UINT cb
, PROPVARIANT
*ppropvar
)
138 TRACE("(%p %u %p)\n", pv
, cb
, ppropvar
);
140 ppropvar
->u
.caub
.pElems
= CoTaskMemAlloc(cb
);
141 if(!ppropvar
->u
.caub
.pElems
)
142 return E_OUTOFMEMORY
;
144 ppropvar
->vt
= VT_VECTOR
|VT_UI1
;
145 ppropvar
->u
.caub
.cElems
= cb
;
146 memcpy(ppropvar
->u
.caub
.pElems
, pv
, cb
);
150 HRESULT WINAPI
InitVariantFromBuffer(const VOID
*pv
, UINT cb
, VARIANT
*pvar
)
156 TRACE("(%p %u %p)\n", pv
, cb
, pvar
);
158 arr
= SafeArrayCreateVector(VT_UI1
, 0, cb
);
160 return E_OUTOFMEMORY
;
162 hres
= SafeArrayAccessData(arr
, &data
);
164 SafeArrayDestroy(arr
);
168 memcpy(data
, pv
, cb
);
170 hres
= SafeArrayUnaccessData(arr
);
172 SafeArrayDestroy(arr
);
176 V_VT(pvar
) = VT_ARRAY
|VT_UI1
;
181 static inline DWORD
PROPVAR_HexToNum(const WCHAR
*hex
)
185 if(hex
[0]>='0' && hex
[0]<='9')
187 else if(hex
[0]>='a' && hex
[0]<='f')
189 else if(hex
[0]>='A' && hex
[0]<='F')
195 if(hex
[1]>='0' && hex
[1]<='9')
196 return ret
+ hex
[1]-'0';
197 else if(hex
[1]>='a' && hex
[1]<='f')
198 return ret
+ hex
[1]-'a'+10;
199 else if(hex
[1]>='A' && hex
[1]<='F')
200 return ret
+ hex
[1]-'A'+10;
205 static inline HRESULT
PROPVAR_WCHARToGUID(const WCHAR
*str
, int len
, GUID
*guid
)
210 memset(guid
, 0, sizeof(GUID
));
212 if(len
!=38 || str
[0]!='{' || str
[9]!='-' || str
[14]!='-'
213 || str
[19]!='-' || str
[24]!='-' || str
[37]!='}') {
214 WARN("Error parsing %s\n", debugstr_w(str
));
219 for(i
=0; i
<4 && val
!=-1; i
++) {
220 val
= PROPVAR_HexToNum(p
);
221 guid
->Data1
= (guid
->Data1
<<8) + val
;
225 for(i
=0; i
<2 && val
!=-1; i
++) {
226 val
= PROPVAR_HexToNum(p
);
227 guid
->Data2
= (guid
->Data2
<<8) + val
;
231 for(i
=0; i
<2 && val
!=-1; i
++) {
232 val
= PROPVAR_HexToNum(p
);
233 guid
->Data3
= (guid
->Data3
<<8) + val
;
237 for(i
=0; i
<8 && val
!=-1; i
++) {
241 val
= guid
->Data4
[i
] = PROPVAR_HexToNum(p
);
246 WARN("Error parsing %s\n", debugstr_w(str
));
247 memset(guid
, 0, sizeof(GUID
));
253 HRESULT WINAPI
PropVariantToGUID(const PROPVARIANT
*ppropvar
, GUID
*guid
)
255 TRACE("%p %p)\n", ppropvar
, guid
);
257 switch(ppropvar
->vt
) {
259 return PROPVAR_WCHARToGUID(ppropvar
->u
.bstrVal
, SysStringLen(ppropvar
->u
.bstrVal
), guid
);
261 return PROPVAR_WCHARToGUID(ppropvar
->u
.pwszVal
, strlenW(ppropvar
->u
.pwszVal
), guid
);
264 FIXME("unsupported vt: %d\n", ppropvar
->vt
);
269 HRESULT WINAPI
VariantToGUID(const VARIANT
*pvar
, GUID
*guid
)
271 TRACE("(%p %p)\n", pvar
, guid
);
275 HRESULT hres
= PROPVAR_WCHARToGUID(V_BSTR(pvar
), SysStringLen(V_BSTR(pvar
)), guid
);
276 if(hres
== E_INVALIDARG
)
282 FIXME("unsupported vt: %d\n", V_VT(pvar
));