2 * handling of SHELL32.DLL OLE-Objects
4 * Copyright 1997 Marcus Meissner
5 * Copyright 1998 Juergen Schmied <juergen.schmied@metronet.de>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #define NONAMELESSUNION
42 #include "undocshell.h"
43 #include "wine/unicode.h"
44 #include "shell32_main.h"
46 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(shell);
52 extern INT WINAPI SHStringFromGUIDW(REFGUID guid, LPWSTR lpszDest, INT cchMax); /* shlwapi.24 */
54 /**************************************************************************
55 * Default ClassFactory types
57 typedef HRESULT (CALLBACK *LPFNCREATEINSTANCE)(IUnknown* pUnkOuter, REFIID riid, LPVOID* ppvObject);
58 static IClassFactory * IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI, PLONG pcRefDll, REFIID riidInst);
60 /* this table contains all CLSIDs of shell32 objects */
63 LPFNCREATEINSTANCE lpfnCI;
64 } InterfaceTable[] = {
66 {&CLSID_ApplicationAssociationRegistration, ApplicationAssociationRegistration_Constructor},
67 {&CLSID_AutoComplete, IAutoComplete_Constructor},
68 {&CLSID_ControlPanel, IControlPanel_Constructor},
69 {&CLSID_DragDropHelper, IDropTargetHelper_Constructor},
70 {&CLSID_FolderShortcut, FolderShortcut_Constructor},
71 {&CLSID_MyComputer, ISF_MyComputer_Constructor},
72 {&CLSID_MyDocuments, MyDocuments_Constructor},
73 {&CLSID_NetworkPlaces, ISF_NetworkPlaces_Constructor},
74 {&CLSID_Printers, Printers_Constructor},
75 {&CLSID_QueryAssociations, QueryAssociations_Constructor},
76 {&CLSID_RecycleBin, RecycleBin_Constructor},
77 {&CLSID_ShellDesktop, ISF_Desktop_Constructor},
78 {&CLSID_ShellFSFolder, IFSFolder_Constructor},
79 {&CLSID_ShellItem, IShellItem_Constructor},
80 {&CLSID_ShellLink, IShellLink_Constructor},
81 {&CLSID_UnixDosFolder, UnixDosFolder_Constructor},
82 {&CLSID_UnixFolder, UnixFolder_Constructor},
83 {&CLSID_ExplorerBrowser,ExplorerBrowser_Constructor},
84 {&CLSID_KnownFolderManager, KnownFolderManager_Constructor},
85 {&CLSID_Shell, IShellDispatch_Constructor},
89 /*************************************************************************
90 * SHCoCreateInstance [SHELL32.102]
92 * Equivalent to CoCreateInstance. Under Windows 9x this function could sometimes
93 * use the shell32 built-in "mini-COM" without the need to load ole32.dll - see
94 * SHLoadOLE for details.
96 * Under wine if a "LoadWithoutCOM" value is present or the object resides in
97 * shell32.dll the function will load the object manually without the help of ole32
100 * exported by ordinal
103 * CoCreateInstace, SHLoadOLE
105 HRESULT WINAPI SHCoCreateInstance(
114 const CLSID * myclsid = clsid;
115 WCHAR sKeyName[MAX_PATH];
116 static const WCHAR sCLSID[] = {'C','L','S','I','D','\\','\0'};
118 static const WCHAR sInProcServer32[] = {'\\','I','n','p','r','o','c','S','e','r','v','e','r','3','2','\0'};
119 static const WCHAR sLoadWithoutCOM[] = {'L','o','a','d','W','i','t','h','o','u','t','C','O','M','\0'};
120 WCHAR sDllPath[MAX_PATH];
123 IClassFactory * pcf = NULL;
125 if(!ppv) return E_POINTER;
128 /* if the clsid is a string, convert it */
131 if (!aclsid) return REGDB_E_CLASSNOTREG;
132 SHCLSIDFromStringW(aclsid, &iid);
136 TRACE("(%p,%s,unk:%p,%s,%p)\n",
137 aclsid,shdebugstr_guid(myclsid),pUnkOuter,shdebugstr_guid(refiid),ppv);
139 if (SUCCEEDED(DllGetClassObject(myclsid, &IID_IClassFactory,(LPVOID*)&pcf)))
141 hres = IClassFactory_CreateInstance(pcf, pUnkOuter, refiid, ppv);
142 IClassFactory_Release(pcf);
146 /* we look up the dll path in the registry */
147 SHStringFromGUIDW(myclsid, sClassID, sizeof(sClassID)/sizeof(WCHAR));
148 lstrcpyW(sKeyName, sCLSID);
149 lstrcatW(sKeyName, sClassID);
150 lstrcatW(sKeyName, sInProcServer32);
152 if (RegOpenKeyExW(HKEY_CLASSES_ROOT, sKeyName, 0, KEY_READ, &hKey))
153 return E_ACCESSDENIED;
155 /* if a special registry key is set, we load a shell extension without help of OLE32 */
156 if (!SHQueryValueExW(hKey, sLoadWithoutCOM, 0, 0, 0, 0))
158 /* load an external dll without ole32 */
160 typedef HRESULT (CALLBACK *DllGetClassObjectFunc)(REFCLSID clsid, REFIID iid, LPVOID *ppv);
161 DllGetClassObjectFunc DllGetClassObject;
163 dwSize = sizeof(sDllPath);
164 SHQueryValueExW(hKey, NULL, 0,0, sDllPath, &dwSize );
166 if ((hLibrary = LoadLibraryExW(sDllPath, 0, LOAD_WITH_ALTERED_SEARCH_PATH)) == 0) {
167 ERR("couldn't load InprocServer32 dll %s\n", debugstr_w(sDllPath));
168 hres = E_ACCESSDENIED;
170 } else if (!(DllGetClassObject = (DllGetClassObjectFunc)GetProcAddress(hLibrary, "DllGetClassObject"))) {
171 ERR("couldn't find function DllGetClassObject in %s\n", debugstr_w(sDllPath));
172 FreeLibrary( hLibrary );
173 hres = E_ACCESSDENIED;
175 } else if (FAILED(hres = DllGetClassObject(myclsid, &IID_IClassFactory, (LPVOID*)&pcf))) {
176 TRACE("GetClassObject failed 0x%08x\n", hres);
180 hres = IClassFactory_CreateInstance(pcf, pUnkOuter, refiid, ppv);
181 IClassFactory_Release(pcf);
184 /* load an external dll in the usual way */
185 hres = CoCreateInstance(myclsid, pUnkOuter, CLSCTX_INPROC_SERVER, refiid, ppv);
189 if (hKey) RegCloseKey(hKey);
192 ERR("failed (0x%08x) to create CLSID:%s IID:%s\n",
193 hres, shdebugstr_guid(myclsid), shdebugstr_guid(refiid));
194 ERR("class not found in registry\n");
197 TRACE("-- instance: %p\n",*ppv);
201 /*************************************************************************
202 * DllGetClassObject [SHELL32.@]
203 * SHDllGetClassObject [SHELL32.128]
205 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
207 HRESULT hres = E_OUTOFMEMORY;
208 IClassFactory * pcf = NULL;
211 TRACE("CLSID:%s,IID:%s\n",shdebugstr_guid(rclsid),shdebugstr_guid(iid));
213 if (!ppv) return E_INVALIDARG;
216 /* search our internal interface table */
217 for(i=0;InterfaceTable[i].riid;i++) {
218 if(IsEqualIID(InterfaceTable[i].riid, rclsid)) {
219 TRACE("index[%u]\n", i);
220 pcf = IDefClF_fnConstructor(InterfaceTable[i].lpfnCI, NULL, NULL);
225 FIXME("failed for CLSID=%s\n", shdebugstr_guid(rclsid));
226 return CLASS_E_CLASSNOTAVAILABLE;
229 hres = IClassFactory_QueryInterface(pcf, iid, ppv);
230 IClassFactory_Release(pcf);
232 TRACE("-- pointer to class factory: %p\n",*ppv);
236 /*************************************************************************
237 * SHCLSIDFromString [SHELL32.147]
239 * Under Windows 9x this was an ANSI version of CLSIDFromString. It also allowed
240 * to avoid dependency on ole32.dll (see SHLoadOLE for details).
242 * Under Windows NT/2000/XP this is equivalent to CLSIDFromString
245 * exported by ordinal
248 * CLSIDFromString, SHLoadOLE
250 DWORD WINAPI SHCLSIDFromStringA (LPCSTR clsid, CLSID *id)
253 TRACE("(%p(%s) %p)\n", clsid, clsid, id);
254 if (!MultiByteToWideChar( CP_ACP, 0, clsid, -1, buffer, sizeof(buffer)/sizeof(WCHAR) ))
255 return CO_E_CLASSSTRING;
256 return CLSIDFromString( buffer, id );
258 DWORD WINAPI SHCLSIDFromStringW (LPCWSTR clsid, CLSID *id)
260 TRACE("(%p(%s) %p)\n", clsid, debugstr_w(clsid), id);
261 return CLSIDFromString(clsid, id);
263 DWORD WINAPI SHCLSIDFromStringAW (LPCVOID clsid, CLSID *id)
265 if (SHELL_OsIsUnicode())
266 return SHCLSIDFromStringW (clsid, id);
267 return SHCLSIDFromStringA (clsid, id);
270 /*************************************************************************
271 * SHGetMalloc [SHELL32.@]
273 * Equivalent to CoGetMalloc(MEMCTX_TASK, ...). Under Windows 9x this function
274 * could use the shell32 built-in "mini-COM" without the need to load ole32.dll -
275 * see SHLoadOLE for details.
278 * lpmal [O] Destination for IMalloc interface.
281 * Success: S_OK. lpmal contains the shells IMalloc interface.
282 * Failure. An HRESULT error code.
285 * CoGetMalloc, SHLoadOLE
287 HRESULT WINAPI SHGetMalloc(LPMALLOC *lpmal)
289 TRACE("(%p)\n", lpmal);
290 return CoGetMalloc(MEMCTX_TASK, lpmal);
293 /*************************************************************************
294 * SHAlloc [SHELL32.196]
296 * Equivalent to CoTaskMemAlloc. Under Windows 9x this function could use
297 * the shell32 built-in "mini-COM" without the need to load ole32.dll -
298 * see SHLoadOLE for details.
301 * exported by ordinal
304 * CoTaskMemAlloc, SHLoadOLE
306 LPVOID WINAPI SHAlloc(DWORD len)
310 ret = CoTaskMemAlloc(len);
311 TRACE("%u bytes at %p\n",len, ret);
315 /*************************************************************************
316 * SHFree [SHELL32.195]
318 * Equivalent to CoTaskMemFree. Under Windows 9x this function could use
319 * the shell32 built-in "mini-COM" without the need to load ole32.dll -
320 * see SHLoadOLE for details.
323 * exported by ordinal
326 * CoTaskMemFree, SHLoadOLE
328 void WINAPI SHFree(LPVOID pv)
334 /*************************************************************************
335 * SHGetDesktopFolder [SHELL32.@]
337 HRESULT WINAPI SHGetDesktopFolder(IShellFolder **psf)
341 TRACE("(%p)\n", psf);
343 if(!psf) return E_INVALIDARG;
346 hres = ISF_Desktop_Constructor(NULL, &IID_IShellFolder, (LPVOID*)psf);
348 TRACE("-- %p->(%p) 0x%08x\n", psf, *psf, hres);
351 /**************************************************************************
352 * Default ClassFactory Implementation
354 * SHCreateDefClassObject
357 * Helper function for dlls without their own classfactory.
358 * A generic classfactory is returned.
359 * When the CreateInstance of the cf is called the callback is executed.
364 IClassFactory IClassFactory_iface;
367 LPFNCREATEINSTANCE lpfnCI;
368 const IID * riidInst;
369 LONG * pcRefDll; /* pointer to refcounter in external dll (ugrrr...) */
372 static inline IDefClFImpl *impl_from_IClassFactory(IClassFactory *iface)
374 return CONTAINING_RECORD(iface, IDefClFImpl, IClassFactory_iface);
377 static const IClassFactoryVtbl dclfvt;
379 /**************************************************************************
380 * IDefClF_fnConstructor
383 static IClassFactory * IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI, PLONG pcRefDll, REFIID riidInst)
387 lpclf = HeapAlloc(GetProcessHeap(),0,sizeof(IDefClFImpl));
389 lpclf->IClassFactory_iface.lpVtbl = &dclfvt;
390 lpclf->lpfnCI = lpfnCI;
391 lpclf->pcRefDll = pcRefDll;
393 if (pcRefDll) InterlockedIncrement(pcRefDll);
394 lpclf->riidInst = riidInst;
396 TRACE("(%p)%s\n",lpclf, shdebugstr_guid(riidInst));
397 return (LPCLASSFACTORY)lpclf;
399 /**************************************************************************
400 * IDefClF_fnQueryInterface
402 static HRESULT WINAPI IDefClF_fnQueryInterface(
403 LPCLASSFACTORY iface, REFIID riid, LPVOID *ppvObj)
405 IDefClFImpl *This = impl_from_IClassFactory(iface);
407 TRACE("(%p)->(%s)\n",This,shdebugstr_guid(riid));
411 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory)) {
413 InterlockedIncrement(&This->ref);
417 TRACE("-- E_NOINTERFACE\n");
418 return E_NOINTERFACE;
420 /******************************************************************************
423 static ULONG WINAPI IDefClF_fnAddRef(LPCLASSFACTORY iface)
425 IDefClFImpl *This = impl_from_IClassFactory(iface);
426 ULONG refCount = InterlockedIncrement(&This->ref);
428 TRACE("(%p)->(count=%u)\n", This, refCount - 1);
432 /******************************************************************************
435 static ULONG WINAPI IDefClF_fnRelease(LPCLASSFACTORY iface)
437 IDefClFImpl *This = impl_from_IClassFactory(iface);
438 ULONG refCount = InterlockedDecrement(&This->ref);
440 TRACE("(%p)->(count=%u)\n", This, refCount + 1);
444 if (This->pcRefDll) InterlockedDecrement(This->pcRefDll);
446 TRACE("-- destroying IClassFactory(%p)\n",This);
447 HeapFree(GetProcessHeap(),0,This);
452 /******************************************************************************
453 * IDefClF_fnCreateInstance
455 static HRESULT WINAPI IDefClF_fnCreateInstance(
456 LPCLASSFACTORY iface, LPUNKNOWN pUnkOuter, REFIID riid, LPVOID *ppvObject)
458 IDefClFImpl *This = impl_from_IClassFactory(iface);
460 TRACE("%p->(%p,%s,%p)\n",This,pUnkOuter,shdebugstr_guid(riid),ppvObject);
464 if ( This->riidInst==NULL ||
465 IsEqualCLSID(riid, This->riidInst) ||
466 IsEqualCLSID(riid, &IID_IUnknown) )
468 return This->lpfnCI(pUnkOuter, riid, ppvObject);
471 ERR("unknown IID requested %s\n",shdebugstr_guid(riid));
472 return E_NOINTERFACE;
474 /******************************************************************************
475 * IDefClF_fnLockServer
477 static HRESULT WINAPI IDefClF_fnLockServer(LPCLASSFACTORY iface, BOOL fLock)
479 IDefClFImpl *This = impl_from_IClassFactory(iface);
480 TRACE("%p->(0x%x), not implemented\n",This, fLock);
484 static const IClassFactoryVtbl dclfvt =
486 IDefClF_fnQueryInterface,
489 IDefClF_fnCreateInstance,
493 /******************************************************************************
494 * SHCreateDefClassObject [SHELL32.70]
496 HRESULT WINAPI SHCreateDefClassObject(
499 LPFNCREATEINSTANCE lpfnCI, /* [in] create instance callback entry */
500 LPDWORD pcRefDll, /* [in/out] ref count of the dll */
501 REFIID riidInst) /* [in] optional interface to the instance */
505 TRACE("%s %p %p %p %s\n",
506 shdebugstr_guid(riid), ppv, lpfnCI, pcRefDll, shdebugstr_guid(riidInst));
508 if (! IsEqualCLSID(riid, &IID_IClassFactory) ) return E_NOINTERFACE;
509 if (! (pcf = IDefClF_fnConstructor(lpfnCI, (PLONG)pcRefDll, riidInst))) return E_OUTOFMEMORY;
514 /*************************************************************************
515 * DragAcceptFiles [SHELL32.@]
517 void WINAPI DragAcceptFiles(HWND hWnd, BOOL b)
521 if( !IsWindow(hWnd) ) return;
522 exstyle = GetWindowLongA(hWnd,GWL_EXSTYLE);
524 exstyle |= WS_EX_ACCEPTFILES;
526 exstyle &= ~WS_EX_ACCEPTFILES;
527 SetWindowLongA(hWnd,GWL_EXSTYLE,exstyle);
530 /*************************************************************************
531 * DragFinish [SHELL32.@]
533 void WINAPI DragFinish(HDROP h)
539 /*************************************************************************
540 * DragQueryPoint [SHELL32.@]
542 BOOL WINAPI DragQueryPoint(HDROP hDrop, POINT *p)
544 DROPFILES *lpDropFileStruct;
549 lpDropFileStruct = GlobalLock(hDrop);
551 *p = lpDropFileStruct->pt;
552 bRet = lpDropFileStruct->fNC;
558 /*************************************************************************
559 * DragQueryFileA [SHELL32.@]
560 * DragQueryFile [SHELL32.@]
562 UINT WINAPI DragQueryFileA(
570 DROPFILES *lpDropFileStruct = GlobalLock(hDrop);
572 TRACE("(%p, %x, %p, %u)\n", hDrop,lFile,lpszFile,lLength);
574 if(!lpDropFileStruct) goto end;
576 lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->pFiles;
578 if(lpDropFileStruct->fWide) {
579 LPWSTR lpszFileW = NULL;
582 lpszFileW = HeapAlloc(GetProcessHeap(), 0, lLength*sizeof(WCHAR));
583 if(lpszFileW == NULL) {
587 i = DragQueryFileW(hDrop, lFile, lpszFileW, lLength);
590 WideCharToMultiByte(CP_ACP, 0, lpszFileW, -1, lpszFile, lLength, 0, NULL);
591 HeapFree(GetProcessHeap(), 0, lpszFileW);
598 while (*lpDrop++); /* skip filename */
601 i = (lFile == 0xFFFFFFFF) ? i : 0;
607 if (!lpszFile ) goto end; /* needed buffer size */
608 lstrcpynA (lpszFile, lpDrop, lLength);
614 /*************************************************************************
615 * DragQueryFileW [SHELL32.@]
617 UINT WINAPI DragQueryFileW(
625 DROPFILES *lpDropFileStruct = GlobalLock(hDrop);
627 TRACE("(%p, %x, %p, %u)\n", hDrop,lFile,lpszwFile,lLength);
629 if(!lpDropFileStruct) goto end;
631 lpwDrop = (LPWSTR) ((LPSTR)lpDropFileStruct + lpDropFileStruct->pFiles);
633 if(lpDropFileStruct->fWide == FALSE) {
634 LPSTR lpszFileA = NULL;
637 lpszFileA = HeapAlloc(GetProcessHeap(), 0, lLength);
638 if(lpszFileA == NULL) {
642 i = DragQueryFileA(hDrop, lFile, lpszFileA, lLength);
645 MultiByteToWideChar(CP_ACP, 0, lpszFileA, -1, lpszwFile, lLength);
646 HeapFree(GetProcessHeap(), 0, lpszFileA);
654 while (*lpwDrop++); /* skip filename */
657 i = (lFile == 0xFFFFFFFF) ? i : 0;
662 i = strlenW(lpwDrop);
663 if ( !lpszwFile) goto end; /* needed buffer size */
664 lstrcpynW (lpszwFile, lpwDrop, lLength);
670 /*************************************************************************
671 * SHPropStgCreate [SHELL32.685]
673 HRESULT WINAPI SHPropStgCreate(IPropertySetStorage *psstg, REFFMTID fmtid,
674 const CLSID *pclsid, DWORD grfFlags, DWORD grfMode,
675 DWORD dwDisposition, IPropertyStorage **ppstg, UINT *puCodePage)
681 TRACE("%p %s %s %x %x %x %p %p\n", psstg, debugstr_guid(fmtid), debugstr_guid(pclsid),
682 grfFlags, grfMode, dwDisposition, ppstg, puCodePage);
684 hres = IPropertySetStorage_Open(psstg, fmtid, grfMode, ppstg);
686 switch(dwDisposition) {
688 if(SUCCEEDED(hres)) {
689 IPropertyStorage_Release(*ppstg);
690 hres = IPropertySetStorage_Delete(psstg, fmtid);
699 hres = IPropertySetStorage_Create(psstg, fmtid, pclsid,
700 grfFlags, grfMode, ppstg);
707 prop.ulKind = PRSPEC_PROPID;
708 prop.u.propid = PID_CODEPAGE;
709 hres = IPropertyStorage_ReadMultiple(*ppstg, 1, &prop, &ret);
710 if(FAILED(hres) || ret.vt!=VT_I2)
713 *puCodePage = ret.u.iVal;
720 /*************************************************************************
721 * SHPropStgReadMultiple [SHELL32.688]
723 HRESULT WINAPI SHPropStgReadMultiple(IPropertyStorage *pps, UINT uCodePage,
724 ULONG cpspec, const PROPSPEC *rgpspec, PROPVARIANT *rgvar)
729 FIXME("%p %u %u %p %p\n", pps, uCodePage, cpspec, rgpspec, rgvar);
731 memset(rgvar, 0, cpspec*sizeof(PROPVARIANT));
732 hres = IPropertyStorage_ReadMultiple(pps, cpspec, rgpspec, rgvar);
740 prop.ulKind = PRSPEC_PROPID;
741 prop.u.propid = PID_CODEPAGE;
742 hres = IPropertyStorage_ReadMultiple(pps, 1, &prop, &ret);
743 if(FAILED(hres) || ret.vt!=VT_I2)
746 uCodePage = ret.u.iVal;
749 hres = IPropertyStorage_Stat(pps, &stat);
753 /* TODO: do something with codepage and stat */
757 /*************************************************************************
758 * SHPropStgWriteMultiple [SHELL32.689]
760 HRESULT WINAPI SHPropStgWriteMultiple(IPropertyStorage *pps, UINT *uCodePage,
761 ULONG cpspec, const PROPSPEC *rgpspec, PROPVARIANT *rgvar, PROPID propidNameFirst)
767 FIXME("%p %p %u %p %p %d\n", pps, uCodePage, cpspec, rgpspec, rgvar, propidNameFirst);
769 hres = IPropertyStorage_Stat(pps, &stat);
773 if(uCodePage && *uCodePage)
774 codepage = *uCodePage;
779 prop.ulKind = PRSPEC_PROPID;
780 prop.u.propid = PID_CODEPAGE;
781 hres = IPropertyStorage_ReadMultiple(pps, 1, &prop, &ret);
784 if(ret.vt!=VT_I2 || !ret.u.iVal)
787 codepage = ret.u.iVal;
789 *uCodePage = codepage;
792 /* TODO: do something with codepage and stat */
794 hres = IPropertyStorage_WriteMultiple(pps, cpspec, rgpspec, rgvar, propidNameFirst);