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. */
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(shdocvw
);
42 #define CHARS_IN_GUID 39
44 /******************************************************************************
47 * Gives access to a registry key's values via the IPropertyBag interface.
49 typedef struct _RegistryPropertyBag
{
50 IPropertyBag IPropertyBag_iface
;
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
);
70 if (IsEqualIID(&IID_IUnknown
, riid
) || IsEqualIID(&IID_IPropertyBag
, riid
)) {
71 *ppv
= &This
->IPropertyBag_iface
;
77 IUnknown_AddRef((IUnknown
*)*ppv
);
81 static ULONG WINAPI
RegistryPropertyBag_IPropertyBag_AddRef(IPropertyBag
*iface
)
83 RegistryPropertyBag
*This
= impl_from_IPropertyBag(iface
);
86 TRACE("(iface=%p)\n", iface
);
88 cRef
= InterlockedIncrement(&This
->m_cRef
);
96 static ULONG WINAPI
RegistryPropertyBag_IPropertyBag_Release(IPropertyBag
*iface
)
98 RegistryPropertyBag
*This
= impl_from_IPropertyBag(iface
);
101 TRACE("(iface=%p)\n", iface
);
103 cRef
= InterlockedDecrement(&This
->m_cRef
);
106 TRACE("Destroying This=%p)\n", This
);
107 RegCloseKey(This
->m_hInitPropertyBagKey
);
109 SHDOCVW_UnlockModule();
115 static HRESULT WINAPI
RegistryPropertyBag_IPropertyBag_Read(IPropertyBag
*iface
,
116 LPCOLESTR pwszPropName
, VARIANT
*pVar
, IErrorLog
*pErrorLog
)
118 RegistryPropertyBag
*This
= impl_from_IPropertyBag(iface
);
120 DWORD dwType
, cbData
;
122 VARTYPE vtDst
= V_VT(pVar
);
125 TRACE("(iface=%p, pwszPropName=%s, pVar=%p, pErrorLog=%p)\n", iface
, debugstr_w(pwszPropName
),
128 res
= RegQueryValueExW(This
->m_hInitPropertyBagKey
, pwszPropName
, NULL
, &dwType
, NULL
, &cbData
);
129 if (res
!= ERROR_SUCCESS
)
132 pwszValue
= heap_alloc(cbData
);
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
);
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
);
150 SysFreeString(V_BSTR(pVar
));
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
);
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
) {
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
);
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
;
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
);
221 if (IsEqualIID(&IID_IUnknown
, riid
) || IsEqualIID(&IID_IClassFactory
, riid
)) {
222 *ppv
= &This
->IClassFactory_iface
;
225 return E_NOINTERFACE
;
228 IUnknown_AddRef((IUnknown
*)*ppv
);
232 static ULONG WINAPI
InstanceObjectFactory_IClassFactory_AddRef(IClassFactory
*iface
)
234 InstanceObjectFactory
*This
= impl_from_IClassFactory(iface
);
237 TRACE("(iface=%p)\n", iface
);
239 cRef
= InterlockedIncrement(&This
->m_cRef
);
242 IClassFactory_LockServer(iface
, TRUE
);
247 static ULONG WINAPI
InstanceObjectFactory_IClassFactory_Release(IClassFactory
*iface
)
249 InstanceObjectFactory
*This
= impl_from_IClassFactory(iface
);
252 TRACE("(iface=%p)\n", iface
);
254 cRef
= InterlockedDecrement(&This
->m_cRef
);
257 IClassFactory_LockServer(iface
, FALSE
);
258 IPropertyBag_Release(This
->m_pPropertyBag
);
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
;
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
);
277 TRACE("Failed to create instance of %s. hr = %08lx\n",
278 debugstr_guid(&This
->m_clsidInstance
), hr
);
282 hr
= IPersistPropertyBag_Load(pPersistPropertyBag
, This
->m_pPropertyBag
, NULL
);
284 TRACE("Failed to initialize object from PropertyBag: hr = %08lx\n", hr
);
285 IPersistPropertyBag_Release(pPersistPropertyBag
);
289 hr
= IPersistPropertyBag_QueryInterface(pPersistPropertyBag
, riid
, ppvObj
);
290 IPersistPropertyBag_Release(pPersistPropertyBag
);
295 static HRESULT WINAPI
InstanceObjectFactory_IClassFactory_LockServer(IClassFactory
*iface
,
298 TRACE("(iface=%p, fLock=%d) stub\n", iface
, fLock
);
301 SHDOCVW_LockModule();
303 SHDOCVW_UnlockModule();
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
;
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
,
336 IClassFactory_Release(&pInstanceObjectFactory
->IClassFactory_iface
);
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.
349 * rclsid [I] CLSID of the 'Shell Instance Object'.
350 * riid [I] Desired interface. Only IClassFactory supported.
351 * ppvClassObj [O] The corresponding ClassObject.
355 * Failure: CLASS_E_CLASSNOTAVAILABLE
357 HRESULT
SHDOCVW_GetShellInstanceObjectClassObject(REFCLSID rclsid
, REFIID riid
,
360 WCHAR wszInstanceKey
[] = L
"CLSID\\{00000000-0000-0000-0000-000000000000}\\Instance";
361 WCHAR wszCLSIDInstance
[CHARS_IN_GUID
];
363 HKEY hInstanceKey
, hInitPropertyBagKey
;
364 DWORD dwType
, cbBytes
= sizeof(wszCLSIDInstance
);
365 IPropertyBag
*pInitPropertyBag
;
369 TRACE("(rclsid=%s, riid=%s, ppvClassObject=%p)\n", debugstr_guid(rclsid
), debugstr_guid(riid
),
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
);
404 RegCloseKey(hInitPropertyBagKey
);
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. */