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},
80 /*************************************************************************
81 * SHCoCreateInstance [SHELL32.102]
87 /* FIXME: this should be SHLWAPI.24 since we can't yet import by ordinal */
89 DWORD WINAPI __SHGUIDToStringW (REFGUID guid, LPWSTR str)
91 WCHAR sFormat[52] = {'{','%','0','8','l','x','-','%','0','4',
92 'x','-','%','0','4','x','-','%','0','2',
93 'x','%','0','2','x','-','%','0','2','x',
94 '%','0','2','x','%','0','2','x','%','0',
95 '2','x','%','0','2','x','%','0','2','x',
98 return wsprintfW ( str, sFormat,
99 guid->Data1, guid->Data2, guid->Data3,
100 guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
101 guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7] );
105 /************************************************************************/
107 HRESULT WINAPI SHCoCreateInstance(
116 CLSID * myclsid = (CLSID*)clsid;
117 WCHAR sKeyName[MAX_PATH];
118 const WCHAR sCLSID[7] = {'C','L','S','I','D','\\','\0'};
120 const WCHAR sInProcServer32[16] ={'\\','I','n','p','r','o','c','S','e','r','v','e','r','3','2','\0'};
121 const WCHAR sLoadWithoutCOM[15] ={'L','o','a','d','W','i','t','h','o','u','t','C','O','M','\0'};
122 WCHAR sDllPath[MAX_PATH];
125 BOOLEAN bLoadFromShell32 = FALSE;
126 BOOLEAN bLoadWithoutCOM = FALSE;
127 IClassFactory * pcf = NULL;
129 if(!ppv) return E_POINTER;
132 /* if the clsid is a string, convert it */
135 if (!aclsid) return REGDB_E_CLASSNOTREG;
136 SHCLSIDFromStringW(aclsid, &iid);
140 TRACE("(%p,%s,unk:%p,%s,%p)\n",
141 aclsid,shdebugstr_guid(myclsid),pUnkOuter,shdebugstr_guid(refiid),ppv);
143 /* we look up the dll path in the registry */
144 __SHGUIDToStringW(myclsid, sClassID);
145 lstrcpyW(sKeyName, sCLSID);
146 lstrcatW(sKeyName, sClassID);
147 lstrcatW(sKeyName, sInProcServer32);
149 if (ERROR_SUCCESS == RegOpenKeyExW(HKEY_CLASSES_ROOT, sKeyName, 0, KEY_READ, &hKey)) {
150 dwSize = sizeof(sDllPath);
151 SHQueryValueExW(hKey, NULL, 0,0, sDllPath, &dwSize );
153 /* if a special registry key is set, we load a shell extension without help of OLE32 */
154 bLoadWithoutCOM = (ERROR_SUCCESS == SHQueryValueExW(hKey, sLoadWithoutCOM, 0, 0, 0, 0));
156 /* if the com object is inside shell32, omit use of ole32 */
157 bLoadFromShell32 = (0==lstrcmpiW( PathFindFileNameW(sDllPath), sShell32));
161 /* since we can't find it in the registry we try internally */
162 bLoadFromShell32 = TRUE;
165 TRACE("WithoutCom=%u FromShell=%u\n", bLoadWithoutCOM, bLoadFromShell32);
167 /* now we create an instance */
168 if (bLoadFromShell32) {
169 if (! SUCCEEDED(DllGetClassObject(myclsid, &IID_IClassFactory,(LPVOID*)&pcf))) {
170 ERR("LoadFromShell failed for CLSID=%s\n", shdebugstr_guid(myclsid));
172 } else if (bLoadWithoutCOM) {
174 /* load an external dll without ole32 */
176 typedef HRESULT (CALLBACK *DllGetClassObjectFunc)(REFCLSID clsid, REFIID iid, LPVOID *ppv);
177 DllGetClassObjectFunc DllGetClassObject;
179 if ((hLibrary = LoadLibraryExW(sDllPath, 0, LOAD_WITH_ALTERED_SEARCH_PATH)) == 0) {
180 ERR("couldn't load InprocServer32 dll %s\n", debugstr_w(sDllPath));
181 hres = E_ACCESSDENIED;
183 } else if (!(DllGetClassObject = (DllGetClassObjectFunc)GetProcAddress(hLibrary, "DllGetClassObject"))) {
184 ERR("couldn't find function DllGetClassObject in %s\n", debugstr_w(sDllPath));
185 FreeLibrary( hLibrary );
186 hres = E_ACCESSDENIED;
188 } else if (! SUCCEEDED(hres = DllGetClassObject(myclsid, &IID_IClassFactory, (LPVOID*)&pcf))) {
189 TRACE("GetClassObject failed 0x%08lx\n", hres);
195 /* load an external dll in the usual way */
196 hres = CoCreateInstance(myclsid, pUnkOuter, CLSCTX_INPROC_SERVER, refiid, ppv);
200 /* here we should have a ClassFactory */
201 if (!pcf) return E_ACCESSDENIED;
203 hres = IClassFactory_CreateInstance(pcf, pUnkOuter, refiid, ppv);
204 IClassFactory_Release(pcf);
208 ERR("failed (0x%08lx) to create CLSID:%s IID:%s\n",
209 hres, shdebugstr_guid(myclsid), shdebugstr_guid(refiid));
210 ERR("class not found in registry\n");
213 TRACE("-- instance: %p\n",*ppv);
217 /*************************************************************************
218 * DllGetClassObject [SHELL32.@]
220 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
222 HRESULT hres = E_OUTOFMEMORY;
223 IClassFactory * pcf = NULL;
226 TRACE("CLSID:%s,IID:%s\n",shdebugstr_guid(rclsid),shdebugstr_guid(iid));
228 if (!ppv) return E_INVALIDARG;
231 /* search our internal interface table */
232 for(i=0;InterfaceTable[i].riid;i++) {
233 if(IsEqualIID(InterfaceTable[i].riid, rclsid)) {
234 TRACE("index[%u]\n", i);
235 pcf = IDefClF_fnConstructor(InterfaceTable[i].lpfnCI, NULL, NULL);
240 FIXME("failed for CLSID=%s\n", shdebugstr_guid(rclsid));
241 return CLASS_E_CLASSNOTAVAILABLE;
244 hres = IClassFactory_QueryInterface(pcf, iid, ppv);
245 IClassFactory_Release(pcf);
247 TRACE("-- pointer to class factory: %p\n",*ppv);
251 /*************************************************************************
252 * SHCLSIDFromString [SHELL32.147]
255 * exported by ordinal
257 DWORD WINAPI SHCLSIDFromStringA (LPCSTR clsid, CLSID *id)
260 TRACE("(%p(%s) %p)\n", clsid, clsid, id);
261 if (!MultiByteToWideChar( CP_ACP, 0, clsid, -1, buffer, sizeof(buffer)/sizeof(WCHAR) ))
262 return CO_E_CLASSSTRING;
263 return CLSIDFromString( buffer, id );
265 DWORD WINAPI SHCLSIDFromStringW (LPCWSTR clsid, CLSID *id)
267 TRACE("(%p(%s) %p)\n", clsid, debugstr_w(clsid), id);
268 return CLSIDFromString((LPWSTR)clsid, id);
270 DWORD WINAPI SHCLSIDFromStringAW (LPVOID clsid, CLSID *id)
272 if (SHELL_OsIsUnicode())
273 return SHCLSIDFromStringW (clsid, id);
274 return SHCLSIDFromStringA (clsid, id);
277 /*************************************************************************
278 * Shell Memory Allocator
281 /* set the vtable later */
282 static const IMallocVtbl VT_Shell_IMalloc32;
284 /* this is the static object instance */
286 const IMallocVtbl *lpVtbl;
290 static _ShellMalloc Shell_Malloc = { &VT_Shell_IMalloc32,1};
292 /* this is the global allocator of shell32 */
293 static IMalloc * ShellTaskAllocator = NULL;
295 /******************************************************************************
296 * IShellMalloc_QueryInterface [VTABLE]
298 static HRESULT WINAPI IShellMalloc_fnQueryInterface(LPMALLOC iface, REFIID refiid, LPVOID *obj)
300 TRACE("(%s,%p)\n",shdebugstr_guid(refiid),obj);
301 if (IsEqualIID(refiid, &IID_IUnknown) || IsEqualIID(refiid, &IID_IMalloc)) {
302 *obj = (LPMALLOC) &Shell_Malloc;
305 return E_NOINTERFACE;
308 /******************************************************************************
309 * IShellMalloc_AddRefRelease [VTABLE]
311 static ULONG WINAPI IShellMalloc_fnAddRefRelease(LPMALLOC iface)
316 /******************************************************************************
317 * IShellMalloc_Alloc [VTABLE]
319 static LPVOID WINAPI IShellMalloc_fnAlloc(LPMALLOC iface, DWORD cb)
323 addr = (LPVOID) LocalAlloc(LMEM_ZEROINIT, cb);
324 TRACE("(%p,%ld);\n",addr,cb);
328 /******************************************************************************
329 * IShellMalloc_Realloc [VTABLE]
331 static LPVOID WINAPI IShellMalloc_fnRealloc(LPMALLOC iface, LPVOID pv, DWORD cb)
337 addr = (LPVOID) LocalReAlloc((HANDLE) pv, cb, LMEM_ZEROINIT | LMEM_MOVEABLE);
339 LocalFree((HANDLE) pv);
344 addr = (LPVOID) LocalAlloc(LMEM_ZEROINIT, cb);
350 TRACE("(%p->%p,%ld)\n",pv,addr,cb);
354 /******************************************************************************
355 * IShellMalloc_Free [VTABLE]
357 static VOID WINAPI IShellMalloc_fnFree(LPMALLOC iface, LPVOID pv)
360 LocalFree((HANDLE) pv);
363 /******************************************************************************
364 * IShellMalloc_GetSize [VTABLE]
366 static DWORD WINAPI IShellMalloc_fnGetSize(LPMALLOC iface, LPVOID pv)
368 DWORD cb = (DWORD) LocalSize((HANDLE)pv);
369 TRACE("(%p,%ld)\n", pv, cb);
373 /******************************************************************************
374 * IShellMalloc_DidAlloc [VTABLE]
376 static INT WINAPI IShellMalloc_fnDidAlloc(LPMALLOC iface, LPVOID pv)
382 /******************************************************************************
383 * IShellMalloc_HeapMinimize [VTABLE]
385 static VOID WINAPI IShellMalloc_fnHeapMinimize(LPMALLOC iface)
390 static const IMallocVtbl VT_Shell_IMalloc32 =
392 IShellMalloc_fnQueryInterface,
393 IShellMalloc_fnAddRefRelease,
394 IShellMalloc_fnAddRefRelease,
395 IShellMalloc_fnAlloc,
396 IShellMalloc_fnRealloc,
398 IShellMalloc_fnGetSize,
399 IShellMalloc_fnDidAlloc,
400 IShellMalloc_fnHeapMinimize
403 /*************************************************************************
404 * SHGetMalloc [SHELL32.@]
406 * Return the shell IMalloc interface.
409 * lpmal [O] Destination for IMalloc interface.
412 * Success: S_OK. lpmal contains the shells IMalloc interface.
413 * Failure. An HRESULT error code.
416 * This function will use CoGetMalloc() if OLE32.DLL is already loaded.
417 * If not it uses an internal implementation as a fallback.
419 HRESULT WINAPI SHGetMalloc(LPMALLOC *lpmal)
421 TRACE("(%p)\n", lpmal);
423 if (!ShellTaskAllocator)
425 HMODULE hOle32 = GetModuleHandleA("OLE32.DLL");
426 /* this is very suspect. we should not being using a different
427 * allocator from deallocator based on something undeterministic
428 * like whether ole32 is loaded. as it happens currently, they
429 * both map to the same allocator deep down, but this could
430 * change in the future. */
432 CoGetMalloc(MEMCTX_TASK, &ShellTaskAllocator);
433 TRACE("got ole32 IMalloc\n");
435 if(!ShellTaskAllocator) {
436 ShellTaskAllocator = (IMalloc* ) &Shell_Malloc;
437 TRACE("use fallback allocator\n");
440 *lpmal = ShellTaskAllocator;
444 /*************************************************************************
445 * SHAlloc [SHELL32.196]
448 * exported by ordinal
450 LPVOID WINAPI SHAlloc(DWORD len)
455 if (!ShellTaskAllocator) SHGetMalloc(&ppv);
457 ret = (LPVOID) IMalloc_Alloc(ShellTaskAllocator, len);
458 TRACE("%lu bytes at %p\n",len, ret);
462 /*************************************************************************
463 * SHFree [SHELL32.195]
466 * exported by ordinal
468 void WINAPI SHFree(LPVOID pv)
473 if (!ShellTaskAllocator) SHGetMalloc(&ppv);
474 IMalloc_Free(ShellTaskAllocator, pv);
477 /*************************************************************************
478 * SHGetDesktopFolder [SHELL32.@]
480 HRESULT WINAPI SHGetDesktopFolder(IShellFolder **psf)
485 if(!psf) return E_INVALIDARG;
487 hres = ISF_Desktop_Constructor(NULL, &IID_IShellFolder,(LPVOID*)psf);
489 TRACE("-- %p->(%p)\n",psf, *psf);
492 /**************************************************************************
493 * Default ClassFactory Implementation
495 * SHCreateDefClassObject
498 * Helper function for dlls without their own classfactory.
499 * A generic classfactory is returned.
500 * When the CreateInstance of the cf is called the callback is executed.
505 const IClassFactoryVtbl *lpVtbl;
508 LPFNCREATEINSTANCE lpfnCI;
509 const IID * riidInst;
510 LONG * pcRefDll; /* pointer to refcounter in external dll (ugrrr...) */
513 static const IClassFactoryVtbl dclfvt;
515 /**************************************************************************
516 * IDefClF_fnConstructor
519 IClassFactory * IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI, PLONG pcRefDll, REFIID riidInst)
523 lpclf = HeapAlloc(GetProcessHeap(),0,sizeof(IDefClFImpl));
525 lpclf->lpVtbl = &dclfvt;
526 lpclf->lpfnCI = lpfnCI;
527 lpclf->pcRefDll = pcRefDll;
529 if (pcRefDll) InterlockedIncrement(pcRefDll);
530 lpclf->riidInst = riidInst;
532 TRACE("(%p)%s\n",lpclf, shdebugstr_guid(riidInst));
533 return (LPCLASSFACTORY)lpclf;
535 /**************************************************************************
536 * IDefClF_fnQueryInterface
538 static HRESULT WINAPI IDefClF_fnQueryInterface(
539 LPCLASSFACTORY iface, REFIID riid, LPVOID *ppvObj)
541 IDefClFImpl *This = (IDefClFImpl *)iface;
543 TRACE("(%p)->(%s)\n",This,shdebugstr_guid(riid));
547 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory)) {
549 InterlockedIncrement(&This->ref);
553 TRACE("-- E_NOINTERFACE\n");
554 return E_NOINTERFACE;
556 /******************************************************************************
559 static ULONG WINAPI IDefClF_fnAddRef(LPCLASSFACTORY iface)
561 IDefClFImpl *This = (IDefClFImpl *)iface;
562 ULONG refCount = InterlockedIncrement(&This->ref);
564 TRACE("(%p)->(count=%lu)\n", This, refCount - 1);
568 /******************************************************************************
571 static ULONG WINAPI IDefClF_fnRelease(LPCLASSFACTORY iface)
573 IDefClFImpl *This = (IDefClFImpl *)iface;
574 ULONG refCount = InterlockedDecrement(&This->ref);
576 TRACE("(%p)->(count=%lu)\n", This, refCount + 1);
580 if (This->pcRefDll) InterlockedDecrement(This->pcRefDll);
582 TRACE("-- destroying IClassFactory(%p)\n",This);
583 HeapFree(GetProcessHeap(),0,This);
588 /******************************************************************************
589 * IDefClF_fnCreateInstance
591 static HRESULT WINAPI IDefClF_fnCreateInstance(
592 LPCLASSFACTORY iface, LPUNKNOWN pUnkOuter, REFIID riid, LPVOID *ppvObject)
594 IDefClFImpl *This = (IDefClFImpl *)iface;
596 TRACE("%p->(%p,%s,%p)\n",This,pUnkOuter,shdebugstr_guid(riid),ppvObject);
600 if ( This->riidInst==NULL ||
601 IsEqualCLSID(riid, This->riidInst) ||
602 IsEqualCLSID(riid, &IID_IUnknown) )
604 return This->lpfnCI(pUnkOuter, riid, ppvObject);
607 ERR("unknown IID requested %s\n",shdebugstr_guid(riid));
608 return E_NOINTERFACE;
610 /******************************************************************************
611 * IDefClF_fnLockServer
613 static HRESULT WINAPI IDefClF_fnLockServer(LPCLASSFACTORY iface, BOOL fLock)
615 IDefClFImpl *This = (IDefClFImpl *)iface;
616 TRACE("%p->(0x%x), not implemented\n",This, fLock);
620 static const IClassFactoryVtbl dclfvt =
622 IDefClF_fnQueryInterface,
625 IDefClF_fnCreateInstance,
629 /******************************************************************************
630 * SHCreateDefClassObject [SHELL32.70]
632 HRESULT WINAPI SHCreateDefClassObject(
635 LPFNCREATEINSTANCE lpfnCI, /* [in] create instance callback entry */
636 LPDWORD pcRefDll, /* [in/out] ref count of the dll */
637 REFIID riidInst) /* [in] optional interface to the instance */
641 TRACE("%s %p %p %p %s\n",
642 shdebugstr_guid(riid), ppv, lpfnCI, pcRefDll, shdebugstr_guid(riidInst));
644 if (! IsEqualCLSID(riid, &IID_IClassFactory) ) return E_NOINTERFACE;
645 if (! (pcf = IDefClF_fnConstructor(lpfnCI, (PLONG)pcRefDll, riidInst))) return E_OUTOFMEMORY;
650 /*************************************************************************
651 * DragAcceptFiles [SHELL32.@]
653 void WINAPI DragAcceptFiles(HWND hWnd, BOOL b)
657 if( !IsWindow(hWnd) ) return;
658 exstyle = GetWindowLongA(hWnd,GWL_EXSTYLE);
660 exstyle |= WS_EX_ACCEPTFILES;
662 exstyle &= ~WS_EX_ACCEPTFILES;
663 SetWindowLongA(hWnd,GWL_EXSTYLE,exstyle);
666 /*************************************************************************
667 * DragFinish [SHELL32.@]
669 void WINAPI DragFinish(HDROP h)
672 GlobalFree((HGLOBAL)h);
675 /*************************************************************************
676 * DragQueryPoint [SHELL32.@]
678 BOOL WINAPI DragQueryPoint(HDROP hDrop, POINT *p)
680 DROPFILES *lpDropFileStruct;
685 lpDropFileStruct = (DROPFILES *) GlobalLock(hDrop);
687 *p = lpDropFileStruct->pt;
688 bRet = lpDropFileStruct->fNC;
694 /*************************************************************************
695 * DragQueryFile [SHELL32.@]
696 * DragQueryFileA [SHELL32.@]
698 UINT WINAPI DragQueryFileA(
706 DROPFILES *lpDropFileStruct = (DROPFILES *) GlobalLock(hDrop);
708 TRACE("(%p, %x, %p, %u)\n", hDrop,lFile,lpszFile,lLength);
710 if(!lpDropFileStruct) goto end;
712 lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->pFiles;
714 if(lpDropFileStruct->fWide) {
715 LPWSTR lpszFileW = NULL;
718 lpszFileW = HeapAlloc(GetProcessHeap(), 0, lLength*sizeof(WCHAR));
719 if(lpszFileW == NULL) {
723 i = DragQueryFileW(hDrop, lFile, lpszFileW, lLength);
726 WideCharToMultiByte(CP_ACP, 0, lpszFileW, -1, lpszFile, lLength, 0, NULL);
727 HeapFree(GetProcessHeap(), 0, lpszFileW);
734 while (*lpDrop++); /* skip filename */
737 i = (lFile == 0xFFFFFFFF) ? i : 0;
744 if (!lpszFile ) goto end; /* needed buffer size */
745 i = (lLength > i) ? i : lLength;
746 lstrcpynA (lpszFile, lpDrop, i);
752 /*************************************************************************
753 * DragQueryFileW [SHELL32.@]
755 UINT WINAPI DragQueryFileW(
763 DROPFILES *lpDropFileStruct = (DROPFILES *) GlobalLock(hDrop);
765 TRACE("(%p, %x, %p, %u)\n", hDrop,lFile,lpszwFile,lLength);
767 if(!lpDropFileStruct) goto end;
769 lpwDrop = (LPWSTR) ((LPSTR)lpDropFileStruct + lpDropFileStruct->pFiles);
771 if(lpDropFileStruct->fWide == FALSE) {
772 LPSTR lpszFileA = NULL;
775 lpszFileA = HeapAlloc(GetProcessHeap(), 0, lLength);
776 if(lpszFileA == NULL) {
780 i = DragQueryFileA(hDrop, lFile, lpszFileA, lLength);
783 MultiByteToWideChar(CP_ACP, 0, lpszFileA, -1, lpszwFile, lLength);
784 HeapFree(GetProcessHeap(), 0, lpszFileA);
792 while (*lpwDrop++); /* skip filename */
795 i = (lFile == 0xFFFFFFFF) ? i : 0;
800 i = strlenW(lpwDrop);
802 if ( !lpszwFile) goto end; /* needed buffer size */
804 i = (lLength > i) ? i : lLength;
805 lstrcpynW (lpszwFile, lpwDrop, i);