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},
79 /*************************************************************************
80 * SHCoCreateInstance [SHELL32.102]
86 /* FIXME: this should be SHLWAPI.24 since we can't yet import by ordinal */
88 DWORD WINAPI __SHGUIDToStringW (REFGUID guid, LPWSTR str)
90 WCHAR sFormat[52] = {'{','%','0','8','l','x','-','%','0','4',
91 'x','-','%','0','4','x','-','%','0','2',
92 'x','%','0','2','x','-','%','0','2','x',
93 '%','0','2','x','%','0','2','x','%','0',
94 '2','x','%','0','2','x','%','0','2','x',
97 return wsprintfW ( str, sFormat,
98 guid->Data1, guid->Data2, guid->Data3,
99 guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
100 guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7] );
104 /************************************************************************/
106 HRESULT WINAPI SHCoCreateInstance(
115 CLSID * myclsid = (CLSID*)clsid;
116 WCHAR sKeyName[MAX_PATH];
117 const WCHAR sCLSID[7] = {'C','L','S','I','D','\\','\0'};
119 const WCHAR sInProcServer32[16] ={'\\','I','n','p','r','o','c','S','e','r','v','e','r','3','2','\0'};
120 const WCHAR sLoadWithoutCOM[15] ={'L','o','a','d','W','i','t','h','o','u','t','C','O','M','\0'};
121 WCHAR sDllPath[MAX_PATH];
124 BOOLEAN bLoadFromShell32 = FALSE;
125 BOOLEAN bLoadWithoutCOM = FALSE;
126 IClassFactory * pcf = NULL;
128 if(!ppv) return E_POINTER;
131 /* if the clsid is a string, convert it */
134 if (!aclsid) return REGDB_E_CLASSNOTREG;
135 SHCLSIDFromStringW(aclsid, &iid);
139 TRACE("(%p,%s,unk:%p,%s,%p)\n",
140 aclsid,shdebugstr_guid(myclsid),pUnkOuter,shdebugstr_guid(refiid),ppv);
142 /* we look up the dll path in the registry */
143 __SHGUIDToStringW(myclsid, sClassID);
144 lstrcpyW(sKeyName, sCLSID);
145 lstrcatW(sKeyName, sClassID);
146 lstrcatW(sKeyName, sInProcServer32);
148 if (ERROR_SUCCESS == RegOpenKeyExW(HKEY_CLASSES_ROOT, sKeyName, 0, KEY_READ, &hKey)) {
149 dwSize = sizeof(sDllPath);
150 SHQueryValueExW(hKey, NULL, 0,0, sDllPath, &dwSize );
152 /* if a special registry key is set, we load a shell extension without help of OLE32 */
153 bLoadWithoutCOM = (ERROR_SUCCESS == SHQueryValueExW(hKey, sLoadWithoutCOM, 0, 0, 0, 0));
155 /* if the com object is inside shell32, omit use of ole32 */
156 bLoadFromShell32 = (0==lstrcmpiW( PathFindFileNameW(sDllPath), sShell32));
160 /* since we can't find it in the registry we try internally */
161 bLoadFromShell32 = TRUE;
164 TRACE("WithoutCom=%u FromShell=%u\n", bLoadWithoutCOM, bLoadFromShell32);
166 /* now we create an instance */
167 if (bLoadFromShell32) {
168 if (! SUCCEEDED(SHELL32_DllGetClassObject(myclsid, &IID_IClassFactory,(LPVOID*)&pcf))) {
169 ERR("LoadFromShell failed for CLSID=%s\n", shdebugstr_guid(myclsid));
171 } else if (bLoadWithoutCOM) {
173 /* load an external dll without ole32 */
175 typedef HRESULT (CALLBACK *DllGetClassObjectFunc)(REFCLSID clsid, REFIID iid, LPVOID *ppv);
176 DllGetClassObjectFunc DllGetClassObject;
178 if ((hLibrary = LoadLibraryExW(sDllPath, 0, LOAD_WITH_ALTERED_SEARCH_PATH)) == 0) {
179 ERR("couldn't load InprocServer32 dll %s\n", debugstr_w(sDllPath));
180 hres = E_ACCESSDENIED;
182 } else if (!(DllGetClassObject = (DllGetClassObjectFunc)GetProcAddress(hLibrary, "DllGetClassObject"))) {
183 ERR("couldn't find function DllGetClassObject in %s\n", debugstr_w(sDllPath));
184 FreeLibrary( hLibrary );
185 hres = E_ACCESSDENIED;
187 } else if (! SUCCEEDED(hres = DllGetClassObject(myclsid, &IID_IClassFactory, (LPVOID*)&pcf))) {
188 TRACE("GetClassObject failed 0x%08lx\n", hres);
194 /* load an external dll in the usual way */
195 hres = CoCreateInstance(myclsid, pUnkOuter, CLSCTX_INPROC_SERVER, refiid, ppv);
199 /* here we should have a ClassFactory */
200 if (!pcf) return E_ACCESSDENIED;
202 hres = IClassFactory_CreateInstance(pcf, pUnkOuter, refiid, ppv);
203 IClassFactory_Release(pcf);
207 ERR("failed (0x%08lx) to create CLSID:%s IID:%s\n",
208 hres, shdebugstr_guid(myclsid), shdebugstr_guid(refiid));
209 ERR("class not found in registry\n");
212 TRACE("-- instance: %p\n",*ppv);
216 /*************************************************************************
217 * DllGetClassObject [SHELL32.@]
219 HRESULT WINAPI SHELL32_DllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
221 HRESULT hres = E_OUTOFMEMORY;
222 IClassFactory * pcf = NULL;
225 TRACE("CLSID:%s,IID:%s\n",shdebugstr_guid(rclsid),shdebugstr_guid(iid));
227 if (!ppv) return E_INVALIDARG;
230 /* search our internal interface table */
231 for(i=0;InterfaceTable[i].riid;i++) {
232 if(IsEqualIID(InterfaceTable[i].riid, rclsid)) {
233 TRACE("index[%u]\n", i);
234 pcf = IDefClF_fnConstructor(InterfaceTable[i].lpfnCI, NULL, NULL);
239 FIXME("failed for CLSID=%s\n", shdebugstr_guid(rclsid));
240 return CLASS_E_CLASSNOTAVAILABLE;
243 hres = IClassFactory_QueryInterface(pcf, iid, ppv);
244 IClassFactory_Release(pcf);
246 TRACE("-- pointer to class factory: %p\n",*ppv);
250 /*************************************************************************
251 * SHCLSIDFromString [SHELL32.147]
254 * exported by ordinal
256 DWORD WINAPI SHCLSIDFromStringA (LPCSTR clsid, CLSID *id)
259 TRACE("(%p(%s) %p)\n", clsid, clsid, id);
260 if (!MultiByteToWideChar( CP_ACP, 0, clsid, -1, buffer, sizeof(buffer)/sizeof(WCHAR) ))
261 return CO_E_CLASSSTRING;
262 return CLSIDFromString( buffer, id );
264 DWORD WINAPI SHCLSIDFromStringW (LPCWSTR clsid, CLSID *id)
266 TRACE("(%p(%s) %p)\n", clsid, debugstr_w(clsid), id);
267 return CLSIDFromString((LPWSTR)clsid, id);
269 DWORD WINAPI SHCLSIDFromStringAW (LPVOID clsid, CLSID *id)
271 if (SHELL_OsIsUnicode())
272 return SHCLSIDFromStringW (clsid, id);
273 return SHCLSIDFromStringA (clsid, id);
276 /*************************************************************************
277 * Shell Memory Allocator
280 /* set the vtable later */
281 static const IMallocVtbl VT_Shell_IMalloc32;
283 /* this is the static object instance */
285 const IMallocVtbl *lpVtbl;
289 static _ShellMalloc Shell_Malloc = { &VT_Shell_IMalloc32,1};
291 /* this is the global allocator of shell32 */
292 static IMalloc * ShellTaskAllocator = NULL;
294 /******************************************************************************
295 * IShellMalloc_QueryInterface [VTABLE]
297 static HRESULT WINAPI IShellMalloc_fnQueryInterface(LPMALLOC iface, REFIID refiid, LPVOID *obj)
299 TRACE("(%s,%p)\n",shdebugstr_guid(refiid),obj);
300 if (IsEqualIID(refiid, &IID_IUnknown) || IsEqualIID(refiid, &IID_IMalloc)) {
301 *obj = (LPMALLOC) &Shell_Malloc;
304 return E_NOINTERFACE;
307 /******************************************************************************
308 * IShellMalloc_AddRefRelease [VTABLE]
310 static ULONG WINAPI IShellMalloc_fnAddRefRelease(LPMALLOC iface)
315 /******************************************************************************
316 * IShellMalloc_Alloc [VTABLE]
318 static LPVOID WINAPI IShellMalloc_fnAlloc(LPMALLOC iface, DWORD cb)
322 addr = (LPVOID) LocalAlloc(LMEM_ZEROINIT, cb);
323 TRACE("(%p,%ld);\n",addr,cb);
327 /******************************************************************************
328 * IShellMalloc_Realloc [VTABLE]
330 static LPVOID WINAPI IShellMalloc_fnRealloc(LPMALLOC iface, LPVOID pv, DWORD cb)
336 addr = (LPVOID) LocalReAlloc((HANDLE) pv, cb, LMEM_ZEROINIT | LMEM_MOVEABLE);
338 LocalFree((HANDLE) pv);
343 addr = (LPVOID) LocalAlloc(LMEM_ZEROINIT, cb);
349 TRACE("(%p->%p,%ld)\n",pv,addr,cb);
353 /******************************************************************************
354 * IShellMalloc_Free [VTABLE]
356 static VOID WINAPI IShellMalloc_fnFree(LPMALLOC iface, LPVOID pv)
359 LocalFree((HANDLE) pv);
362 /******************************************************************************
363 * IShellMalloc_GetSize [VTABLE]
365 static DWORD WINAPI IShellMalloc_fnGetSize(LPMALLOC iface, LPVOID pv)
367 DWORD cb = (DWORD) LocalSize((HANDLE)pv);
368 TRACE("(%p,%ld)\n", pv, cb);
372 /******************************************************************************
373 * IShellMalloc_DidAlloc [VTABLE]
375 static INT WINAPI IShellMalloc_fnDidAlloc(LPMALLOC iface, LPVOID pv)
381 /******************************************************************************
382 * IShellMalloc_HeapMinimize [VTABLE]
384 static VOID WINAPI IShellMalloc_fnHeapMinimize(LPMALLOC iface)
389 static const IMallocVtbl VT_Shell_IMalloc32 =
391 IShellMalloc_fnQueryInterface,
392 IShellMalloc_fnAddRefRelease,
393 IShellMalloc_fnAddRefRelease,
394 IShellMalloc_fnAlloc,
395 IShellMalloc_fnRealloc,
397 IShellMalloc_fnGetSize,
398 IShellMalloc_fnDidAlloc,
399 IShellMalloc_fnHeapMinimize
402 /*************************************************************************
403 * SHGetMalloc [SHELL32.@]
405 * Return the shell IMalloc interface.
408 * lpmal [O] Destination for IMalloc interface.
411 * Success: S_OK. lpmal contains the shells IMalloc interface.
412 * Failure. An HRESULT error code.
415 * This function will use CoGetMalloc() if OLE32.DLL is already loaded.
416 * If not it uses an internal implementation as a fallback.
418 HRESULT WINAPI SHGetMalloc(LPMALLOC *lpmal)
420 TRACE("(%p)\n", lpmal);
422 if (!ShellTaskAllocator)
424 HMODULE hOle32 = GetModuleHandleA("OLE32.DLL");
425 /* this is very suspect. we should not being using a different
426 * allocator from deallocator based on something undeterministic
427 * like whether ole32 is loaded. as it happens currently, they
428 * both map to the same allocator deep down, but this could
429 * change in the future. */
431 CoGetMalloc(MEMCTX_TASK, &ShellTaskAllocator);
432 TRACE("got ole32 IMalloc\n");
434 if(!ShellTaskAllocator) {
435 ShellTaskAllocator = (IMalloc* ) &Shell_Malloc;
436 TRACE("use fallback allocator\n");
439 *lpmal = ShellTaskAllocator;
443 /*************************************************************************
444 * SHAlloc [SHELL32.196]
447 * exported by ordinal
449 LPVOID WINAPI SHAlloc(DWORD len)
454 if (!ShellTaskAllocator) SHGetMalloc(&ppv);
456 ret = (LPVOID) IMalloc_Alloc(ShellTaskAllocator, len);
457 TRACE("%lu bytes at %p\n",len, ret);
461 /*************************************************************************
462 * SHFree [SHELL32.195]
465 * exported by ordinal
467 void WINAPI SHFree(LPVOID pv)
472 if (!ShellTaskAllocator) SHGetMalloc(&ppv);
473 IMalloc_Free(ShellTaskAllocator, pv);
476 /*************************************************************************
477 * SHGetDesktopFolder [SHELL32.@]
479 HRESULT WINAPI SHGetDesktopFolder(IShellFolder **psf)
484 if(!psf) return E_INVALIDARG;
486 hres = ISF_Desktop_Constructor(NULL, &IID_IShellFolder,(LPVOID*)psf);
488 TRACE("-- %p->(%p)\n",psf, *psf);
491 /**************************************************************************
492 * Default ClassFactory Implementation
494 * SHCreateDefClassObject
497 * Helper function for dlls without their own classfactory.
498 * A generic classfactory is returned.
499 * When the CreateInstance of the cf is called the callback is executed.
504 const IClassFactoryVtbl *lpVtbl;
507 LPFNCREATEINSTANCE lpfnCI;
508 const IID * riidInst;
509 LONG * pcRefDll; /* pointer to refcounter in external dll (ugrrr...) */
512 static const IClassFactoryVtbl dclfvt;
514 /**************************************************************************
515 * IDefClF_fnConstructor
518 IClassFactory * IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI, PLONG pcRefDll, REFIID riidInst)
522 lpclf = HeapAlloc(GetProcessHeap(),0,sizeof(IDefClFImpl));
524 lpclf->lpVtbl = &dclfvt;
525 lpclf->lpfnCI = lpfnCI;
526 lpclf->pcRefDll = pcRefDll;
528 if (pcRefDll) InterlockedIncrement(pcRefDll);
529 lpclf->riidInst = riidInst;
531 TRACE("(%p)%s\n",lpclf, shdebugstr_guid(riidInst));
532 return (LPCLASSFACTORY)lpclf;
534 /**************************************************************************
535 * IDefClF_fnQueryInterface
537 static HRESULT WINAPI IDefClF_fnQueryInterface(
538 LPCLASSFACTORY iface, REFIID riid, LPVOID *ppvObj)
540 IDefClFImpl *This = (IDefClFImpl *)iface;
542 TRACE("(%p)->(%s)\n",This,shdebugstr_guid(riid));
546 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory)) {
548 InterlockedIncrement(&This->ref);
552 TRACE("-- E_NOINTERFACE\n");
553 return E_NOINTERFACE;
555 /******************************************************************************
558 static ULONG WINAPI IDefClF_fnAddRef(LPCLASSFACTORY iface)
560 IDefClFImpl *This = (IDefClFImpl *)iface;
561 ULONG refCount = InterlockedIncrement(&This->ref);
563 TRACE("(%p)->(count=%lu)\n", This, refCount - 1);
567 /******************************************************************************
570 static ULONG WINAPI IDefClF_fnRelease(LPCLASSFACTORY iface)
572 IDefClFImpl *This = (IDefClFImpl *)iface;
573 ULONG refCount = InterlockedDecrement(&This->ref);
575 TRACE("(%p)->(count=%lu)\n", This, refCount + 1);
579 if (This->pcRefDll) InterlockedDecrement(This->pcRefDll);
581 TRACE("-- destroying IClassFactory(%p)\n",This);
582 HeapFree(GetProcessHeap(),0,This);
587 /******************************************************************************
588 * IDefClF_fnCreateInstance
590 static HRESULT WINAPI IDefClF_fnCreateInstance(
591 LPCLASSFACTORY iface, LPUNKNOWN pUnkOuter, REFIID riid, LPVOID *ppvObject)
593 IDefClFImpl *This = (IDefClFImpl *)iface;
595 TRACE("%p->(%p,%s,%p)\n",This,pUnkOuter,shdebugstr_guid(riid),ppvObject);
599 if ( This->riidInst==NULL ||
600 IsEqualCLSID(riid, This->riidInst) ||
601 IsEqualCLSID(riid, &IID_IUnknown) )
603 return This->lpfnCI(pUnkOuter, riid, ppvObject);
606 ERR("unknown IID requested %s\n",shdebugstr_guid(riid));
607 return E_NOINTERFACE;
609 /******************************************************************************
610 * IDefClF_fnLockServer
612 static HRESULT WINAPI IDefClF_fnLockServer(LPCLASSFACTORY iface, BOOL fLock)
614 IDefClFImpl *This = (IDefClFImpl *)iface;
615 TRACE("%p->(0x%x), not implemented\n",This, fLock);
619 static const IClassFactoryVtbl dclfvt =
621 IDefClF_fnQueryInterface,
624 IDefClF_fnCreateInstance,
628 /******************************************************************************
629 * SHCreateDefClassObject [SHELL32.70]
631 HRESULT WINAPI SHCreateDefClassObject(
634 LPFNCREATEINSTANCE lpfnCI, /* [in] create instance callback entry */
635 LPDWORD pcRefDll, /* [in/out] ref count of the dll */
636 REFIID riidInst) /* [in] optional interface to the instance */
640 TRACE("%s %p %p %p %s\n",
641 shdebugstr_guid(riid), ppv, lpfnCI, pcRefDll, shdebugstr_guid(riidInst));
643 if (! IsEqualCLSID(riid, &IID_IClassFactory) ) return E_NOINTERFACE;
644 if (! (pcf = IDefClF_fnConstructor(lpfnCI, pcRefDll, riidInst))) return E_OUTOFMEMORY;
649 /*************************************************************************
650 * DragAcceptFiles [SHELL32.@]
652 void WINAPI DragAcceptFiles(HWND hWnd, BOOL b)
656 if( !IsWindow(hWnd) ) return;
657 exstyle = GetWindowLongA(hWnd,GWL_EXSTYLE);
659 exstyle |= WS_EX_ACCEPTFILES;
661 exstyle &= ~WS_EX_ACCEPTFILES;
662 SetWindowLongA(hWnd,GWL_EXSTYLE,exstyle);
665 /*************************************************************************
666 * DragFinish [SHELL32.@]
668 void WINAPI DragFinish(HDROP h)
671 GlobalFree((HGLOBAL)h);
674 /*************************************************************************
675 * DragQueryPoint [SHELL32.@]
677 BOOL WINAPI DragQueryPoint(HDROP hDrop, POINT *p)
679 DROPFILES *lpDropFileStruct;
684 lpDropFileStruct = (DROPFILES *) GlobalLock(hDrop);
686 *p = lpDropFileStruct->pt;
687 bRet = lpDropFileStruct->fNC;
693 /*************************************************************************
694 * DragQueryFile [SHELL32.@]
695 * DragQueryFileA [SHELL32.@]
697 UINT WINAPI DragQueryFileA(
705 DROPFILES *lpDropFileStruct = (DROPFILES *) GlobalLock(hDrop);
707 TRACE("(%p, %x, %p, %u)\n", hDrop,lFile,lpszFile,lLength);
709 if(!lpDropFileStruct) goto end;
711 lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->pFiles;
713 if(lpDropFileStruct->fWide) {
714 LPWSTR lpszFileW = NULL;
717 lpszFileW = HeapAlloc(GetProcessHeap(), 0, lLength*sizeof(WCHAR));
718 if(lpszFileW == NULL) {
722 i = DragQueryFileW(hDrop, lFile, lpszFileW, lLength);
725 WideCharToMultiByte(CP_ACP, 0, lpszFileW, -1, lpszFile, lLength, 0, NULL);
726 HeapFree(GetProcessHeap(), 0, lpszFileW);
733 while (*lpDrop++); /* skip filename */
736 i = (lFile == 0xFFFFFFFF) ? i : 0;
743 if (!lpszFile ) goto end; /* needed buffer size */
744 i = (lLength > i) ? i : lLength;
745 lstrcpynA (lpszFile, lpDrop, i);
751 /*************************************************************************
752 * DragQueryFileW [SHELL32.@]
754 UINT WINAPI DragQueryFileW(
762 DROPFILES *lpDropFileStruct = (DROPFILES *) GlobalLock(hDrop);
764 TRACE("(%p, %x, %p, %u)\n", hDrop,lFile,lpszwFile,lLength);
766 if(!lpDropFileStruct) goto end;
768 lpwDrop = (LPWSTR) ((LPSTR)lpDropFileStruct + lpDropFileStruct->pFiles);
770 if(lpDropFileStruct->fWide == FALSE) {
771 LPSTR lpszFileA = NULL;
774 lpszFileA = HeapAlloc(GetProcessHeap(), 0, lLength);
775 if(lpszFileA == NULL) {
779 i = DragQueryFileA(hDrop, lFile, lpszFileA, lLength);
782 MultiByteToWideChar(CP_ACP, 0, lpszFileA, -1, lpszwFile, lLength);
783 HeapFree(GetProcessHeap(), 0, lpszFileA);
791 while (*lpwDrop++); /* skip filename */
794 i = (lFile == 0xFFFFFFFF) ? i : 0;
799 i = strlenW(lpwDrop);
801 if ( !lpszwFile) goto end; /* needed buffer size */
803 i = (lLength > i) ? i : lLength;
804 lstrcpynW (lpszwFile, lpwDrop, i);