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[] = {
64 {&CLSID_AutoComplete, IAutoComplete_Constructor},
65 {&CLSID_ControlPanel, IControlPanel_Constructor},
66 {&CLSID_DragDropHelper, IDropTargetHelper_Constructor},
67 {&CLSID_FolderShortcut, FolderShortcut_Constructor},
68 {&CLSID_MyComputer, ISF_MyComputer_Constructor},
69 {&CLSID_MyDocuments, MyDocuments_Constructor},
70 {&CLSID_NetworkPlaces, ISF_NetworkPlaces_Constructor},
71 {&CLSID_Printers, Printers_Constructor},
72 {&CLSID_QueryAssociations, QueryAssociations_Constructor},
73 {&CLSID_RecycleBin, RecycleBin_Constructor},
74 {&CLSID_ShellDesktop, ISF_Desktop_Constructor},
75 {&CLSID_ShellFSFolder, IFSFolder_Constructor},
76 {&CLSID_ShellItem, IShellItem_Constructor},
77 {&CLSID_ShellLink, IShellLink_Constructor},
78 {&CLSID_UnixDosFolder, UnixDosFolder_Constructor},
79 {&CLSID_UnixFolder, UnixFolder_Constructor},
80 {&CLSID_ExplorerBrowser,ExplorerBrowser_Constructor},
84 /*************************************************************************
85 * SHCoCreateInstance [SHELL32.102]
87 * Equivalent to CoCreateInstance. Under Windows 9x this function could sometimes
88 * use the shell32 built-in "mini-COM" without the need to load ole32.dll - see
89 * SHLoadOLE for details.
91 * Under wine if a "LoadWithoutCOM" value is present or the object resides in
92 * shell32.dll the function will load the object manually without the help of ole32
98 * CoCreateInstace, SHLoadOLE
100 HRESULT WINAPI SHCoCreateInstance(
109 const CLSID * myclsid = clsid;
110 WCHAR sKeyName[MAX_PATH];
111 const WCHAR sCLSID[7] = {'C','L','S','I','D','\\','\0'};
113 const WCHAR sInProcServer32[16] ={'\\','I','n','p','r','o','c','S','e','r','v','e','r','3','2','\0'};
114 const WCHAR sLoadWithoutCOM[15] ={'L','o','a','d','W','i','t','h','o','u','t','C','O','M','\0'};
115 WCHAR sDllPath[MAX_PATH];
118 IClassFactory * pcf = NULL;
120 if(!ppv) return E_POINTER;
123 /* if the clsid is a string, convert it */
126 if (!aclsid) return REGDB_E_CLASSNOTREG;
127 SHCLSIDFromStringW(aclsid, &iid);
131 TRACE("(%p,%s,unk:%p,%s,%p)\n",
132 aclsid,shdebugstr_guid(myclsid),pUnkOuter,shdebugstr_guid(refiid),ppv);
134 if (SUCCEEDED(DllGetClassObject(myclsid, &IID_IClassFactory,(LPVOID*)&pcf)))
136 hres = IClassFactory_CreateInstance(pcf, pUnkOuter, refiid, ppv);
137 IClassFactory_Release(pcf);
141 /* we look up the dll path in the registry */
142 SHStringFromGUIDW(myclsid, sClassID, sizeof(sClassID)/sizeof(WCHAR));
143 lstrcpyW(sKeyName, sCLSID);
144 lstrcatW(sKeyName, sClassID);
145 lstrcatW(sKeyName, sInProcServer32);
147 if (RegOpenKeyExW(HKEY_CLASSES_ROOT, sKeyName, 0, KEY_READ, &hKey))
148 return E_ACCESSDENIED;
150 /* if a special registry key is set, we load a shell extension without help of OLE32 */
151 if (!SHQueryValueExW(hKey, sLoadWithoutCOM, 0, 0, 0, 0))
153 /* load an external dll without ole32 */
155 typedef HRESULT (CALLBACK *DllGetClassObjectFunc)(REFCLSID clsid, REFIID iid, LPVOID *ppv);
156 DllGetClassObjectFunc DllGetClassObject;
158 dwSize = sizeof(sDllPath);
159 SHQueryValueExW(hKey, NULL, 0,0, sDllPath, &dwSize );
161 if ((hLibrary = LoadLibraryExW(sDllPath, 0, LOAD_WITH_ALTERED_SEARCH_PATH)) == 0) {
162 ERR("couldn't load InprocServer32 dll %s\n", debugstr_w(sDllPath));
163 hres = E_ACCESSDENIED;
165 } else if (!(DllGetClassObject = (DllGetClassObjectFunc)GetProcAddress(hLibrary, "DllGetClassObject"))) {
166 ERR("couldn't find function DllGetClassObject in %s\n", debugstr_w(sDllPath));
167 FreeLibrary( hLibrary );
168 hres = E_ACCESSDENIED;
170 } else if (FAILED(hres = DllGetClassObject(myclsid, &IID_IClassFactory, (LPVOID*)&pcf))) {
171 TRACE("GetClassObject failed 0x%08x\n", hres);
175 hres = IClassFactory_CreateInstance(pcf, pUnkOuter, refiid, ppv);
176 IClassFactory_Release(pcf);
179 /* load an external dll in the usual way */
180 hres = CoCreateInstance(myclsid, pUnkOuter, CLSCTX_INPROC_SERVER, refiid, ppv);
184 if (hKey) RegCloseKey(hKey);
187 ERR("failed (0x%08x) to create CLSID:%s IID:%s\n",
188 hres, shdebugstr_guid(myclsid), shdebugstr_guid(refiid));
189 ERR("class not found in registry\n");
192 TRACE("-- instance: %p\n",*ppv);
196 /*************************************************************************
197 * DllGetClassObject [SHELL32.@]
198 * SHDllGetClassObject [SHELL32.128]
200 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
202 HRESULT hres = E_OUTOFMEMORY;
203 IClassFactory * pcf = NULL;
206 TRACE("CLSID:%s,IID:%s\n",shdebugstr_guid(rclsid),shdebugstr_guid(iid));
208 if (!ppv) return E_INVALIDARG;
211 /* search our internal interface table */
212 for(i=0;InterfaceTable[i].riid;i++) {
213 if(IsEqualIID(InterfaceTable[i].riid, rclsid)) {
214 TRACE("index[%u]\n", i);
215 pcf = IDefClF_fnConstructor(InterfaceTable[i].lpfnCI, NULL, NULL);
220 FIXME("failed for CLSID=%s\n", shdebugstr_guid(rclsid));
221 return CLASS_E_CLASSNOTAVAILABLE;
224 hres = IClassFactory_QueryInterface(pcf, iid, ppv);
225 IClassFactory_Release(pcf);
227 TRACE("-- pointer to class factory: %p\n",*ppv);
231 /*************************************************************************
232 * SHCLSIDFromString [SHELL32.147]
234 * Under Windows 9x this was an ANSI version of CLSIDFromString. It also allowed
235 * to avoid dependency on ole32.dll (see SHLoadOLE for details).
237 * Under Windows NT/2000/XP this is equivalent to CLSIDFromString
240 * exported by ordinal
243 * CLSIDFromString, SHLoadOLE
245 DWORD WINAPI SHCLSIDFromStringA (LPCSTR clsid, CLSID *id)
248 TRACE("(%p(%s) %p)\n", clsid, clsid, id);
249 if (!MultiByteToWideChar( CP_ACP, 0, clsid, -1, buffer, sizeof(buffer)/sizeof(WCHAR) ))
250 return CO_E_CLASSSTRING;
251 return CLSIDFromString( buffer, id );
253 DWORD WINAPI SHCLSIDFromStringW (LPCWSTR clsid, CLSID *id)
255 TRACE("(%p(%s) %p)\n", clsid, debugstr_w(clsid), id);
256 return CLSIDFromString(clsid, id);
258 DWORD WINAPI SHCLSIDFromStringAW (LPCVOID clsid, CLSID *id)
260 if (SHELL_OsIsUnicode())
261 return SHCLSIDFromStringW (clsid, id);
262 return SHCLSIDFromStringA (clsid, id);
265 /*************************************************************************
266 * SHGetMalloc [SHELL32.@]
268 * Equivalent to CoGetMalloc(MEMCTX_TASK, ...). Under Windows 9x this function
269 * could use the shell32 built-in "mini-COM" without the need to load ole32.dll -
270 * see SHLoadOLE for details.
273 * lpmal [O] Destination for IMalloc interface.
276 * Success: S_OK. lpmal contains the shells IMalloc interface.
277 * Failure. An HRESULT error code.
280 * CoGetMalloc, SHLoadOLE
282 HRESULT WINAPI SHGetMalloc(LPMALLOC *lpmal)
284 TRACE("(%p)\n", lpmal);
285 return CoGetMalloc(MEMCTX_TASK, lpmal);
288 /*************************************************************************
289 * SHAlloc [SHELL32.196]
291 * Equivalent to CoTaskMemAlloc. Under Windows 9x this function could use
292 * the shell32 built-in "mini-COM" without the need to load ole32.dll -
293 * see SHLoadOLE for details.
296 * exported by ordinal
299 * CoTaskMemAlloc, SHLoadOLE
301 LPVOID WINAPI SHAlloc(DWORD len)
305 ret = CoTaskMemAlloc(len);
306 TRACE("%u bytes at %p\n",len, ret);
310 /*************************************************************************
311 * SHFree [SHELL32.195]
313 * Equivalent to CoTaskMemFree. Under Windows 9x this function could use
314 * the shell32 built-in "mini-COM" without the need to load ole32.dll -
315 * see SHLoadOLE for details.
318 * exported by ordinal
321 * CoTaskMemFree, SHLoadOLE
323 void WINAPI SHFree(LPVOID pv)
329 /*************************************************************************
330 * SHGetDesktopFolder [SHELL32.@]
332 HRESULT WINAPI SHGetDesktopFolder(IShellFolder **psf)
336 TRACE("(%p)\n", psf);
338 if(!psf) return E_INVALIDARG;
341 hres = ISF_Desktop_Constructor(NULL, &IID_IShellFolder, (LPVOID*)psf);
343 TRACE("-- %p->(%p) 0x%08x\n", psf, *psf, hres);
346 /**************************************************************************
347 * Default ClassFactory Implementation
349 * SHCreateDefClassObject
352 * Helper function for dlls without their own classfactory.
353 * A generic classfactory is returned.
354 * When the CreateInstance of the cf is called the callback is executed.
359 const IClassFactoryVtbl *lpVtbl;
362 LPFNCREATEINSTANCE lpfnCI;
363 const IID * riidInst;
364 LONG * pcRefDll; /* pointer to refcounter in external dll (ugrrr...) */
367 static const IClassFactoryVtbl dclfvt;
369 /**************************************************************************
370 * IDefClF_fnConstructor
373 static IClassFactory * IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI, PLONG pcRefDll, REFIID riidInst)
377 lpclf = HeapAlloc(GetProcessHeap(),0,sizeof(IDefClFImpl));
379 lpclf->lpVtbl = &dclfvt;
380 lpclf->lpfnCI = lpfnCI;
381 lpclf->pcRefDll = pcRefDll;
383 if (pcRefDll) InterlockedIncrement(pcRefDll);
384 lpclf->riidInst = riidInst;
386 TRACE("(%p)%s\n",lpclf, shdebugstr_guid(riidInst));
387 return (LPCLASSFACTORY)lpclf;
389 /**************************************************************************
390 * IDefClF_fnQueryInterface
392 static HRESULT WINAPI IDefClF_fnQueryInterface(
393 LPCLASSFACTORY iface, REFIID riid, LPVOID *ppvObj)
395 IDefClFImpl *This = (IDefClFImpl *)iface;
397 TRACE("(%p)->(%s)\n",This,shdebugstr_guid(riid));
401 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory)) {
403 InterlockedIncrement(&This->ref);
407 TRACE("-- E_NOINTERFACE\n");
408 return E_NOINTERFACE;
410 /******************************************************************************
413 static ULONG WINAPI IDefClF_fnAddRef(LPCLASSFACTORY iface)
415 IDefClFImpl *This = (IDefClFImpl *)iface;
416 ULONG refCount = InterlockedIncrement(&This->ref);
418 TRACE("(%p)->(count=%u)\n", This, refCount - 1);
422 /******************************************************************************
425 static ULONG WINAPI IDefClF_fnRelease(LPCLASSFACTORY iface)
427 IDefClFImpl *This = (IDefClFImpl *)iface;
428 ULONG refCount = InterlockedDecrement(&This->ref);
430 TRACE("(%p)->(count=%u)\n", This, refCount + 1);
434 if (This->pcRefDll) InterlockedDecrement(This->pcRefDll);
436 TRACE("-- destroying IClassFactory(%p)\n",This);
437 HeapFree(GetProcessHeap(),0,This);
442 /******************************************************************************
443 * IDefClF_fnCreateInstance
445 static HRESULT WINAPI IDefClF_fnCreateInstance(
446 LPCLASSFACTORY iface, LPUNKNOWN pUnkOuter, REFIID riid, LPVOID *ppvObject)
448 IDefClFImpl *This = (IDefClFImpl *)iface;
450 TRACE("%p->(%p,%s,%p)\n",This,pUnkOuter,shdebugstr_guid(riid),ppvObject);
454 if ( This->riidInst==NULL ||
455 IsEqualCLSID(riid, This->riidInst) ||
456 IsEqualCLSID(riid, &IID_IUnknown) )
458 return This->lpfnCI(pUnkOuter, riid, ppvObject);
461 ERR("unknown IID requested %s\n",shdebugstr_guid(riid));
462 return E_NOINTERFACE;
464 /******************************************************************************
465 * IDefClF_fnLockServer
467 static HRESULT WINAPI IDefClF_fnLockServer(LPCLASSFACTORY iface, BOOL fLock)
469 IDefClFImpl *This = (IDefClFImpl *)iface;
470 TRACE("%p->(0x%x), not implemented\n",This, fLock);
474 static const IClassFactoryVtbl dclfvt =
476 IDefClF_fnQueryInterface,
479 IDefClF_fnCreateInstance,
483 /******************************************************************************
484 * SHCreateDefClassObject [SHELL32.70]
486 HRESULT WINAPI SHCreateDefClassObject(
489 LPFNCREATEINSTANCE lpfnCI, /* [in] create instance callback entry */
490 LPDWORD pcRefDll, /* [in/out] ref count of the dll */
491 REFIID riidInst) /* [in] optional interface to the instance */
495 TRACE("%s %p %p %p %s\n",
496 shdebugstr_guid(riid), ppv, lpfnCI, pcRefDll, shdebugstr_guid(riidInst));
498 if (! IsEqualCLSID(riid, &IID_IClassFactory) ) return E_NOINTERFACE;
499 if (! (pcf = IDefClF_fnConstructor(lpfnCI, (PLONG)pcRefDll, riidInst))) return E_OUTOFMEMORY;
504 /*************************************************************************
505 * DragAcceptFiles [SHELL32.@]
507 void WINAPI DragAcceptFiles(HWND hWnd, BOOL b)
511 if( !IsWindow(hWnd) ) return;
512 exstyle = GetWindowLongA(hWnd,GWL_EXSTYLE);
514 exstyle |= WS_EX_ACCEPTFILES;
516 exstyle &= ~WS_EX_ACCEPTFILES;
517 SetWindowLongA(hWnd,GWL_EXSTYLE,exstyle);
520 /*************************************************************************
521 * DragFinish [SHELL32.@]
523 void WINAPI DragFinish(HDROP h)
529 /*************************************************************************
530 * DragQueryPoint [SHELL32.@]
532 BOOL WINAPI DragQueryPoint(HDROP hDrop, POINT *p)
534 DROPFILES *lpDropFileStruct;
539 lpDropFileStruct = GlobalLock(hDrop);
541 *p = lpDropFileStruct->pt;
542 bRet = lpDropFileStruct->fNC;
548 /*************************************************************************
549 * DragQueryFileA [SHELL32.@]
550 * DragQueryFile [SHELL32.@]
552 UINT WINAPI DragQueryFileA(
560 DROPFILES *lpDropFileStruct = GlobalLock(hDrop);
562 TRACE("(%p, %x, %p, %u)\n", hDrop,lFile,lpszFile,lLength);
564 if(!lpDropFileStruct) goto end;
566 lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->pFiles;
568 if(lpDropFileStruct->fWide) {
569 LPWSTR lpszFileW = NULL;
572 lpszFileW = HeapAlloc(GetProcessHeap(), 0, lLength*sizeof(WCHAR));
573 if(lpszFileW == NULL) {
577 i = DragQueryFileW(hDrop, lFile, lpszFileW, lLength);
580 WideCharToMultiByte(CP_ACP, 0, lpszFileW, -1, lpszFile, lLength, 0, NULL);
581 HeapFree(GetProcessHeap(), 0, lpszFileW);
588 while (*lpDrop++); /* skip filename */
591 i = (lFile == 0xFFFFFFFF) ? i : 0;
597 if (!lpszFile ) goto end; /* needed buffer size */
598 lstrcpynA (lpszFile, lpDrop, lLength);
604 /*************************************************************************
605 * DragQueryFileW [SHELL32.@]
607 UINT WINAPI DragQueryFileW(
615 DROPFILES *lpDropFileStruct = GlobalLock(hDrop);
617 TRACE("(%p, %x, %p, %u)\n", hDrop,lFile,lpszwFile,lLength);
619 if(!lpDropFileStruct) goto end;
621 lpwDrop = (LPWSTR) ((LPSTR)lpDropFileStruct + lpDropFileStruct->pFiles);
623 if(lpDropFileStruct->fWide == FALSE) {
624 LPSTR lpszFileA = NULL;
627 lpszFileA = HeapAlloc(GetProcessHeap(), 0, lLength);
628 if(lpszFileA == NULL) {
632 i = DragQueryFileA(hDrop, lFile, lpszFileA, lLength);
635 MultiByteToWideChar(CP_ACP, 0, lpszFileA, -1, lpszwFile, lLength);
636 HeapFree(GetProcessHeap(), 0, lpszFileA);
644 while (*lpwDrop++); /* skip filename */
647 i = (lFile == 0xFFFFFFFF) ? i : 0;
652 i = strlenW(lpwDrop);
653 if ( !lpszwFile) goto end; /* needed buffer size */
654 lstrcpynW (lpszwFile, lpwDrop, lLength);