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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
40 #include "undocshell.h"
41 #include "wine/unicode.h"
42 #include "shell32_main.h"
44 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(shell);
50 extern HRESULT WINAPI IFSFolder_Constructor(IUnknown * pUnkOuter, REFIID riid, LPVOID * ppv);
52 const WCHAR sShell32[12] = {'S','H','E','L','L','3','2','.','D','L','L','\0'};
53 const WCHAR sOLE32[10] = {'O','L','E','3','2','.','D','L','L','\0'};
55 HINSTANCE hShellOle32 = 0;
56 /**************************************************************************
57 * Default ClassFactory types
59 typedef HRESULT (CALLBACK *LPFNCREATEINSTANCE)(IUnknown* pUnkOuter, REFIID riid, LPVOID* ppvObject);
60 IClassFactory * IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI, PLONG pcRefDll, REFIID riidInst);
62 /* this table contains all CLSID's of shell32 objects */
65 LPFNCREATEINSTANCE lpfnCI;
66 } InterfaceTable[] = {
67 {&CLSID_ShellFSFolder, &IFSFolder_Constructor},
68 {&CLSID_MyComputer, &ISF_MyComputer_Constructor},
69 {&CLSID_ShellDesktop, &ISF_Desktop_Constructor},
70 {&CLSID_ShellLink, &IShellLink_Constructor},
71 {&CLSID_DragDropHelper, &IDropTargetHelper_Constructor},
72 {&CLSID_ControlPanel, &IControlPanel_Constructor},
73 {&CLSID_AutoComplete, &IAutoComplete_Constructor},
74 {&CLSID_UnixFolder, &UnixFolder_Constructor},
75 {&CLSID_UnixDosFolder, &UnixDosFolder_Constructor},
76 {&CLSID_FolderShortcut, &FolderShortcut_Constructor},
77 {&CLSID_MyDocuments, &MyDocuments_Constructor},
81 /*************************************************************************
82 * SHCoCreateInstance [SHELL32.102]
88 /* FIXME: this should be SHLWAPI.24 since we can't yet import by ordinal */
90 DWORD WINAPI __SHGUIDToStringW (REFGUID guid, LPWSTR str)
92 WCHAR sFormat[52] = {'{','%','0','8','l','x','-','%','0','4',
93 'x','-','%','0','4','x','-','%','0','2',
94 'x','%','0','2','x','-','%','0','2','x',
95 '%','0','2','x','%','0','2','x','%','0',
96 '2','x','%','0','2','x','%','0','2','x',
99 return wsprintfW ( str, sFormat,
100 guid->Data1, guid->Data2, guid->Data3,
101 guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
102 guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7] );
106 /************************************************************************/
108 HRESULT WINAPI SHCoCreateInstance(
117 CLSID * myclsid = (CLSID*)clsid;
118 WCHAR sKeyName[MAX_PATH];
119 const WCHAR sCLSID[7] = {'C','L','S','I','D','\\','\0'};
121 const WCHAR sInProcServer32[16] ={'\\','I','n','p','r','o','c','S','e','r','v','e','r','3','2','\0'};
122 const WCHAR sLoadWithoutCOM[15] ={'L','o','a','d','W','i','t','h','o','u','t','C','O','M','\0'};
123 WCHAR sDllPath[MAX_PATH];
126 BOOLEAN bLoadFromShell32 = FALSE;
127 BOOLEAN bLoadWithoutCOM = FALSE;
128 IClassFactory * pcf = NULL;
130 if(!ppv) return E_POINTER;
133 /* if the clsid is a string, convert it */
136 if (!aclsid) return REGDB_E_CLASSNOTREG;
137 SHCLSIDFromStringW(aclsid, &iid);
141 TRACE("(%p,%s,unk:%p,%s,%p)\n",
142 aclsid,shdebugstr_guid(myclsid),pUnkOuter,shdebugstr_guid(refiid),ppv);
144 /* we look up the dll path in the registry */
145 __SHGUIDToStringW(myclsid, sClassID);
146 lstrcpyW(sKeyName, sCLSID);
147 lstrcatW(sKeyName, sClassID);
148 lstrcatW(sKeyName, sInProcServer32);
150 if (ERROR_SUCCESS == RegOpenKeyExW(HKEY_CLASSES_ROOT, sKeyName, 0, KEY_READ, &hKey)) {
151 dwSize = sizeof(sDllPath);
152 SHQueryValueExW(hKey, NULL, 0,0, sDllPath, &dwSize );
154 /* if a special registry key is set, we load a shell extension without help of OLE32 */
155 bLoadWithoutCOM = (ERROR_SUCCESS == SHQueryValueExW(hKey, sLoadWithoutCOM, 0, 0, 0, 0));
157 /* if the com object is inside shell32, omit use of ole32 */
158 bLoadFromShell32 = (0==lstrcmpiW( PathFindFileNameW(sDllPath), sShell32));
162 /* since we can't find it in the registry we try internally */
163 bLoadFromShell32 = TRUE;
166 TRACE("WithoutCom=%u FromShell=%u\n", bLoadWithoutCOM, bLoadFromShell32);
168 /* now we create an instance */
169 if (bLoadFromShell32) {
170 if (! SUCCEEDED(DllGetClassObject(myclsid, &IID_IClassFactory,(LPVOID*)&pcf))) {
171 ERR("LoadFromShell failed for CLSID=%s\n", shdebugstr_guid(myclsid));
173 } else if (bLoadWithoutCOM) {
175 /* load an external dll without ole32 */
177 typedef HRESULT (CALLBACK *DllGetClassObjectFunc)(REFCLSID clsid, REFIID iid, LPVOID *ppv);
178 DllGetClassObjectFunc DllGetClassObject;
180 if ((hLibrary = LoadLibraryExW(sDllPath, 0, LOAD_WITH_ALTERED_SEARCH_PATH)) == 0) {
181 ERR("couldn't load InprocServer32 dll %s\n", debugstr_w(sDllPath));
182 hres = E_ACCESSDENIED;
184 } else if (!(DllGetClassObject = (DllGetClassObjectFunc)GetProcAddress(hLibrary, "DllGetClassObject"))) {
185 ERR("couldn't find function DllGetClassObject in %s\n", debugstr_w(sDllPath));
186 FreeLibrary( hLibrary );
187 hres = E_ACCESSDENIED;
189 } else if (! SUCCEEDED(hres = DllGetClassObject(myclsid, &IID_IClassFactory, (LPVOID*)&pcf))) {
190 TRACE("GetClassObject failed 0x%08lx\n", hres);
196 /* load an external dll in the usual way */
197 hres = CoCreateInstance(myclsid, pUnkOuter, CLSCTX_INPROC_SERVER, refiid, ppv);
201 /* here we should have a ClassFactory */
202 if (!pcf) return E_ACCESSDENIED;
204 hres = IClassFactory_CreateInstance(pcf, pUnkOuter, refiid, ppv);
205 IClassFactory_Release(pcf);
209 ERR("failed (0x%08lx) to create CLSID:%s IID:%s\n",
210 hres, shdebugstr_guid(myclsid), shdebugstr_guid(refiid));
211 ERR("class not found in registry\n");
214 TRACE("-- instance: %p\n",*ppv);
218 /*************************************************************************
219 * DllGetClassObject [SHELL32.@]
221 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
223 HRESULT hres = E_OUTOFMEMORY;
224 IClassFactory * pcf = NULL;
227 TRACE("CLSID:%s,IID:%s\n",shdebugstr_guid(rclsid),shdebugstr_guid(iid));
229 if (!ppv) return E_INVALIDARG;
232 /* search our internal interface table */
233 for(i=0;InterfaceTable[i].riid;i++) {
234 if(IsEqualIID(InterfaceTable[i].riid, rclsid)) {
235 TRACE("index[%u]\n", i);
236 pcf = IDefClF_fnConstructor(InterfaceTable[i].lpfnCI, NULL, NULL);
241 FIXME("failed for CLSID=%s\n", shdebugstr_guid(rclsid));
242 return CLASS_E_CLASSNOTAVAILABLE;
245 hres = IClassFactory_QueryInterface(pcf, iid, ppv);
246 IClassFactory_Release(pcf);
248 TRACE("-- pointer to class factory: %p\n",*ppv);
252 /*************************************************************************
253 * SHCLSIDFromString [SHELL32.147]
256 * exported by ordinal
258 DWORD WINAPI SHCLSIDFromStringA (LPCSTR clsid, CLSID *id)
261 TRACE("(%p(%s) %p)\n", clsid, clsid, id);
262 if (!MultiByteToWideChar( CP_ACP, 0, clsid, -1, buffer, sizeof(buffer)/sizeof(WCHAR) ))
263 return CO_E_CLASSSTRING;
264 return CLSIDFromString( buffer, id );
266 DWORD WINAPI SHCLSIDFromStringW (LPCWSTR clsid, CLSID *id)
268 TRACE("(%p(%s) %p)\n", clsid, debugstr_w(clsid), id);
269 return CLSIDFromString((LPWSTR)clsid, id);
271 DWORD WINAPI SHCLSIDFromStringAW (LPVOID clsid, CLSID *id)
273 if (SHELL_OsIsUnicode())
274 return SHCLSIDFromStringW (clsid, id);
275 return SHCLSIDFromStringA (clsid, id);
278 /*************************************************************************
279 * Shell Memory Allocator
282 /* set the vtable later */
283 static const IMallocVtbl VT_Shell_IMalloc32;
285 /* this is the static object instance */
287 const IMallocVtbl *lpVtbl;
291 static _ShellMalloc Shell_Malloc = { &VT_Shell_IMalloc32,1};
293 /* this is the global allocator of shell32 */
294 static IMalloc * ShellTaskAllocator = NULL;
296 /******************************************************************************
297 * IShellMalloc_QueryInterface [VTABLE]
299 static HRESULT WINAPI IShellMalloc_fnQueryInterface(LPMALLOC iface, REFIID refiid, LPVOID *obj)
301 TRACE("(%s,%p)\n",shdebugstr_guid(refiid),obj);
302 if (IsEqualIID(refiid, &IID_IUnknown) || IsEqualIID(refiid, &IID_IMalloc)) {
303 *obj = (LPMALLOC) &Shell_Malloc;
306 return E_NOINTERFACE;
309 /******************************************************************************
310 * IShellMalloc_AddRefRelease [VTABLE]
312 static ULONG WINAPI IShellMalloc_fnAddRefRelease(LPMALLOC iface)
317 /******************************************************************************
318 * IShellMalloc_Alloc [VTABLE]
320 static LPVOID WINAPI IShellMalloc_fnAlloc(LPMALLOC iface, DWORD cb)
324 addr = (LPVOID) LocalAlloc(LMEM_ZEROINIT, cb);
325 TRACE("(%p,%ld);\n",addr,cb);
329 /******************************************************************************
330 * IShellMalloc_Realloc [VTABLE]
332 static LPVOID WINAPI IShellMalloc_fnRealloc(LPMALLOC iface, LPVOID pv, DWORD cb)
338 addr = (LPVOID) LocalReAlloc((HANDLE) pv, cb, LMEM_ZEROINIT | LMEM_MOVEABLE);
340 LocalFree((HANDLE) pv);
345 addr = (LPVOID) LocalAlloc(LMEM_ZEROINIT, cb);
351 TRACE("(%p->%p,%ld)\n",pv,addr,cb);
355 /******************************************************************************
356 * IShellMalloc_Free [VTABLE]
358 static VOID WINAPI IShellMalloc_fnFree(LPMALLOC iface, LPVOID pv)
361 LocalFree((HANDLE) pv);
364 /******************************************************************************
365 * IShellMalloc_GetSize [VTABLE]
367 static DWORD WINAPI IShellMalloc_fnGetSize(LPMALLOC iface, LPVOID pv)
369 DWORD cb = (DWORD) LocalSize((HANDLE)pv);
370 TRACE("(%p,%ld)\n", pv, cb);
374 /******************************************************************************
375 * IShellMalloc_DidAlloc [VTABLE]
377 static INT WINAPI IShellMalloc_fnDidAlloc(LPMALLOC iface, LPVOID pv)
383 /******************************************************************************
384 * IShellMalloc_HeapMinimize [VTABLE]
386 static VOID WINAPI IShellMalloc_fnHeapMinimize(LPMALLOC iface)
391 static const IMallocVtbl VT_Shell_IMalloc32 =
393 IShellMalloc_fnQueryInterface,
394 IShellMalloc_fnAddRefRelease,
395 IShellMalloc_fnAddRefRelease,
396 IShellMalloc_fnAlloc,
397 IShellMalloc_fnRealloc,
399 IShellMalloc_fnGetSize,
400 IShellMalloc_fnDidAlloc,
401 IShellMalloc_fnHeapMinimize
404 /*************************************************************************
405 * SHGetMalloc [SHELL32.@]
407 * Return the shell IMalloc interface.
410 * lpmal [O] Destination for IMalloc interface.
413 * Success: S_OK. lpmal contains the shells IMalloc interface.
414 * Failure. An HRESULT error code.
417 * This function will use CoGetMalloc() if OLE32.DLL is already loaded.
418 * If not it uses an internal implementation as a fallback.
420 HRESULT WINAPI SHGetMalloc(LPMALLOC *lpmal)
422 TRACE("(%p)\n", lpmal);
424 if (!ShellTaskAllocator)
426 HMODULE hOle32 = GetModuleHandleA("OLE32.DLL");
427 /* this is very suspect. we should not being using a different
428 * allocator from deallocator based on something undeterministic
429 * like whether ole32 is loaded. as it happens currently, they
430 * both map to the same allocator deep down, but this could
431 * change in the future. */
433 CoGetMalloc(MEMCTX_TASK, &ShellTaskAllocator);
434 TRACE("got ole32 IMalloc\n");
436 if(!ShellTaskAllocator) {
437 ShellTaskAllocator = (IMalloc* ) &Shell_Malloc;
438 TRACE("use fallback allocator\n");
441 *lpmal = ShellTaskAllocator;
445 /*************************************************************************
446 * SHAlloc [SHELL32.196]
449 * exported by ordinal
451 LPVOID WINAPI SHAlloc(DWORD len)
456 if (!ShellTaskAllocator) SHGetMalloc(&ppv);
458 ret = (LPVOID) IMalloc_Alloc(ShellTaskAllocator, len);
459 TRACE("%lu bytes at %p\n",len, ret);
463 /*************************************************************************
464 * SHFree [SHELL32.195]
467 * exported by ordinal
469 void WINAPI SHFree(LPVOID pv)
474 if (!ShellTaskAllocator) SHGetMalloc(&ppv);
475 IMalloc_Free(ShellTaskAllocator, pv);
478 /*************************************************************************
479 * SHGetDesktopFolder [SHELL32.@]
481 HRESULT WINAPI SHGetDesktopFolder(IShellFolder **psf)
486 if(!psf) return E_INVALIDARG;
488 hres = ISF_Desktop_Constructor(NULL, &IID_IShellFolder,(LPVOID*)psf);
490 TRACE("-- %p->(%p)\n",psf, *psf);
493 /**************************************************************************
494 * Default ClassFactory Implementation
496 * SHCreateDefClassObject
499 * Helper function for dlls without their own classfactory.
500 * A generic classfactory is returned.
501 * When the CreateInstance of the cf is called the callback is executed.
506 const IClassFactoryVtbl *lpVtbl;
509 LPFNCREATEINSTANCE lpfnCI;
510 const IID * riidInst;
511 LONG * pcRefDll; /* pointer to refcounter in external dll (ugrrr...) */
514 static const IClassFactoryVtbl dclfvt;
516 /**************************************************************************
517 * IDefClF_fnConstructor
520 IClassFactory * IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI, PLONG pcRefDll, REFIID riidInst)
524 lpclf = HeapAlloc(GetProcessHeap(),0,sizeof(IDefClFImpl));
526 lpclf->lpVtbl = &dclfvt;
527 lpclf->lpfnCI = lpfnCI;
528 lpclf->pcRefDll = pcRefDll;
530 if (pcRefDll) InterlockedIncrement(pcRefDll);
531 lpclf->riidInst = riidInst;
533 TRACE("(%p)%s\n",lpclf, shdebugstr_guid(riidInst));
534 return (LPCLASSFACTORY)lpclf;
536 /**************************************************************************
537 * IDefClF_fnQueryInterface
539 static HRESULT WINAPI IDefClF_fnQueryInterface(
540 LPCLASSFACTORY iface, REFIID riid, LPVOID *ppvObj)
542 IDefClFImpl *This = (IDefClFImpl *)iface;
544 TRACE("(%p)->(%s)\n",This,shdebugstr_guid(riid));
548 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory)) {
550 InterlockedIncrement(&This->ref);
554 TRACE("-- E_NOINTERFACE\n");
555 return E_NOINTERFACE;
557 /******************************************************************************
560 static ULONG WINAPI IDefClF_fnAddRef(LPCLASSFACTORY iface)
562 IDefClFImpl *This = (IDefClFImpl *)iface;
563 ULONG refCount = InterlockedIncrement(&This->ref);
565 TRACE("(%p)->(count=%lu)\n", This, refCount - 1);
569 /******************************************************************************
572 static ULONG WINAPI IDefClF_fnRelease(LPCLASSFACTORY iface)
574 IDefClFImpl *This = (IDefClFImpl *)iface;
575 ULONG refCount = InterlockedDecrement(&This->ref);
577 TRACE("(%p)->(count=%lu)\n", This, refCount + 1);
581 if (This->pcRefDll) InterlockedDecrement(This->pcRefDll);
583 TRACE("-- destroying IClassFactory(%p)\n",This);
584 HeapFree(GetProcessHeap(),0,This);
589 /******************************************************************************
590 * IDefClF_fnCreateInstance
592 static HRESULT WINAPI IDefClF_fnCreateInstance(
593 LPCLASSFACTORY iface, LPUNKNOWN pUnkOuter, REFIID riid, LPVOID *ppvObject)
595 IDefClFImpl *This = (IDefClFImpl *)iface;
597 TRACE("%p->(%p,%s,%p)\n",This,pUnkOuter,shdebugstr_guid(riid),ppvObject);
601 if ( This->riidInst==NULL ||
602 IsEqualCLSID(riid, This->riidInst) ||
603 IsEqualCLSID(riid, &IID_IUnknown) )
605 return This->lpfnCI(pUnkOuter, riid, ppvObject);
608 ERR("unknown IID requested %s\n",shdebugstr_guid(riid));
609 return E_NOINTERFACE;
611 /******************************************************************************
612 * IDefClF_fnLockServer
614 static HRESULT WINAPI IDefClF_fnLockServer(LPCLASSFACTORY iface, BOOL fLock)
616 IDefClFImpl *This = (IDefClFImpl *)iface;
617 TRACE("%p->(0x%x), not implemented\n",This, fLock);
621 static const IClassFactoryVtbl dclfvt =
623 IDefClF_fnQueryInterface,
626 IDefClF_fnCreateInstance,
630 /******************************************************************************
631 * SHCreateDefClassObject [SHELL32.70]
633 HRESULT WINAPI SHCreateDefClassObject(
636 LPFNCREATEINSTANCE lpfnCI, /* [in] create instance callback entry */
637 LPDWORD pcRefDll, /* [in/out] ref count of the dll */
638 REFIID riidInst) /* [in] optional interface to the instance */
642 TRACE("%s %p %p %p %s\n",
643 shdebugstr_guid(riid), ppv, lpfnCI, pcRefDll, shdebugstr_guid(riidInst));
645 if (! IsEqualCLSID(riid, &IID_IClassFactory) ) return E_NOINTERFACE;
646 if (! (pcf = IDefClF_fnConstructor(lpfnCI, (PLONG)pcRefDll, riidInst))) return E_OUTOFMEMORY;
651 /*************************************************************************
652 * DragAcceptFiles [SHELL32.@]
654 void WINAPI DragAcceptFiles(HWND hWnd, BOOL b)
658 if( !IsWindow(hWnd) ) return;
659 exstyle = GetWindowLongA(hWnd,GWL_EXSTYLE);
661 exstyle |= WS_EX_ACCEPTFILES;
663 exstyle &= ~WS_EX_ACCEPTFILES;
664 SetWindowLongA(hWnd,GWL_EXSTYLE,exstyle);
667 /*************************************************************************
668 * DragFinish [SHELL32.@]
670 void WINAPI DragFinish(HDROP h)
673 GlobalFree((HGLOBAL)h);
676 /*************************************************************************
677 * DragQueryPoint [SHELL32.@]
679 BOOL WINAPI DragQueryPoint(HDROP hDrop, POINT *p)
681 DROPFILES *lpDropFileStruct;
686 lpDropFileStruct = (DROPFILES *) GlobalLock(hDrop);
688 *p = lpDropFileStruct->pt;
689 bRet = lpDropFileStruct->fNC;
695 /*************************************************************************
696 * DragQueryFileA [SHELL32.@]
697 * DragQueryFile [SHELL32.@]
699 UINT WINAPI DragQueryFileA(
707 DROPFILES *lpDropFileStruct = (DROPFILES *) GlobalLock(hDrop);
709 TRACE("(%p, %x, %p, %u)\n", hDrop,lFile,lpszFile,lLength);
711 if(!lpDropFileStruct) goto end;
713 lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->pFiles;
715 if(lpDropFileStruct->fWide) {
716 LPWSTR lpszFileW = NULL;
719 lpszFileW = HeapAlloc(GetProcessHeap(), 0, lLength*sizeof(WCHAR));
720 if(lpszFileW == NULL) {
724 i = DragQueryFileW(hDrop, lFile, lpszFileW, lLength);
727 WideCharToMultiByte(CP_ACP, 0, lpszFileW, -1, lpszFile, lLength, 0, NULL);
728 HeapFree(GetProcessHeap(), 0, lpszFileW);
735 while (*lpDrop++); /* skip filename */
738 i = (lFile == 0xFFFFFFFF) ? i : 0;
745 if (!lpszFile ) goto end; /* needed buffer size */
746 i = (lLength > i) ? i : lLength;
747 lstrcpynA (lpszFile, lpDrop, i);
753 /*************************************************************************
754 * DragQueryFileW [SHELL32.@]
756 UINT WINAPI DragQueryFileW(
764 DROPFILES *lpDropFileStruct = (DROPFILES *) GlobalLock(hDrop);
766 TRACE("(%p, %x, %p, %u)\n", hDrop,lFile,lpszwFile,lLength);
768 if(!lpDropFileStruct) goto end;
770 lpwDrop = (LPWSTR) ((LPSTR)lpDropFileStruct + lpDropFileStruct->pFiles);
772 if(lpDropFileStruct->fWide == FALSE) {
773 LPSTR lpszFileA = NULL;
776 lpszFileA = HeapAlloc(GetProcessHeap(), 0, lLength);
777 if(lpszFileA == NULL) {
781 i = DragQueryFileA(hDrop, lFile, lpszFileA, lLength);
784 MultiByteToWideChar(CP_ACP, 0, lpszFileA, -1, lpszwFile, lLength);
785 HeapFree(GetProcessHeap(), 0, lpszFileA);
793 while (*lpwDrop++); /* skip filename */
796 i = (lFile == 0xFFFFFFFF) ? i : 0;
801 i = strlenW(lpwDrop);
803 if ( !lpszwFile) goto end; /* needed buffer size */
805 i = (lLength > i) ? i : lLength;
806 lstrcpynW (lpszwFile, lpwDrop, i);