userenv/tests: Enable compilation with long types.
[wine.git] / dlls / shdocvw / shlinstobj.c
blobd1405a6bb534364986eb3a6cb0dab3ebff6b6991
1 /*
2 * Shell Instance Objects - Add hot water and stir until dissolved.
4 * Copyright 2005 Michael Jung
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 /* 'Shell Instance Objects' allow you to add a node to the shell namespace
22 * (typically a shortcut to some location in the filesystem), just by setting
23 * some registry entries. This feature was introduced with win2k. Please
24 * search for 'Shell Instance Objects' on MSDN to get more information. */
26 #include <stdarg.h>
28 #define COBJMACROS
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winreg.h"
33 #include "objbase.h"
34 #include "oleauto.h"
36 #include "shdocvw.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(shdocvw);
42 #define CHARS_IN_GUID 39
44 /******************************************************************************
45 * RegistryPropertyBag
47 * Gives access to a registry key's values via the IPropertyBag interface.
49 typedef struct _RegistryPropertyBag {
50 IPropertyBag IPropertyBag_iface;
51 LONG m_cRef;
52 HKEY m_hInitPropertyBagKey;
53 } RegistryPropertyBag;
55 static inline RegistryPropertyBag *impl_from_IPropertyBag(IPropertyBag *iface)
57 return CONTAINING_RECORD(iface, RegistryPropertyBag, IPropertyBag_iface);
60 static HRESULT WINAPI RegistryPropertyBag_IPropertyBag_QueryInterface(IPropertyBag *iface,
61 REFIID riid, void **ppv)
63 RegistryPropertyBag *This = impl_from_IPropertyBag(iface);
65 TRACE("(iface=%p, riid=%s, ppv=%p)\n", iface, debugstr_guid(riid), ppv);
67 if (!ppv)
68 return E_INVALIDARG;
70 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IPropertyBag, riid)) {
71 *ppv = &This->IPropertyBag_iface;
72 } else {
73 *ppv = NULL;
74 return E_NOINTERFACE;
77 IUnknown_AddRef((IUnknown*)*ppv);
78 return S_OK;
81 static ULONG WINAPI RegistryPropertyBag_IPropertyBag_AddRef(IPropertyBag *iface)
83 RegistryPropertyBag *This = impl_from_IPropertyBag(iface);
84 ULONG cRef;
86 TRACE("(iface=%p)\n", iface);
88 cRef = InterlockedIncrement(&This->m_cRef);
90 if (cRef == 1)
91 SHDOCVW_LockModule();
93 return cRef;
96 static ULONG WINAPI RegistryPropertyBag_IPropertyBag_Release(IPropertyBag *iface)
98 RegistryPropertyBag *This = impl_from_IPropertyBag(iface);
99 ULONG cRef;
101 TRACE("(iface=%p)\n", iface);
103 cRef = InterlockedDecrement(&This->m_cRef);
105 if (cRef == 0) {
106 TRACE("Destroying This=%p)\n", This);
107 RegCloseKey(This->m_hInitPropertyBagKey);
108 heap_free(This);
109 SHDOCVW_UnlockModule();
112 return cRef;
115 static HRESULT WINAPI RegistryPropertyBag_IPropertyBag_Read(IPropertyBag *iface,
116 LPCOLESTR pwszPropName, VARIANT *pVar, IErrorLog *pErrorLog)
118 RegistryPropertyBag *This = impl_from_IPropertyBag(iface);
119 WCHAR *pwszValue;
120 DWORD dwType, cbData;
121 LONG res;
122 VARTYPE vtDst = V_VT(pVar);
123 HRESULT hr = S_OK;
125 TRACE("(iface=%p, pwszPropName=%s, pVar=%p, pErrorLog=%p)\n", iface, debugstr_w(pwszPropName),
126 pVar, pErrorLog);
128 res = RegQueryValueExW(This->m_hInitPropertyBagKey, pwszPropName, NULL, &dwType, NULL, &cbData);
129 if (res != ERROR_SUCCESS)
130 return E_INVALIDARG;
132 pwszValue = heap_alloc(cbData);
133 if (!pwszValue)
134 return E_OUTOFMEMORY;
136 res = RegQueryValueExW(This->m_hInitPropertyBagKey, pwszPropName, NULL, &dwType,
137 (LPBYTE)pwszValue, &cbData);
138 if (res != ERROR_SUCCESS) {
139 heap_free(pwszValue);
140 return E_INVALIDARG;
143 V_VT(pVar) = VT_BSTR;
144 V_BSTR(pVar) = SysAllocString(pwszValue);
145 heap_free(pwszValue);
147 if (vtDst != VT_BSTR) {
148 hr = VariantChangeTypeEx(pVar, pVar, LOCALE_SYSTEM_DEFAULT, 0, vtDst);
149 if (FAILED(hr))
150 SysFreeString(V_BSTR(pVar));
153 return hr;
156 static HRESULT WINAPI RegistryPropertyBag_IPropertyBag_Write(IPropertyBag *iface,
157 LPCOLESTR pwszPropName, VARIANT *pVar)
159 FIXME("(iface=%p, pwszPropName=%s, pVar=%p) stub\n", iface, debugstr_w(pwszPropName), pVar);
160 return E_NOTIMPL;
163 static const IPropertyBagVtbl RegistryPropertyBag_IPropertyBagVtbl = {
164 RegistryPropertyBag_IPropertyBag_QueryInterface,
165 RegistryPropertyBag_IPropertyBag_AddRef,
166 RegistryPropertyBag_IPropertyBag_Release,
167 RegistryPropertyBag_IPropertyBag_Read,
168 RegistryPropertyBag_IPropertyBag_Write
171 static HRESULT RegistryPropertyBag_Constructor(HKEY hInitPropertyBagKey, REFIID riid, LPVOID *ppvObject) {
172 HRESULT hr = E_FAIL;
173 RegistryPropertyBag *pRegistryPropertyBag;
175 TRACE("(hInitPropertyBagKey=%p, riid=%s, ppvObject=%p)\n", hInitPropertyBagKey,
176 debugstr_guid(riid), ppvObject);
178 pRegistryPropertyBag = heap_alloc(sizeof(RegistryPropertyBag));
179 if (pRegistryPropertyBag) {
180 pRegistryPropertyBag->IPropertyBag_iface.lpVtbl = &RegistryPropertyBag_IPropertyBagVtbl;
181 pRegistryPropertyBag->m_cRef = 0;
182 pRegistryPropertyBag->m_hInitPropertyBagKey = hInitPropertyBagKey;
184 /* The clasping AddRef/Release is for the case that QueryInterface fails, which will result
185 * in a reference count of 0 in the Release call, which will result in object destruction.*/
186 IPropertyBag_AddRef(&pRegistryPropertyBag->IPropertyBag_iface);
187 hr = IPropertyBag_QueryInterface(&pRegistryPropertyBag->IPropertyBag_iface, riid, ppvObject);
188 IPropertyBag_Release(&pRegistryPropertyBag->IPropertyBag_iface);
191 return hr;
194 /******************************************************************************
195 * InstanceObjectFactory
196 * Builds Instance Objects and asks them to initialize themselves based on the
197 * values of a PropertyBag.
199 typedef struct _InstanceObjectFactory {
200 IClassFactory IClassFactory_iface;
201 LONG m_cRef;
202 CLSID m_clsidInstance; /* CLSID of the objects to create. */
203 IPropertyBag *m_pPropertyBag; /* PropertyBag to initialize those objects. */
204 } InstanceObjectFactory;
206 static inline InstanceObjectFactory *impl_from_IClassFactory(IClassFactory *iface)
208 return CONTAINING_RECORD(iface, InstanceObjectFactory, IClassFactory_iface);
211 static HRESULT WINAPI InstanceObjectFactory_IClassFactory_QueryInterface(IClassFactory *iface,
212 REFIID riid, void **ppv)
214 InstanceObjectFactory *This = impl_from_IClassFactory(iface);
216 TRACE("iface=%p, riid=%s, ppv=%p)\n", iface, debugstr_guid(riid), ppv);
218 if (!ppv)
219 return E_INVALIDARG;
221 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IClassFactory, riid)) {
222 *ppv = &This->IClassFactory_iface;
223 } else {
224 *ppv = NULL;
225 return E_NOINTERFACE;
228 IUnknown_AddRef((IUnknown*)*ppv);
229 return S_OK;
232 static ULONG WINAPI InstanceObjectFactory_IClassFactory_AddRef(IClassFactory *iface)
234 InstanceObjectFactory *This = impl_from_IClassFactory(iface);
235 ULONG cRef;
237 TRACE("(iface=%p)\n", iface);
239 cRef = InterlockedIncrement(&This->m_cRef);
241 if (cRef == 1)
242 IClassFactory_LockServer(iface, TRUE);
244 return cRef;
247 static ULONG WINAPI InstanceObjectFactory_IClassFactory_Release(IClassFactory *iface)
249 InstanceObjectFactory *This = impl_from_IClassFactory(iface);
250 ULONG cRef;
252 TRACE("(iface=%p)\n", iface);
254 cRef = InterlockedDecrement(&This->m_cRef);
256 if (cRef == 0) {
257 IClassFactory_LockServer(iface, FALSE);
258 IPropertyBag_Release(This->m_pPropertyBag);
259 heap_free(This);
262 return cRef;
265 static HRESULT WINAPI InstanceObjectFactory_IClassFactory_CreateInstance(IClassFactory *iface,
266 IUnknown *pUnkOuter, REFIID riid, LPVOID *ppvObj)
268 InstanceObjectFactory *This = impl_from_IClassFactory(iface);
269 IPersistPropertyBag *pPersistPropertyBag;
270 HRESULT hr;
272 TRACE("(pUnkOuter=%p, riid=%s, ppvObj=%p)\n", pUnkOuter, debugstr_guid(riid), ppvObj);
274 hr = CoCreateInstance(&This->m_clsidInstance, NULL, CLSCTX_INPROC_SERVER,
275 &IID_IPersistPropertyBag, (LPVOID*)&pPersistPropertyBag);
276 if (FAILED(hr)) {
277 TRACE("Failed to create instance of %s. hr = %08lx\n",
278 debugstr_guid(&This->m_clsidInstance), hr);
279 return hr;
282 hr = IPersistPropertyBag_Load(pPersistPropertyBag, This->m_pPropertyBag, NULL);
283 if (FAILED(hr)) {
284 TRACE("Failed to initialize object from PropertyBag: hr = %08lx\n", hr);
285 IPersistPropertyBag_Release(pPersistPropertyBag);
286 return hr;
289 hr = IPersistPropertyBag_QueryInterface(pPersistPropertyBag, riid, ppvObj);
290 IPersistPropertyBag_Release(pPersistPropertyBag);
292 return hr;
295 static HRESULT WINAPI InstanceObjectFactory_IClassFactory_LockServer(IClassFactory *iface,
296 BOOL fLock)
298 TRACE("(iface=%p, fLock=%d) stub\n", iface, fLock);
300 if (fLock)
301 SHDOCVW_LockModule();
302 else
303 SHDOCVW_UnlockModule();
305 return S_OK;
308 static const IClassFactoryVtbl InstanceObjectFactory_IClassFactoryVtbl = {
309 InstanceObjectFactory_IClassFactory_QueryInterface,
310 InstanceObjectFactory_IClassFactory_AddRef,
311 InstanceObjectFactory_IClassFactory_Release,
312 InstanceObjectFactory_IClassFactory_CreateInstance,
313 InstanceObjectFactory_IClassFactory_LockServer
316 static HRESULT InstanceObjectFactory_Constructor(REFCLSID rclsid, IPropertyBag *pPropertyBag,
317 REFIID riid, LPVOID *ppvObject)
319 InstanceObjectFactory *pInstanceObjectFactory;
320 HRESULT hr = E_FAIL;
322 TRACE("(RegistryPropertyBag=%p, riid=%s, ppvObject=%p)\n", pPropertyBag,
323 debugstr_guid(riid), ppvObject);
325 pInstanceObjectFactory = heap_alloc(sizeof(InstanceObjectFactory));
326 if (pInstanceObjectFactory) {
327 pInstanceObjectFactory->IClassFactory_iface.lpVtbl = &InstanceObjectFactory_IClassFactoryVtbl;
328 pInstanceObjectFactory->m_cRef = 0;
329 pInstanceObjectFactory->m_clsidInstance = *rclsid;
330 pInstanceObjectFactory->m_pPropertyBag = pPropertyBag;
331 IPropertyBag_AddRef(pPropertyBag);
333 IClassFactory_AddRef(&pInstanceObjectFactory->IClassFactory_iface);
334 hr = IClassFactory_QueryInterface(&pInstanceObjectFactory->IClassFactory_iface,
335 riid, ppvObject);
336 IClassFactory_Release(&pInstanceObjectFactory->IClassFactory_iface);
339 return hr;
342 /******************************************************************************
343 * SHDOCVW_GetShellInstanceObjectClassObject [Internal]
345 * Figure if there is a 'Shell Instance Object' conformant registry entry for
346 * the given CLSID and if so create and return a corresponding ClassObject.
348 * PARAMS
349 * rclsid [I] CLSID of the 'Shell Instance Object'.
350 * riid [I] Desired interface. Only IClassFactory supported.
351 * ppvClassObj [O] The corresponding ClassObject.
353 * RETURNS
354 * Success: S_OK,
355 * Failure: CLASS_E_CLASSNOTAVAILABLE
357 HRESULT SHDOCVW_GetShellInstanceObjectClassObject(REFCLSID rclsid, REFIID riid,
358 LPVOID *ppvClassObj)
360 WCHAR wszInstanceKey[] = L"CLSID\\{00000000-0000-0000-0000-000000000000}\\Instance";
361 WCHAR wszCLSIDInstance[CHARS_IN_GUID];
362 CLSID clsidInstance;
363 HKEY hInstanceKey, hInitPropertyBagKey;
364 DWORD dwType, cbBytes = sizeof(wszCLSIDInstance);
365 IPropertyBag *pInitPropertyBag;
366 HRESULT hr;
367 LONG res;
369 TRACE("(rclsid=%s, riid=%s, ppvClassObject=%p)\n", debugstr_guid(rclsid), debugstr_guid(riid),
370 ppvClassObj);
372 /* Figure if there is an 'Instance' subkey for the given CLSID and acquire a handle. */
373 if (!StringFromGUID2(rclsid, wszInstanceKey + 6, CHARS_IN_GUID))
374 return CLASS_E_CLASSNOTAVAILABLE;
375 wszInstanceKey[5+CHARS_IN_GUID] = '\\'; /* Repair the null-termination. */
376 if (ERROR_SUCCESS != RegOpenKeyExW(HKEY_CLASSES_ROOT, wszInstanceKey, 0, KEY_READ, &hInstanceKey))
377 /* If there is no 'Instance' subkey, then it's not a Shell Instance Object. */
378 return CLASS_E_CLASSNOTAVAILABLE;
380 if (ERROR_SUCCESS != RegQueryValueExW(hInstanceKey, L"CLSID", NULL, &dwType, (BYTE *)wszCLSIDInstance, &cbBytes) ||
381 FAILED(CLSIDFromString(wszCLSIDInstance, &clsidInstance)))
383 /* 'Instance' should have a 'CLSID' value with a well-formed clsid-string. */
384 FIXME("Failed to infer instance CLSID! %s\n", debugstr_w(wszCLSIDInstance));
385 RegCloseKey(hInstanceKey);
386 return CLASS_E_CLASSNOTAVAILABLE;
389 /* Try to open the 'InitPropertyBag' subkey. */
390 res = RegOpenKeyExW(hInstanceKey, L"InitPropertyBag", 0, KEY_READ, &hInitPropertyBagKey);
391 RegCloseKey(hInstanceKey);
392 if (res != ERROR_SUCCESS) {
393 /* Besides 'InitPropertyBag's, shell instance objects might be initialized by streams.
394 * So this case might not be an error. */
395 TRACE("No InitPropertyBag key found!\n");
396 return CLASS_E_CLASSNOTAVAILABLE;
399 /* If the construction succeeds, the new RegistryPropertyBag is responsible for closing
400 * hInitPropertyBagKey. */
401 hr = RegistryPropertyBag_Constructor(hInitPropertyBagKey, &IID_IPropertyBag,
402 (LPVOID*)&pInitPropertyBag);
403 if (FAILED(hr)) {
404 RegCloseKey(hInitPropertyBagKey);
405 return hr;
408 /* Construct an Instance Object Factory, which creates objects of class 'clsidInstance'
409 * and asks them to initialize themselves with the help of the 'pInitiPropertyBag' */
410 hr = InstanceObjectFactory_Constructor(&clsidInstance, pInitPropertyBag, riid, ppvClassObj);
411 IPropertyBag_Release(pInitPropertyBag); /* The factory will hold a reference the bag. */
413 return hr;