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 INT WINAPI SHStringFromGUIDW(REFGUID guid, LPWSTR lpszDest, INT cchMax); /* shlwapi.24 */
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_ShellItem, IShellItem_Constructor},
68 {&CLSID_ShellLink, IShellLink_Constructor},
69 {&CLSID_DragDropHelper, IDropTargetHelper_Constructor},
70 {&CLSID_ControlPanel, IControlPanel_Constructor},
71 {&CLSID_AutoComplete, IAutoComplete_Constructor},
72 {&CLSID_UnixFolder, UnixFolder_Constructor},
73 {&CLSID_UnixDosFolder, UnixDosFolder_Constructor},
74 {&CLSID_FolderShortcut, FolderShortcut_Constructor},
75 {&CLSID_MyDocuments, MyDocuments_Constructor},
76 {&CLSID_RecycleBin, RecycleBin_Constructor},
82 /*************************************************************************
83 * SHCoCreateInstance [SHELL32.102]
85 * Equivalent to CoCreateInstance. Under Windows 9x this function could sometimes
86 * use the shell32 built-in "mini-COM" without the need to load ole32.dll - see
87 * SHLoadOLE for details.
89 * Under wine if a "LoadWithoutCOM" value is present or the object resides in
90 * shell32.dll the function will load the object manually without the help of ole32
96 * CoCreateInstace, SHLoadOLE
98 HRESULT WINAPI SHCoCreateInstance(
107 const CLSID * myclsid = clsid;
108 WCHAR sKeyName[MAX_PATH];
109 const WCHAR sCLSID[7] = {'C','L','S','I','D','\\','\0'};
111 const WCHAR sInProcServer32[16] ={'\\','I','n','p','r','o','c','S','e','r','v','e','r','3','2','\0'};
112 const WCHAR sLoadWithoutCOM[15] ={'L','o','a','d','W','i','t','h','o','u','t','C','O','M','\0'};
113 WCHAR sDllPath[MAX_PATH];
116 IClassFactory * pcf = NULL;
118 if(!ppv) return E_POINTER;
121 /* if the clsid is a string, convert it */
124 if (!aclsid) return REGDB_E_CLASSNOTREG;
125 SHCLSIDFromStringW(aclsid, &iid);
129 TRACE("(%p,%s,unk:%p,%s,%p)\n",
130 aclsid,shdebugstr_guid(myclsid),pUnkOuter,shdebugstr_guid(refiid),ppv);
132 if (SUCCEEDED(DllGetClassObject(myclsid, &IID_IClassFactory,(LPVOID*)&pcf)))
134 hres = IClassFactory_CreateInstance(pcf, pUnkOuter, refiid, ppv);
135 IClassFactory_Release(pcf);
139 /* we look up the dll path in the registry */
140 SHStringFromGUIDW(myclsid, sClassID, sizeof(sClassID)/sizeof(WCHAR));
141 lstrcpyW(sKeyName, sCLSID);
142 lstrcatW(sKeyName, sClassID);
143 lstrcatW(sKeyName, sInProcServer32);
145 if (RegOpenKeyExW(HKEY_CLASSES_ROOT, sKeyName, 0, KEY_READ, &hKey))
146 return E_ACCESSDENIED;
148 /* if a special registry key is set, we load a shell extension without help of OLE32 */
149 if (!SHQueryValueExW(hKey, sLoadWithoutCOM, 0, 0, 0, 0))
151 /* load an external dll without ole32 */
153 typedef HRESULT (CALLBACK *DllGetClassObjectFunc)(REFCLSID clsid, REFIID iid, LPVOID *ppv);
154 DllGetClassObjectFunc DllGetClassObject;
156 dwSize = sizeof(sDllPath);
157 SHQueryValueExW(hKey, NULL, 0,0, sDllPath, &dwSize );
159 if ((hLibrary = LoadLibraryExW(sDllPath, 0, LOAD_WITH_ALTERED_SEARCH_PATH)) == 0) {
160 ERR("couldn't load InprocServer32 dll %s\n", debugstr_w(sDllPath));
161 hres = E_ACCESSDENIED;
163 } else if (!(DllGetClassObject = (DllGetClassObjectFunc)GetProcAddress(hLibrary, "DllGetClassObject"))) {
164 ERR("couldn't find function DllGetClassObject in %s\n", debugstr_w(sDllPath));
165 FreeLibrary( hLibrary );
166 hres = E_ACCESSDENIED;
168 } else if (FAILED(hres = DllGetClassObject(myclsid, &IID_IClassFactory, (LPVOID*)&pcf))) {
169 TRACE("GetClassObject failed 0x%08x\n", hres);
173 hres = IClassFactory_CreateInstance(pcf, pUnkOuter, refiid, ppv);
174 IClassFactory_Release(pcf);
177 /* load an external dll in the usual way */
178 hres = CoCreateInstance(myclsid, pUnkOuter, CLSCTX_INPROC_SERVER, refiid, ppv);
182 if (hKey) RegCloseKey(hKey);
185 ERR("failed (0x%08x) to create CLSID:%s IID:%s\n",
186 hres, shdebugstr_guid(myclsid), shdebugstr_guid(refiid));
187 ERR("class not found in registry\n");
190 TRACE("-- instance: %p\n",*ppv);
194 /*************************************************************************
195 * DllGetClassObject [SHELL32.@]
196 * SHDllGetClassObject [SHELL32.128]
198 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
200 HRESULT hres = E_OUTOFMEMORY;
201 IClassFactory * pcf = NULL;
204 TRACE("CLSID:%s,IID:%s\n",shdebugstr_guid(rclsid),shdebugstr_guid(iid));
206 if (!ppv) return E_INVALIDARG;
209 /* search our internal interface table */
210 for(i=0;InterfaceTable[i].riid;i++) {
211 if(IsEqualIID(InterfaceTable[i].riid, rclsid)) {
212 TRACE("index[%u]\n", i);
213 pcf = IDefClF_fnConstructor(InterfaceTable[i].lpfnCI, NULL, NULL);
218 FIXME("failed for CLSID=%s\n", shdebugstr_guid(rclsid));
219 return CLASS_E_CLASSNOTAVAILABLE;
222 hres = IClassFactory_QueryInterface(pcf, iid, ppv);
223 IClassFactory_Release(pcf);
225 TRACE("-- pointer to class factory: %p\n",*ppv);
229 /*************************************************************************
230 * SHCLSIDFromString [SHELL32.147]
232 * Under Windows 9x this was an ANSI version of CLSIDFromString. It also allowed
233 * to avoid dependency on ole32.dll (see SHLoadOLE for details).
235 * Under Windows NT/2000/XP this is equivalent to CLSIDFromString
238 * exported by ordinal
241 * CLSIDFromString, SHLoadOLE
243 DWORD WINAPI SHCLSIDFromStringA (LPCSTR clsid, CLSID *id)
246 TRACE("(%p(%s) %p)\n", clsid, clsid, id);
247 if (!MultiByteToWideChar( CP_ACP, 0, clsid, -1, buffer, sizeof(buffer)/sizeof(WCHAR) ))
248 return CO_E_CLASSSTRING;
249 return CLSIDFromString( buffer, id );
251 DWORD WINAPI SHCLSIDFromStringW (LPCWSTR clsid, CLSID *id)
253 TRACE("(%p(%s) %p)\n", clsid, debugstr_w(clsid), id);
254 return CLSIDFromString((LPWSTR)clsid, id);
256 DWORD WINAPI SHCLSIDFromStringAW (LPCVOID clsid, CLSID *id)
258 if (SHELL_OsIsUnicode())
259 return SHCLSIDFromStringW (clsid, id);
260 return SHCLSIDFromStringA (clsid, id);
263 /*************************************************************************
264 * SHGetMalloc [SHELL32.@]
266 * Equivalent to CoGetMalloc(MEMCTX_TASK, ...). Under Windows 9x this function
267 * could use the shell32 built-in "mini-COM" without the need to load ole32.dll -
268 * see SHLoadOLE for details.
271 * lpmal [O] Destination for IMalloc interface.
274 * Success: S_OK. lpmal contains the shells IMalloc interface.
275 * Failure. An HRESULT error code.
278 * CoGetMalloc, SHLoadOLE
280 HRESULT WINAPI SHGetMalloc(LPMALLOC *lpmal)
282 TRACE("(%p)\n", lpmal);
283 return CoGetMalloc(MEMCTX_TASK, lpmal);
286 /*************************************************************************
287 * SHAlloc [SHELL32.196]
289 * Equivalent to CoTaskMemAlloc. Under Windows 9x this function could use
290 * the shell32 built-in "mini-COM" without the need to load ole32.dll -
291 * see SHLoadOLE for details.
294 * exported by ordinal
297 * CoTaskMemAlloc, SHLoadOLE
299 LPVOID WINAPI SHAlloc(DWORD len)
303 ret = CoTaskMemAlloc(len);
304 TRACE("%u bytes at %p\n",len, ret);
308 /*************************************************************************
309 * SHFree [SHELL32.195]
311 * Equivalent to CoTaskMemFree. Under Windows 9x this function could use
312 * the shell32 built-in "mini-COM" without the need to load ole32.dll -
313 * see SHLoadOLE for details.
316 * exported by ordinal
319 * CoTaskMemFree, SHLoadOLE
321 void WINAPI SHFree(LPVOID pv)
327 /*************************************************************************
328 * SHGetDesktopFolder [SHELL32.@]
330 HRESULT WINAPI SHGetDesktopFolder(IShellFolder **psf)
335 if(!psf) return E_INVALIDARG;
337 hres = ISF_Desktop_Constructor(NULL, &IID_IShellFolder,(LPVOID*)psf);
339 TRACE("-- %p->(%p)\n",psf, *psf);
342 /**************************************************************************
343 * Default ClassFactory Implementation
345 * SHCreateDefClassObject
348 * Helper function for dlls without their own classfactory.
349 * A generic classfactory is returned.
350 * When the CreateInstance of the cf is called the callback is executed.
355 const IClassFactoryVtbl *lpVtbl;
358 LPFNCREATEINSTANCE lpfnCI;
359 const IID * riidInst;
360 LONG * pcRefDll; /* pointer to refcounter in external dll (ugrrr...) */
363 static const IClassFactoryVtbl dclfvt;
365 /**************************************************************************
366 * IDefClF_fnConstructor
369 static IClassFactory * IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI, PLONG pcRefDll, REFIID riidInst)
373 lpclf = HeapAlloc(GetProcessHeap(),0,sizeof(IDefClFImpl));
375 lpclf->lpVtbl = &dclfvt;
376 lpclf->lpfnCI = lpfnCI;
377 lpclf->pcRefDll = pcRefDll;
379 if (pcRefDll) InterlockedIncrement(pcRefDll);
380 lpclf->riidInst = riidInst;
382 TRACE("(%p)%s\n",lpclf, shdebugstr_guid(riidInst));
383 return (LPCLASSFACTORY)lpclf;
385 /**************************************************************************
386 * IDefClF_fnQueryInterface
388 static HRESULT WINAPI IDefClF_fnQueryInterface(
389 LPCLASSFACTORY iface, REFIID riid, LPVOID *ppvObj)
391 IDefClFImpl *This = (IDefClFImpl *)iface;
393 TRACE("(%p)->(%s)\n",This,shdebugstr_guid(riid));
397 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory)) {
399 InterlockedIncrement(&This->ref);
403 TRACE("-- E_NOINTERFACE\n");
404 return E_NOINTERFACE;
406 /******************************************************************************
409 static ULONG WINAPI IDefClF_fnAddRef(LPCLASSFACTORY iface)
411 IDefClFImpl *This = (IDefClFImpl *)iface;
412 ULONG refCount = InterlockedIncrement(&This->ref);
414 TRACE("(%p)->(count=%u)\n", This, refCount - 1);
418 /******************************************************************************
421 static ULONG WINAPI IDefClF_fnRelease(LPCLASSFACTORY iface)
423 IDefClFImpl *This = (IDefClFImpl *)iface;
424 ULONG refCount = InterlockedDecrement(&This->ref);
426 TRACE("(%p)->(count=%u)\n", This, refCount + 1);
430 if (This->pcRefDll) InterlockedDecrement(This->pcRefDll);
432 TRACE("-- destroying IClassFactory(%p)\n",This);
433 HeapFree(GetProcessHeap(),0,This);
438 /******************************************************************************
439 * IDefClF_fnCreateInstance
441 static HRESULT WINAPI IDefClF_fnCreateInstance(
442 LPCLASSFACTORY iface, LPUNKNOWN pUnkOuter, REFIID riid, LPVOID *ppvObject)
444 IDefClFImpl *This = (IDefClFImpl *)iface;
446 TRACE("%p->(%p,%s,%p)\n",This,pUnkOuter,shdebugstr_guid(riid),ppvObject);
450 if ( This->riidInst==NULL ||
451 IsEqualCLSID(riid, This->riidInst) ||
452 IsEqualCLSID(riid, &IID_IUnknown) )
454 return This->lpfnCI(pUnkOuter, riid, ppvObject);
457 ERR("unknown IID requested %s\n",shdebugstr_guid(riid));
458 return E_NOINTERFACE;
460 /******************************************************************************
461 * IDefClF_fnLockServer
463 static HRESULT WINAPI IDefClF_fnLockServer(LPCLASSFACTORY iface, BOOL fLock)
465 IDefClFImpl *This = (IDefClFImpl *)iface;
466 TRACE("%p->(0x%x), not implemented\n",This, fLock);
470 static const IClassFactoryVtbl dclfvt =
472 IDefClF_fnQueryInterface,
475 IDefClF_fnCreateInstance,
479 /******************************************************************************
480 * SHCreateDefClassObject [SHELL32.70]
482 HRESULT WINAPI SHCreateDefClassObject(
485 LPFNCREATEINSTANCE lpfnCI, /* [in] create instance callback entry */
486 LPDWORD pcRefDll, /* [in/out] ref count of the dll */
487 REFIID riidInst) /* [in] optional interface to the instance */
491 TRACE("%s %p %p %p %s\n",
492 shdebugstr_guid(riid), ppv, lpfnCI, pcRefDll, shdebugstr_guid(riidInst));
494 if (! IsEqualCLSID(riid, &IID_IClassFactory) ) return E_NOINTERFACE;
495 if (! (pcf = IDefClF_fnConstructor(lpfnCI, (PLONG)pcRefDll, riidInst))) return E_OUTOFMEMORY;
500 /*************************************************************************
501 * DragAcceptFiles [SHELL32.@]
503 void WINAPI DragAcceptFiles(HWND hWnd, BOOL b)
507 if( !IsWindow(hWnd) ) return;
508 exstyle = GetWindowLongA(hWnd,GWL_EXSTYLE);
510 exstyle |= WS_EX_ACCEPTFILES;
512 exstyle &= ~WS_EX_ACCEPTFILES;
513 SetWindowLongA(hWnd,GWL_EXSTYLE,exstyle);
516 /*************************************************************************
517 * DragFinish [SHELL32.@]
519 void WINAPI DragFinish(HDROP h)
525 /*************************************************************************
526 * DragQueryPoint [SHELL32.@]
528 BOOL WINAPI DragQueryPoint(HDROP hDrop, POINT *p)
530 DROPFILES *lpDropFileStruct;
535 lpDropFileStruct = GlobalLock(hDrop);
537 *p = lpDropFileStruct->pt;
538 bRet = lpDropFileStruct->fNC;
544 /*************************************************************************
545 * DragQueryFileA [SHELL32.@]
546 * DragQueryFile [SHELL32.@]
548 UINT WINAPI DragQueryFileA(
556 DROPFILES *lpDropFileStruct = GlobalLock(hDrop);
558 TRACE("(%p, %x, %p, %u)\n", hDrop,lFile,lpszFile,lLength);
560 if(!lpDropFileStruct) goto end;
562 lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->pFiles;
564 if(lpDropFileStruct->fWide) {
565 LPWSTR lpszFileW = NULL;
568 lpszFileW = HeapAlloc(GetProcessHeap(), 0, lLength*sizeof(WCHAR));
569 if(lpszFileW == NULL) {
573 i = DragQueryFileW(hDrop, lFile, lpszFileW, lLength);
576 WideCharToMultiByte(CP_ACP, 0, lpszFileW, -1, lpszFile, lLength, 0, NULL);
577 HeapFree(GetProcessHeap(), 0, lpszFileW);
584 while (*lpDrop++); /* skip filename */
587 i = (lFile == 0xFFFFFFFF) ? i : 0;
593 if (!lpszFile ) goto end; /* needed buffer size */
594 lstrcpynA (lpszFile, lpDrop, lLength);
600 /*************************************************************************
601 * DragQueryFileW [SHELL32.@]
603 UINT WINAPI DragQueryFileW(
611 DROPFILES *lpDropFileStruct = GlobalLock(hDrop);
613 TRACE("(%p, %x, %p, %u)\n", hDrop,lFile,lpszwFile,lLength);
615 if(!lpDropFileStruct) goto end;
617 lpwDrop = (LPWSTR) ((LPSTR)lpDropFileStruct + lpDropFileStruct->pFiles);
619 if(lpDropFileStruct->fWide == FALSE) {
620 LPSTR lpszFileA = NULL;
623 lpszFileA = HeapAlloc(GetProcessHeap(), 0, lLength);
624 if(lpszFileA == NULL) {
628 i = DragQueryFileA(hDrop, lFile, lpszFileA, lLength);
631 MultiByteToWideChar(CP_ACP, 0, lpszFileA, -1, lpszwFile, lLength);
632 HeapFree(GetProcessHeap(), 0, lpszFileA);
640 while (*lpwDrop++); /* skip filename */
643 i = (lFile == 0xFFFFFFFF) ? i : 0;
648 i = strlenW(lpwDrop);
649 if ( !lpszwFile) goto end; /* needed buffer size */
650 lstrcpynW (lpszwFile, lpwDrop, lLength);