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},
78 /*************************************************************************
79 * SHCoCreateInstance [SHELL32.102]
85 /* FIXME: this should be SHLWAPI.24 since we can't yet import by ordinal */
87 DWORD WINAPI __SHGUIDToStringW (REFGUID guid, LPWSTR str)
89 WCHAR sFormat[52] = {'{','%','0','8','l','x','-','%','0','4',
90 'x','-','%','0','4','x','-','%','0','2',
91 'x','%','0','2','x','-','%','0','2','x',
92 '%','0','2','x','%','0','2','x','%','0',
93 '2','x','%','0','2','x','%','0','2','x',
96 return wsprintfW ( str, sFormat,
97 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] );
103 /************************************************************************/
105 HRESULT WINAPI SHCoCreateInstance(
114 CLSID * myclsid = (CLSID*)clsid;
115 WCHAR sKeyName[MAX_PATH];
116 const WCHAR sCLSID[7] = {'C','L','S','I','D','\\','\0'};
118 const WCHAR sInProcServer32[16] ={'\\','I','n','p','r','o','c','S','e','r','v','e','r','3','2','\0'};
119 const WCHAR sLoadWithoutCOM[15] ={'L','o','a','d','W','i','t','h','o','u','t','C','O','M','\0'};
120 WCHAR sDllPath[MAX_PATH];
123 BOOLEAN bLoadFromShell32 = FALSE;
124 BOOLEAN bLoadWithoutCOM = FALSE;
125 IClassFactory * pcf = NULL;
127 if(!ppv) return E_POINTER;
130 /* if the clsid is a string, convert it */
133 if (!aclsid) return REGDB_E_CLASSNOTREG;
134 SHCLSIDFromStringW(aclsid, &iid);
138 TRACE("(%p,%s,unk:%p,%s,%p)\n",
139 aclsid,shdebugstr_guid(myclsid),pUnkOuter,shdebugstr_guid(refiid),ppv);
141 /* we look up the dll path in the registry */
142 __SHGUIDToStringW(myclsid, sClassID);
143 lstrcpyW(sKeyName, sCLSID);
144 lstrcatW(sKeyName, sClassID);
145 lstrcatW(sKeyName, sInProcServer32);
147 if (ERROR_SUCCESS == RegOpenKeyExW(HKEY_CLASSES_ROOT, sKeyName, 0, KEY_READ, &hKey)) {
148 dwSize = sizeof(sDllPath);
149 SHQueryValueExW(hKey, NULL, 0,0, sDllPath, &dwSize );
151 /* if a special registry key is set, we load a shell extension without help of OLE32 */
152 bLoadWithoutCOM = (ERROR_SUCCESS == SHQueryValueExW(hKey, sLoadWithoutCOM, 0, 0, 0, 0));
154 /* if the com object is inside shell32, omit use of ole32 */
155 bLoadFromShell32 = (0==lstrcmpiW( PathFindFileNameW(sDllPath), sShell32));
159 /* since we can't find it in the registry we try internally */
160 bLoadFromShell32 = TRUE;
163 TRACE("WithoutCom=%u FromShell=%u\n", bLoadWithoutCOM, bLoadFromShell32);
165 /* now we create a instance */
166 if (bLoadFromShell32) {
167 if (! SUCCEEDED(SHELL32_DllGetClassObject(myclsid, &IID_IClassFactory,(LPVOID*)&pcf))) {
168 ERR("LoadFromShell failed for CLSID=%s\n", shdebugstr_guid(myclsid));
170 } else if (bLoadWithoutCOM) {
172 /* load a external dll without ole32 */
174 typedef HRESULT (CALLBACK *DllGetClassObjectFunc)(REFCLSID clsid, REFIID iid, LPVOID *ppv);
175 DllGetClassObjectFunc DllGetClassObject;
177 if ((hLibrary = LoadLibraryExW(sDllPath, 0, LOAD_WITH_ALTERED_SEARCH_PATH)) == 0) {
178 ERR("couldn't load InprocServer32 dll %s\n", debugstr_w(sDllPath));
179 hres = E_ACCESSDENIED;
181 } else if (!(DllGetClassObject = (DllGetClassObjectFunc)GetProcAddress(hLibrary, "DllGetClassObject"))) {
182 ERR("couldn't find function DllGetClassObject in %s\n", debugstr_w(sDllPath));
183 FreeLibrary( hLibrary );
184 hres = E_ACCESSDENIED;
186 } else if (! SUCCEEDED(hres = DllGetClassObject(myclsid, &IID_IClassFactory, (LPVOID*)&pcf))) {
187 TRACE("GetClassObject failed 0x%08lx\n", hres);
193 /* load a external dll in the usual way */
194 hres = CoCreateInstance(myclsid, pUnkOuter, CLSCTX_INPROC_SERVER, refiid, ppv);
198 /* here we should have a ClassFactory */
199 if (!pcf) return E_ACCESSDENIED;
201 hres = IClassFactory_CreateInstance(pcf, pUnkOuter, refiid, ppv);
202 IClassFactory_Release(pcf);
206 ERR("failed (0x%08lx) to create CLSID:%s IID:%s\n",
207 hres, shdebugstr_guid(myclsid), shdebugstr_guid(refiid));
208 ERR("class not found in registry\n");
211 TRACE("-- instance: %p\n",*ppv);
215 /*************************************************************************
216 * DllGetClassObject [SHELL32.128]
218 HRESULT WINAPI SHELL32_DllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
220 HRESULT hres = E_OUTOFMEMORY;
221 IClassFactory * pcf = NULL;
224 TRACE("CLSID:%s,IID:%s\n",shdebugstr_guid(rclsid),shdebugstr_guid(iid));
226 if (!ppv) return E_INVALIDARG;
229 /* search our internal interface table */
230 for(i=0;InterfaceTable[i].riid;i++) {
231 if(IsEqualIID(InterfaceTable[i].riid, rclsid)) {
232 TRACE("index[%u]\n", i);
233 pcf = IDefClF_fnConstructor(InterfaceTable[i].lpfnCI, NULL, NULL);
238 FIXME("failed for CLSID=%s\n", shdebugstr_guid(rclsid));
239 return CLASS_E_CLASSNOTAVAILABLE;
242 hres = IClassFactory_QueryInterface(pcf, iid, ppv);
243 IClassFactory_Release(pcf);
245 TRACE("-- pointer to class factory: %p\n",*ppv);
249 /*************************************************************************
250 * SHCLSIDFromString [SHELL32.147]
253 * exported by ordinal
255 DWORD WINAPI SHCLSIDFromStringA (LPCSTR clsid, CLSID *id)
258 TRACE("(%p(%s) %p)\n", clsid, clsid, id);
259 if (!MultiByteToWideChar( CP_ACP, 0, clsid, -1, buffer, sizeof(buffer)/sizeof(WCHAR) ))
260 return CO_E_CLASSSTRING;
261 return CLSIDFromString( buffer, id );
263 DWORD WINAPI SHCLSIDFromStringW (LPCWSTR clsid, CLSID *id)
265 TRACE("(%p(%s) %p)\n", clsid, debugstr_w(clsid), id);
266 return CLSIDFromString((LPWSTR)clsid, id);
268 DWORD WINAPI SHCLSIDFromStringAW (LPVOID clsid, CLSID *id)
270 if (SHELL_OsIsUnicode())
271 return SHCLSIDFromStringW (clsid, id);
272 return SHCLSIDFromStringA (clsid, id);
275 /*************************************************************************
276 * Shell Memory Allocator
279 /* set the vtable later */
280 static IMallocVtbl VT_Shell_IMalloc32;
282 /* this is the static object instance */
288 static _ShellMalloc Shell_Malloc = { &VT_Shell_IMalloc32,1};
290 /* this is the global allocator of shell32 */
291 static IMalloc * ShellTaskAllocator = NULL;
293 /******************************************************************************
294 * IShellMalloc_QueryInterface [VTABLE]
296 static HRESULT WINAPI IShellMalloc_fnQueryInterface(LPMALLOC iface, REFIID refiid, LPVOID *obj)
298 TRACE("(%s,%p)\n",shdebugstr_guid(refiid),obj);
299 if (IsEqualIID(refiid, &IID_IUnknown) || IsEqualIID(refiid, &IID_IMalloc)) {
300 *obj = (LPMALLOC) &Shell_Malloc;
303 return E_NOINTERFACE;
306 /******************************************************************************
307 * IShellMalloc_AddRefRelease [VTABLE]
309 static ULONG WINAPI IShellMalloc_fnAddRefRelease(LPMALLOC iface)
314 /******************************************************************************
315 * IShellMalloc_Alloc [VTABLE]
317 static LPVOID WINAPI IShellMalloc_fnAlloc(LPMALLOC iface, DWORD cb)
321 addr = (LPVOID) LocalAlloc(LMEM_ZEROINIT, cb);
322 TRACE("(%p,%ld);\n",addr,cb);
326 /******************************************************************************
327 * IShellMalloc_Realloc [VTABLE]
329 static LPVOID WINAPI IShellMalloc_fnRealloc(LPMALLOC iface, LPVOID pv, DWORD cb)
335 addr = (LPVOID) LocalReAlloc((HANDLE) pv, cb, LMEM_ZEROINIT | LMEM_MOVEABLE);
337 LocalFree((HANDLE) pv);
342 addr = (LPVOID) LocalAlloc(LMEM_ZEROINIT, cb);
348 TRACE("(%p->%p,%ld)\n",pv,addr,cb);
352 /******************************************************************************
353 * IShellMalloc_Free [VTABLE]
355 static VOID WINAPI IShellMalloc_fnFree(LPMALLOC iface, LPVOID pv)
358 LocalFree((HANDLE) pv);
361 /******************************************************************************
362 * IShellMalloc_GetSize [VTABLE]
364 static DWORD WINAPI IShellMalloc_fnGetSize(LPMALLOC iface, LPVOID pv)
366 DWORD cb = (DWORD) LocalSize((HANDLE)pv);
367 TRACE("(%p,%ld)\n", pv, cb);
371 /******************************************************************************
372 * IShellMalloc_DidAlloc [VTABLE]
374 static INT WINAPI IShellMalloc_fnDidAlloc(LPMALLOC iface, LPVOID pv)
380 /******************************************************************************
381 * IShellMalloc_HeapMinimize [VTABLE]
383 static VOID WINAPI IShellMalloc_fnHeapMinimize(LPMALLOC iface)
388 static IMallocVtbl VT_Shell_IMalloc32 =
390 IShellMalloc_fnQueryInterface,
391 IShellMalloc_fnAddRefRelease,
392 IShellMalloc_fnAddRefRelease,
393 IShellMalloc_fnAlloc,
394 IShellMalloc_fnRealloc,
396 IShellMalloc_fnGetSize,
397 IShellMalloc_fnDidAlloc,
398 IShellMalloc_fnHeapMinimize
401 /*************************************************************************
402 * SHGetMalloc [SHELL32.@]
404 * Return the shell IMalloc interface.
407 * lpmal [O] Destination for IMalloc interface.
410 * Success: S_OK. lpmal contains the shells IMalloc interface.
411 * Failure. An HRESULT error code.
414 * This function will use CoGetMalloc() if OLE32.DLL is already loaded.
415 * If not it uses an internal implementation as a fallback.
417 HRESULT WINAPI SHGetMalloc(LPMALLOC *lpmal)
419 TRACE("(%p)\n", lpmal);
421 if (!ShellTaskAllocator)
423 HMODULE hOle32 = GetModuleHandleA("OLE32.DLL");
424 /* this is very suspect. we should not being using a different
425 * allocator from deallocator based on something undeterministic
426 * like whether ole32 is loaded. as it happens currently, they
427 * both map to the same allocator deep down, but this could
428 * change in the future. */
430 CoGetMalloc(MEMCTX_TASK, &ShellTaskAllocator);
431 TRACE("got ole32 IMalloc\n");
433 if(!ShellTaskAllocator) {
434 ShellTaskAllocator = (IMalloc* ) &Shell_Malloc;
435 TRACE("use fallback allocator\n");
438 *lpmal = ShellTaskAllocator;
442 /*************************************************************************
443 * SHAlloc [SHELL32.196]
446 * exported by ordinal
448 LPVOID WINAPI SHAlloc(DWORD len)
453 if (!ShellTaskAllocator) SHGetMalloc(&ppv);
455 ret = (LPVOID) IMalloc_Alloc(ShellTaskAllocator, len);
456 TRACE("%lu bytes at %p\n",len, ret);
460 /*************************************************************************
461 * SHFree [SHELL32.195]
464 * exported by ordinal
466 void WINAPI SHFree(LPVOID pv)
471 if (!ShellTaskAllocator) SHGetMalloc(&ppv);
472 IMalloc_Free(ShellTaskAllocator, pv);
475 /*************************************************************************
476 * SHGetDesktopFolder [SHELL32.@]
478 HRESULT WINAPI SHGetDesktopFolder(IShellFolder **psf)
483 if(!psf) return E_INVALIDARG;
485 hres = ISF_Desktop_Constructor(NULL, &IID_IShellFolder,(LPVOID*)psf);
487 TRACE("-- %p->(%p)\n",psf, *psf);
490 /**************************************************************************
491 * Default ClassFactory Implementation
493 * SHCreateDefClassObject
496 * helper function for dll's without a own classfactory
497 * a generic classfactory is returned
498 * when the CreateInstance of the cf is called the callback is executed
503 IClassFactoryVtbl *lpVtbl;
506 LPFNCREATEINSTANCE lpfnCI;
507 const IID * riidInst;
508 ULONG * pcRefDll; /* pointer to refcounter in external dll (ugrrr...) */
511 static IClassFactoryVtbl dclfvt;
513 /**************************************************************************
514 * IDefClF_fnConstructor
517 IClassFactory * IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI, PLONG pcRefDll, REFIID riidInst)
521 lpclf = (IDefClFImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(IDefClFImpl));
523 lpclf->lpVtbl = &dclfvt;
524 lpclf->lpfnCI = lpfnCI;
525 lpclf->pcRefDll = pcRefDll;
527 if (pcRefDll) InterlockedIncrement(pcRefDll);
528 lpclf->riidInst = riidInst;
530 TRACE("(%p)%s\n",lpclf, shdebugstr_guid(riidInst));
531 return (LPCLASSFACTORY)lpclf;
533 /**************************************************************************
534 * IDefClF_fnQueryInterface
536 static HRESULT WINAPI IDefClF_fnQueryInterface(
537 LPCLASSFACTORY iface, REFIID riid, LPVOID *ppvObj)
539 IDefClFImpl *This = (IDefClFImpl *)iface;
541 TRACE("(%p)->(%s)\n",This,shdebugstr_guid(riid));
545 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory)) {
547 InterlockedIncrement(&This->ref);
551 TRACE("-- E_NOINTERFACE\n");
552 return E_NOINTERFACE;
554 /******************************************************************************
557 static ULONG WINAPI IDefClF_fnAddRef(LPCLASSFACTORY iface)
559 IDefClFImpl *This = (IDefClFImpl *)iface;
560 ULONG refCount = InterlockedIncrement(&This->ref);
562 TRACE("(%p)->(count=%lu)\n", This, refCount - 1);
566 /******************************************************************************
569 static ULONG WINAPI IDefClF_fnRelease(LPCLASSFACTORY iface)
571 IDefClFImpl *This = (IDefClFImpl *)iface;
572 ULONG refCount = InterlockedDecrement(&This->ref);
574 TRACE("(%p)->(count=%lu)\n", This, refCount + 1);
578 if (This->pcRefDll) InterlockedDecrement(This->pcRefDll);
580 TRACE("-- destroying IClassFactory(%p)\n",This);
581 HeapFree(GetProcessHeap(),0,This);
586 /******************************************************************************
587 * IDefClF_fnCreateInstance
589 static HRESULT WINAPI IDefClF_fnCreateInstance(
590 LPCLASSFACTORY iface, LPUNKNOWN pUnkOuter, REFIID riid, LPVOID *ppvObject)
592 IDefClFImpl *This = (IDefClFImpl *)iface;
594 TRACE("%p->(%p,%s,%p)\n",This,pUnkOuter,shdebugstr_guid(riid),ppvObject);
598 if ( This->riidInst==NULL ||
599 IsEqualCLSID(riid, This->riidInst) ||
600 IsEqualCLSID(riid, &IID_IUnknown) )
602 return This->lpfnCI(pUnkOuter, riid, ppvObject);
605 ERR("unknown IID requested %s\n",shdebugstr_guid(riid));
606 return E_NOINTERFACE;
608 /******************************************************************************
609 * IDefClF_fnLockServer
611 static HRESULT WINAPI IDefClF_fnLockServer(LPCLASSFACTORY iface, BOOL fLock)
613 IDefClFImpl *This = (IDefClFImpl *)iface;
614 TRACE("%p->(0x%x), not implemented\n",This, fLock);
618 static IClassFactoryVtbl dclfvt =
620 IDefClF_fnQueryInterface,
623 IDefClF_fnCreateInstance,
627 /******************************************************************************
628 * SHCreateDefClassObject [SHELL32.70]
630 HRESULT WINAPI SHCreateDefClassObject(
633 LPFNCREATEINSTANCE lpfnCI, /* [in] create instance callback entry */
634 LPDWORD pcRefDll, /* [in/out] ref count of the dll */
635 REFIID riidInst) /* [in] optional interface to the instance */
639 TRACE("%s %p %p %p %s\n",
640 shdebugstr_guid(riid), ppv, lpfnCI, pcRefDll, shdebugstr_guid(riidInst));
642 if (! IsEqualCLSID(riid, &IID_IClassFactory) ) return E_NOINTERFACE;
643 if (! (pcf = IDefClF_fnConstructor(lpfnCI, pcRefDll, riidInst))) return E_OUTOFMEMORY;
648 /*************************************************************************
649 * DragAcceptFiles [SHELL32.54]
651 void WINAPI DragAcceptFiles(HWND hWnd, BOOL b)
655 if( !IsWindow(hWnd) ) return;
656 exstyle = GetWindowLongA(hWnd,GWL_EXSTYLE);
658 exstyle |= WS_EX_ACCEPTFILES;
660 exstyle &= ~WS_EX_ACCEPTFILES;
661 SetWindowLongA(hWnd,GWL_EXSTYLE,exstyle);
664 /*************************************************************************
665 * DragFinish [SHELL32.80]
667 void WINAPI DragFinish(HDROP h)
670 GlobalFree((HGLOBAL)h);
673 /*************************************************************************
674 * DragQueryPoint [SHELL32.135]
676 BOOL WINAPI DragQueryPoint(HDROP hDrop, POINT *p)
678 DROPFILES *lpDropFileStruct;
683 lpDropFileStruct = (DROPFILES *) GlobalLock(hDrop);
685 *p = lpDropFileStruct->pt;
686 bRet = lpDropFileStruct->fNC;
692 /*************************************************************************
693 * DragQueryFile [SHELL32.81]
694 * DragQueryFileA [SHELL32.82]
696 UINT WINAPI DragQueryFileA(
704 DROPFILES *lpDropFileStruct = (DROPFILES *) GlobalLock(hDrop);
706 TRACE("(%p, %x, %p, %u)\n", hDrop,lFile,lpszFile,lLength);
708 if(!lpDropFileStruct) goto end;
710 lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->pFiles;
712 if(lpDropFileStruct->fWide) {
713 LPWSTR lpszFileW = NULL;
716 lpszFileW = (LPWSTR) HeapAlloc(GetProcessHeap(), 0, lLength*sizeof(WCHAR));
717 if(lpszFileW == NULL) {
721 i = DragQueryFileW(hDrop, lFile, lpszFileW, lLength);
724 WideCharToMultiByte(CP_ACP, 0, lpszFileW, -1, lpszFile, lLength, 0, NULL);
725 HeapFree(GetProcessHeap(), 0, lpszFileW);
732 while (*lpDrop++); /* skip filename */
735 i = (lFile == 0xFFFFFFFF) ? i : 0;
742 if (!lpszFile ) goto end; /* needed buffer size */
743 i = (lLength > i) ? i : lLength;
744 lstrcpynA (lpszFile, lpDrop, i);
750 /*************************************************************************
751 * DragQueryFileW [SHELL32.133]
753 UINT WINAPI DragQueryFileW(
761 DROPFILES *lpDropFileStruct = (DROPFILES *) GlobalLock(hDrop);
763 TRACE("(%p, %x, %p, %u)\n", hDrop,lFile,lpszwFile,lLength);
765 if(!lpDropFileStruct) goto end;
767 lpwDrop = (LPWSTR) ((LPSTR)lpDropFileStruct + lpDropFileStruct->pFiles);
769 if(lpDropFileStruct->fWide == FALSE) {
770 LPSTR lpszFileA = NULL;
773 lpszFileA = (LPSTR) HeapAlloc(GetProcessHeap(), 0, lLength);
774 if(lpszFileA == NULL) {
778 i = DragQueryFileA(hDrop, lFile, lpszFileA, lLength);
781 MultiByteToWideChar(CP_ACP, 0, lpszFileA, -1, lpszwFile, lLength);
782 HeapFree(GetProcessHeap(), 0, lpszFileA);
790 while (*lpwDrop++); /* skip filename */
793 i = (lFile == 0xFFFFFFFF) ? i : 0;
798 i = strlenW(lpwDrop);
800 if ( !lpszwFile) goto end; /* needed buffer size */
802 i = (lLength > i) ? i : lLength;
803 lstrcpynW (lpszwFile, lpwDrop, i);