ddraw: Use unsafe_impl_from_IDirect3DViewport for application provided interfaces.
[wine/multimedia.git] / dlls / propsys / propvar.c
blob4ea01d969ab17e67cc867fe2dd8587dfa998ece5
1 /*
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
21 #include <stdarg.h>
22 #include <stdio.h>
24 #define NONAMELESSUNION
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winerror.h"
29 #include "winreg.h"
30 #include "winuser.h"
31 #include "shlobj.h"
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)
42 SYSTEMTIME time;
44 FileTimeToSystemTime(&propvarSrc->u.filetime, &time);
46 switch (vt)
48 case VT_LPSTR:
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)
55 return E_OUTOFMEMORY;
57 sprintf(ppropvarDest->u.pszVal, format, time.wYear, time.wMonth,
58 time.wDay, time.wHour, time.wMinute,
59 time.wSecond, time.wMilliseconds);
61 return S_OK;
64 default:
65 FIXME("Unhandled target type: %d\n", vt);
68 return E_FAIL;
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)
82 case VT_FILETIME:
83 return PROPVAR_ConvertFILETIME(ppropvarDest, propvarSrc, vt);
84 default:
85 FIXME("Unhandled source type: %d\n", propvarSrc->vt);
88 return E_FAIL;
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);
106 if(!guid)
107 return E_FAIL;
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);
115 return S_OK;
118 HRESULT WINAPI InitVariantFromGUIDAsString(REFGUID guid, VARIANT *pvar)
120 TRACE("(%p %p)\n", guid, pvar);
122 if(!guid) {
123 FIXME("guid == NULL\n");
124 return E_FAIL;
127 V_VT(pvar) = VT_BSTR;
128 V_BSTR(pvar) = SysAllocStringLen(NULL, 38);
129 if(!V_BSTR(pvar))
130 return E_OUTOFMEMORY;
132 PROPVAR_GUIDToWSTR(guid, V_BSTR(pvar));
133 return S_OK;
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);
147 return S_OK;
150 HRESULT WINAPI InitVariantFromBuffer(const VOID *pv, UINT cb, VARIANT *pvar)
152 SAFEARRAY *arr;
153 void *data;
154 HRESULT hres;
156 TRACE("(%p %u %p)\n", pv, cb, pvar);
158 arr = SafeArrayCreateVector(VT_UI1, 0, cb);
159 if(!arr)
160 return E_OUTOFMEMORY;
162 hres = SafeArrayAccessData(arr, &data);
163 if(FAILED(hres)) {
164 SafeArrayDestroy(arr);
165 return hres;
168 memcpy(data, pv, cb);
170 hres = SafeArrayUnaccessData(arr);
171 if(FAILED(hres)) {
172 SafeArrayDestroy(arr);
173 return hres;
176 V_VT(pvar) = VT_ARRAY|VT_UI1;
177 V_ARRAY(pvar) = arr;
178 return S_OK;
181 static inline DWORD PROPVAR_HexToNum(const WCHAR *hex)
183 DWORD ret;
185 if(hex[0]>='0' && hex[0]<='9')
186 ret = hex[0]-'0';
187 else if(hex[0]>='a' && hex[0]<='f')
188 ret = hex[0]-'a'+10;
189 else if(hex[0]>='A' && hex[0]<='F')
190 ret = hex[0]-'A'+10;
191 else
192 return -1;
194 ret <<= 4;
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;
201 else
202 return -1;
205 static inline HRESULT PROPVAR_WCHARToGUID(const WCHAR *str, int len, GUID *guid)
207 DWORD i, val=0;
208 const WCHAR *p;
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));
215 return E_INVALIDARG;
218 p = str+1;
219 for(i=0; i<4 && val!=-1; i++) {
220 val = PROPVAR_HexToNum(p);
221 guid->Data1 = (guid->Data1<<8) + val;
222 p += 2;
224 p++;
225 for(i=0; i<2 && val!=-1; i++) {
226 val = PROPVAR_HexToNum(p);
227 guid->Data2 = (guid->Data2<<8) + val;
228 p += 2;
230 p++;
231 for(i=0; i<2 && val!=-1; i++) {
232 val = PROPVAR_HexToNum(p);
233 guid->Data3 = (guid->Data3<<8) + val;
234 p += 2;
236 p++;
237 for(i=0; i<8 && val!=-1; i++) {
238 if(i == 2)
239 p++;
241 val = guid->Data4[i] = PROPVAR_HexToNum(p);
242 p += 2;
245 if(val == -1) {
246 WARN("Error parsing %s\n", debugstr_w(str));
247 memset(guid, 0, sizeof(GUID));
248 return E_INVALIDARG;
250 return S_OK;
253 HRESULT WINAPI PropVariantToGUID(const PROPVARIANT *ppropvar, GUID *guid)
255 TRACE("%p %p)\n", ppropvar, guid);
257 switch(ppropvar->vt) {
258 case VT_BSTR:
259 return PROPVAR_WCHARToGUID(ppropvar->u.bstrVal, SysStringLen(ppropvar->u.bstrVal), guid);
260 case VT_LPWSTR:
261 return PROPVAR_WCHARToGUID(ppropvar->u.pwszVal, strlenW(ppropvar->u.pwszVal), guid);
263 default:
264 FIXME("unsupported vt: %d\n", ppropvar->vt);
265 return E_NOTIMPL;
269 HRESULT WINAPI VariantToGUID(const VARIANT *pvar, GUID *guid)
271 TRACE("(%p %p)\n", pvar, guid);
273 switch(V_VT(pvar)) {
274 case VT_BSTR: {
275 HRESULT hres = PROPVAR_WCHARToGUID(V_BSTR(pvar), SysStringLen(V_BSTR(pvar)), guid);
276 if(hres == E_INVALIDARG)
277 return E_FAIL;
278 return hres;
281 default:
282 FIXME("unsupported vt: %d\n", V_VT(pvar));
283 return E_NOTIMPL;