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
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 /**************************************************************************
53 * Default ClassFactory types
55 typedef HRESULT (CALLBACK *LPFNCREATEINSTANCE)(IUnknown* pUnkOuter, REFIID riid, LPVOID* ppvObject);
56 static IClassFactory * IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI, PLONG pcRefDll, REFIID riidInst);
58 /* this table contains all CLSID's of shell32 objects */
61 LPFNCREATEINSTANCE lpfnCI;
62 } InterfaceTable[] = {
63 {&CLSID_ShellFSFolder, IFSFolder_Constructor},
64 {&CLSID_MyComputer, ISF_MyComputer_Constructor},
65 {&CLSID_NetworkPlaces, ISF_NetworkPlaces_Constructor},
66 {&CLSID_ShellDesktop, ISF_Desktop_Constructor},
67 {&CLSID_ShellLink, IShellLink_Constructor},
68 {&CLSID_DragDropHelper, IDropTargetHelper_Constructor},
69 {&CLSID_ControlPanel, IControlPanel_Constructor},
70 {&CLSID_AutoComplete, IAutoComplete_Constructor},
71 {&CLSID_UnixFolder, UnixFolder_Constructor},
72 {&CLSID_UnixDosFolder, UnixDosFolder_Constructor},
73 {&CLSID_FolderShortcut, FolderShortcut_Constructor},
74 {&CLSID_MyDocuments, MyDocuments_Constructor},
75 {&CLSID_RecycleBin, RecycleBin_Constructor},
80 /* FIXME: this should be SHLWAPI.24 since we can't yet import by ordinal */
82 DWORD WINAPI __SHGUIDToStringW (REFGUID guid, LPWSTR str)
84 WCHAR sFormat[52] = {'{','%','0','8','l','x','-','%','0','4',
85 'x','-','%','0','4','x','-','%','0','2',
86 'x','%','0','2','x','-','%','0','2','x',
87 '%','0','2','x','%','0','2','x','%','0',
88 '2','x','%','0','2','x','%','0','2','x',
91 return wsprintfW ( str, sFormat,
92 guid->Data1, guid->Data2, guid->Data3,
93 guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
94 guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7] );
98 /*************************************************************************
99 * SHCoCreateInstance [SHELL32.102]
101 * Equivalent to CoCreateInstance. Under Windows 9x this function could sometimes
102 * use the shell32 built-in "mini-COM" without the need to load ole32.dll - see
103 * SHLoadOLE for details.
105 * Under wine if a "LoadWithoutCOM" value is present or the object resides in
106 * shell32.dll the function will load the object manually without the help of ole32
109 * exported by ordinal
112 * CoCreateInstace, SHLoadOLE
114 HRESULT WINAPI SHCoCreateInstance(
123 const CLSID * myclsid = clsid;
124 WCHAR sKeyName[MAX_PATH];
125 const WCHAR sCLSID[7] = {'C','L','S','I','D','\\','\0'};
127 const WCHAR sInProcServer32[16] ={'\\','I','n','p','r','o','c','S','e','r','v','e','r','3','2','\0'};
128 const WCHAR sLoadWithoutCOM[15] ={'L','o','a','d','W','i','t','h','o','u','t','C','O','M','\0'};
129 WCHAR sDllPath[MAX_PATH];
132 IClassFactory * pcf = NULL;
134 if(!ppv) return E_POINTER;
137 /* if the clsid is a string, convert it */
140 if (!aclsid) return REGDB_E_CLASSNOTREG;
141 SHCLSIDFromStringW(aclsid, &iid);
145 TRACE("(%p,%s,unk:%p,%s,%p)\n",
146 aclsid,shdebugstr_guid(myclsid),pUnkOuter,shdebugstr_guid(refiid),ppv);
148 if (SUCCEEDED(DllGetClassObject(myclsid, &IID_IClassFactory,(LPVOID*)&pcf)))
150 hres = IClassFactory_CreateInstance(pcf, pUnkOuter, refiid, ppv);
151 IClassFactory_Release(pcf);
155 /* we look up the dll path in the registry */
156 __SHGUIDToStringW(myclsid, sClassID);
157 lstrcpyW(sKeyName, sCLSID);
158 lstrcatW(sKeyName, sClassID);
159 lstrcatW(sKeyName, sInProcServer32);
161 if (RegOpenKeyExW(HKEY_CLASSES_ROOT, sKeyName, 0, KEY_READ, &hKey))
162 return E_ACCESSDENIED;
164 /* if a special registry key is set, we load a shell extension without help of OLE32 */
165 if (!SHQueryValueExW(hKey, sLoadWithoutCOM, 0, 0, 0, 0))
167 /* load an external dll without ole32 */
169 typedef HRESULT (CALLBACK *DllGetClassObjectFunc)(REFCLSID clsid, REFIID iid, LPVOID *ppv);
170 DllGetClassObjectFunc DllGetClassObject;
172 dwSize = sizeof(sDllPath);
173 SHQueryValueExW(hKey, NULL, 0,0, sDllPath, &dwSize );
175 if ((hLibrary = LoadLibraryExW(sDllPath, 0, LOAD_WITH_ALTERED_SEARCH_PATH)) == 0) {
176 ERR("couldn't load InprocServer32 dll %s\n", debugstr_w(sDllPath));
177 hres = E_ACCESSDENIED;
179 } else if (!(DllGetClassObject = (DllGetClassObjectFunc)GetProcAddress(hLibrary, "DllGetClassObject"))) {
180 ERR("couldn't find function DllGetClassObject in %s\n", debugstr_w(sDllPath));
181 FreeLibrary( hLibrary );
182 hres = E_ACCESSDENIED;
184 } else if (FAILED(hres = DllGetClassObject(myclsid, &IID_IClassFactory, (LPVOID*)&pcf))) {
185 TRACE("GetClassObject failed 0x%08x\n", hres);
189 hres = IClassFactory_CreateInstance(pcf, pUnkOuter, refiid, ppv);
190 IClassFactory_Release(pcf);
193 /* load an external dll in the usual way */
194 hres = CoCreateInstance(myclsid, pUnkOuter, CLSCTX_INPROC_SERVER, refiid, ppv);
198 if (hKey) RegCloseKey(hKey);
201 ERR("failed (0x%08x) to create CLSID:%s IID:%s\n",
202 hres, shdebugstr_guid(myclsid), shdebugstr_guid(refiid));
203 ERR("class not found in registry\n");
206 TRACE("-- instance: %p\n",*ppv);
210 /*************************************************************************
211 * DllGetClassObject [SHELL32.@]
212 * SHDllGetClassObject [SHELL32.128]
214 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
216 HRESULT hres = E_OUTOFMEMORY;
217 IClassFactory * pcf = NULL;
220 TRACE("CLSID:%s,IID:%s\n",shdebugstr_guid(rclsid),shdebugstr_guid(iid));
222 if (!ppv) return E_INVALIDARG;
225 /* search our internal interface table */
226 for(i=0;InterfaceTable[i].riid;i++) {
227 if(IsEqualIID(InterfaceTable[i].riid, rclsid)) {
228 TRACE("index[%u]\n", i);
229 pcf = IDefClF_fnConstructor(InterfaceTable[i].lpfnCI, NULL, NULL);
234 FIXME("failed for CLSID=%s\n", shdebugstr_guid(rclsid));
235 return CLASS_E_CLASSNOTAVAILABLE;
238 hres = IClassFactory_QueryInterface(pcf, iid, ppv);
239 IClassFactory_Release(pcf);
241 TRACE("-- pointer to class factory: %p\n",*ppv);
245 /*************************************************************************
246 * SHCLSIDFromString [SHELL32.147]
248 * Under Windows 9x this was an ANSI version of CLSIDFromString. It also allowed
249 * to avoid dependency on ole32.dll (see SHLoadOLE for details).
251 * Under Windows NT/2000/XP this is equivalent to CLSIDFromString
254 * exported by ordinal
257 * CLSIDFromString, SHLoadOLE
259 DWORD WINAPI SHCLSIDFromStringA (LPCSTR clsid, CLSID *id)
262 TRACE("(%p(%s) %p)\n", clsid, clsid, id);
263 if (!MultiByteToWideChar( CP_ACP, 0, clsid, -1, buffer, sizeof(buffer)/sizeof(WCHAR) ))
264 return CO_E_CLASSSTRING;
265 return CLSIDFromString( buffer, id );
267 DWORD WINAPI SHCLSIDFromStringW (LPCWSTR clsid, CLSID *id)
269 TRACE("(%p(%s) %p)\n", clsid, debugstr_w(clsid), id);
270 return CLSIDFromString((LPWSTR)clsid, id);
272 DWORD WINAPI SHCLSIDFromStringAW (LPCVOID clsid, CLSID *id)
274 if (SHELL_OsIsUnicode())
275 return SHCLSIDFromStringW (clsid, id);
276 return SHCLSIDFromStringA (clsid, id);
279 /*************************************************************************
280 * SHGetMalloc [SHELL32.@]
282 * Equivalent to CoGetMalloc(MEMCTX_TASK, ...). Under Windows 9x this function
283 * could use the shell32 built-in "mini-COM" without the need to load ole32.dll -
284 * see SHLoadOLE for details.
287 * lpmal [O] Destination for IMalloc interface.
290 * Success: S_OK. lpmal contains the shells IMalloc interface.
291 * Failure. An HRESULT error code.
294 * CoGetMalloc, SHLoadOLE
296 HRESULT WINAPI SHGetMalloc(LPMALLOC *lpmal)
298 TRACE("(%p)\n", lpmal);
299 return CoGetMalloc(MEMCTX_TASK, lpmal);
302 /*************************************************************************
303 * SHAlloc [SHELL32.196]
305 * Equivalent to CoTaskMemAlloc. Under Windows 9x this function could use
306 * the shell32 built-in "mini-COM" without the need to load ole32.dll -
307 * see SHLoadOLE for details.
310 * exported by ordinal
313 * CoTaskMemAlloc, SHLoadOLE
315 LPVOID WINAPI SHAlloc(DWORD len)
319 ret = CoTaskMemAlloc(len);
320 TRACE("%u bytes at %p\n",len, ret);
324 /*************************************************************************
325 * SHFree [SHELL32.195]
327 * Equivalent to CoTaskMemFree. Under Windows 9x this function could use
328 * the shell32 built-in "mini-COM" without the need to load ole32.dll -
329 * see SHLoadOLE for details.
332 * exported by ordinal
335 * CoTaskMemFree, SHLoadOLE
337 void WINAPI SHFree(LPVOID pv)
343 /*************************************************************************
344 * SHGetDesktopFolder [SHELL32.@]
346 HRESULT WINAPI SHGetDesktopFolder(IShellFolder **psf)
351 if(!psf) return E_INVALIDARG;
353 hres = ISF_Desktop_Constructor(NULL, &IID_IShellFolder,(LPVOID*)psf);
355 TRACE("-- %p->(%p)\n",psf, *psf);
358 /**************************************************************************
359 * Default ClassFactory Implementation
361 * SHCreateDefClassObject
364 * Helper function for dlls without their own classfactory.
365 * A generic classfactory is returned.
366 * When the CreateInstance of the cf is called the callback is executed.
371 const IClassFactoryVtbl *lpVtbl;
374 LPFNCREATEINSTANCE lpfnCI;
375 const IID * riidInst;
376 LONG * pcRefDll; /* pointer to refcounter in external dll (ugrrr...) */
379 static const IClassFactoryVtbl dclfvt;
381 /**************************************************************************
382 * IDefClF_fnConstructor
385 static IClassFactory * IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI, PLONG pcRefDll, REFIID riidInst)
389 lpclf = HeapAlloc(GetProcessHeap(),0,sizeof(IDefClFImpl));
391 lpclf->lpVtbl = &dclfvt;
392 lpclf->lpfnCI = lpfnCI;
393 lpclf->pcRefDll = pcRefDll;
395 if (pcRefDll) InterlockedIncrement(pcRefDll);
396 lpclf->riidInst = riidInst;
398 TRACE("(%p)%s\n",lpclf, shdebugstr_guid(riidInst));
399 return (LPCLASSFACTORY)lpclf;
401 /**************************************************************************
402 * IDefClF_fnQueryInterface
404 static HRESULT WINAPI IDefClF_fnQueryInterface(
405 LPCLASSFACTORY iface, REFIID riid, LPVOID *ppvObj)
407 IDefClFImpl *This = (IDefClFImpl *)iface;
409 TRACE("(%p)->(%s)\n",This,shdebugstr_guid(riid));
413 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory)) {
415 InterlockedIncrement(&This->ref);
419 TRACE("-- E_NOINTERFACE\n");
420 return E_NOINTERFACE;
422 /******************************************************************************
425 static ULONG WINAPI IDefClF_fnAddRef(LPCLASSFACTORY iface)
427 IDefClFImpl *This = (IDefClFImpl *)iface;
428 ULONG refCount = InterlockedIncrement(&This->ref);
430 TRACE("(%p)->(count=%u)\n", This, refCount - 1);
434 /******************************************************************************
437 static ULONG WINAPI IDefClF_fnRelease(LPCLASSFACTORY iface)
439 IDefClFImpl *This = (IDefClFImpl *)iface;
440 ULONG refCount = InterlockedDecrement(&This->ref);
442 TRACE("(%p)->(count=%u)\n", This, refCount + 1);
446 if (This->pcRefDll) InterlockedDecrement(This->pcRefDll);
448 TRACE("-- destroying IClassFactory(%p)\n",This);
449 HeapFree(GetProcessHeap(),0,This);
454 /******************************************************************************
455 * IDefClF_fnCreateInstance
457 static HRESULT WINAPI IDefClF_fnCreateInstance(
458 LPCLASSFACTORY iface, LPUNKNOWN pUnkOuter, REFIID riid, LPVOID *ppvObject)
460 IDefClFImpl *This = (IDefClFImpl *)iface;
462 TRACE("%p->(%p,%s,%p)\n",This,pUnkOuter,shdebugstr_guid(riid),ppvObject);
466 if ( This->riidInst==NULL ||
467 IsEqualCLSID(riid, This->riidInst) ||
468 IsEqualCLSID(riid, &IID_IUnknown) )
470 return This->lpfnCI(pUnkOuter, riid, ppvObject);
473 ERR("unknown IID requested %s\n",shdebugstr_guid(riid));
474 return E_NOINTERFACE;
476 /******************************************************************************
477 * IDefClF_fnLockServer
479 static HRESULT WINAPI IDefClF_fnLockServer(LPCLASSFACTORY iface, BOOL fLock)
481 IDefClFImpl *This = (IDefClFImpl *)iface;
482 TRACE("%p->(0x%x), not implemented\n",This, fLock);
486 static const IClassFactoryVtbl dclfvt =
488 IDefClF_fnQueryInterface,
491 IDefClF_fnCreateInstance,
495 /******************************************************************************
496 * SHCreateDefClassObject [SHELL32.70]
498 HRESULT WINAPI SHCreateDefClassObject(
501 LPFNCREATEINSTANCE lpfnCI, /* [in] create instance callback entry */
502 LPDWORD pcRefDll, /* [in/out] ref count of the dll */
503 REFIID riidInst) /* [in] optional interface to the instance */
507 TRACE("%s %p %p %p %s\n",
508 shdebugstr_guid(riid), ppv, lpfnCI, pcRefDll, shdebugstr_guid(riidInst));
510 if (! IsEqualCLSID(riid, &IID_IClassFactory) ) return E_NOINTERFACE;
511 if (! (pcf = IDefClF_fnConstructor(lpfnCI, (PLONG)pcRefDll, riidInst))) return E_OUTOFMEMORY;
516 /*************************************************************************
517 * DragAcceptFiles [SHELL32.@]
519 void WINAPI DragAcceptFiles(HWND hWnd, BOOL b)
523 if( !IsWindow(hWnd) ) return;
524 exstyle = GetWindowLongA(hWnd,GWL_EXSTYLE);
526 exstyle |= WS_EX_ACCEPTFILES;
528 exstyle &= ~WS_EX_ACCEPTFILES;
529 SetWindowLongA(hWnd,GWL_EXSTYLE,exstyle);
532 /*************************************************************************
533 * DragFinish [SHELL32.@]
535 void WINAPI DragFinish(HDROP h)
538 GlobalFree((HGLOBAL)h);
541 /*************************************************************************
542 * DragQueryPoint [SHELL32.@]
544 BOOL WINAPI DragQueryPoint(HDROP hDrop, POINT *p)
546 DROPFILES *lpDropFileStruct;
551 lpDropFileStruct = (DROPFILES *) GlobalLock(hDrop);
553 *p = lpDropFileStruct->pt;
554 bRet = lpDropFileStruct->fNC;
560 /*************************************************************************
561 * DragQueryFileA [SHELL32.@]
562 * DragQueryFile [SHELL32.@]
564 UINT WINAPI DragQueryFileA(
572 DROPFILES *lpDropFileStruct = (DROPFILES *) GlobalLock(hDrop);
574 TRACE("(%p, %x, %p, %u)\n", hDrop,lFile,lpszFile,lLength);
576 if(!lpDropFileStruct) goto end;
578 lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->pFiles;
580 if(lpDropFileStruct->fWide) {
581 LPWSTR lpszFileW = NULL;
584 lpszFileW = HeapAlloc(GetProcessHeap(), 0, lLength*sizeof(WCHAR));
585 if(lpszFileW == NULL) {
589 i = DragQueryFileW(hDrop, lFile, lpszFileW, lLength);
592 WideCharToMultiByte(CP_ACP, 0, lpszFileW, -1, lpszFile, lLength, 0, NULL);
593 HeapFree(GetProcessHeap(), 0, lpszFileW);
600 while (*lpDrop++); /* skip filename */
603 i = (lFile == 0xFFFFFFFF) ? i : 0;
609 if (!lpszFile ) goto end; /* needed buffer size */
610 lstrcpynA (lpszFile, lpDrop, lLength);
616 /*************************************************************************
617 * DragQueryFileW [SHELL32.@]
619 UINT WINAPI DragQueryFileW(
627 DROPFILES *lpDropFileStruct = (DROPFILES *) GlobalLock(hDrop);
629 TRACE("(%p, %x, %p, %u)\n", hDrop,lFile,lpszwFile,lLength);
631 if(!lpDropFileStruct) goto end;
633 lpwDrop = (LPWSTR) ((LPSTR)lpDropFileStruct + lpDropFileStruct->pFiles);
635 if(lpDropFileStruct->fWide == FALSE) {
636 LPSTR lpszFileA = NULL;
639 lpszFileA = HeapAlloc(GetProcessHeap(), 0, lLength);
640 if(lpszFileA == NULL) {
644 i = DragQueryFileA(hDrop, lFile, lpszFileA, lLength);
647 MultiByteToWideChar(CP_ACP, 0, lpszFileA, -1, lpszwFile, lLength);
648 HeapFree(GetProcessHeap(), 0, lpszFileA);
656 while (*lpwDrop++); /* skip filename */
659 i = (lFile == 0xFFFFFFFF) ? i : 0;
664 i = strlenW(lpwDrop);
665 if ( !lpszwFile) goto end; /* needed buffer size */
666 lstrcpynW (lpszwFile, lpwDrop, lLength);