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
29 #define NONAMELESSUNION
42 #include "undocshell.h"
43 #include "wine/unicode.h"
44 #include "shell32_main.h"
46 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(shell);
52 extern INT WINAPI SHStringFromGUIDW(REFGUID guid, LPWSTR lpszDest, INT cchMax); /* shlwapi.24 */
54 /**************************************************************************
55 * Default ClassFactory types
57 typedef HRESULT (CALLBACK *LPFNCREATEINSTANCE)(IUnknown* pUnkOuter, REFIID riid, LPVOID* ppvObject);
58 static IClassFactory * IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI, PLONG pcRefDll, REFIID riidInst);
60 /* this table contains all CLSID's of shell32 objects */
63 LPFNCREATEINSTANCE lpfnCI;
64 } InterfaceTable[] = {
66 {&CLSID_AutoComplete, IAutoComplete_Constructor},
67 {&CLSID_ControlPanel, IControlPanel_Constructor},
68 {&CLSID_DragDropHelper, IDropTargetHelper_Constructor},
69 {&CLSID_FolderShortcut, FolderShortcut_Constructor},
70 {&CLSID_MyComputer, ISF_MyComputer_Constructor},
71 {&CLSID_MyDocuments, MyDocuments_Constructor},
72 {&CLSID_NetworkPlaces, ISF_NetworkPlaces_Constructor},
73 {&CLSID_Printers, Printers_Constructor},
74 {&CLSID_QueryAssociations, QueryAssociations_Constructor},
75 {&CLSID_RecycleBin, RecycleBin_Constructor},
76 {&CLSID_ShellDesktop, ISF_Desktop_Constructor},
77 {&CLSID_ShellFSFolder, IFSFolder_Constructor},
78 {&CLSID_ShellItem, IShellItem_Constructor},
79 {&CLSID_ShellLink, IShellLink_Constructor},
80 {&CLSID_UnixDosFolder, UnixDosFolder_Constructor},
81 {&CLSID_UnixFolder, UnixFolder_Constructor},
82 {&CLSID_ExplorerBrowser,ExplorerBrowser_Constructor},
83 {&CLSID_KnownFolderManager, KnownFolderManager_Constructor},
84 {&CLSID_Shell, IShellDispatch_Constructor},
88 /*************************************************************************
89 * SHCoCreateInstance [SHELL32.102]
91 * Equivalent to CoCreateInstance. Under Windows 9x this function could sometimes
92 * use the shell32 built-in "mini-COM" without the need to load ole32.dll - see
93 * SHLoadOLE for details.
95 * Under wine if a "LoadWithoutCOM" value is present or the object resides in
96 * shell32.dll the function will load the object manually without the help of ole32
102 * CoCreateInstace, SHLoadOLE
104 HRESULT WINAPI SHCoCreateInstance(
113 const CLSID * myclsid = clsid;
114 WCHAR sKeyName[MAX_PATH];
115 const WCHAR sCLSID[7] = {'C','L','S','I','D','\\','\0'};
117 const WCHAR sInProcServer32[16] ={'\\','I','n','p','r','o','c','S','e','r','v','e','r','3','2','\0'};
118 const WCHAR sLoadWithoutCOM[15] ={'L','o','a','d','W','i','t','h','o','u','t','C','O','M','\0'};
119 WCHAR sDllPath[MAX_PATH];
122 IClassFactory * pcf = NULL;
124 if(!ppv) return E_POINTER;
127 /* if the clsid is a string, convert it */
130 if (!aclsid) return REGDB_E_CLASSNOTREG;
131 SHCLSIDFromStringW(aclsid, &iid);
135 TRACE("(%p,%s,unk:%p,%s,%p)\n",
136 aclsid,shdebugstr_guid(myclsid),pUnkOuter,shdebugstr_guid(refiid),ppv);
138 if (SUCCEEDED(DllGetClassObject(myclsid, &IID_IClassFactory,(LPVOID*)&pcf)))
140 hres = IClassFactory_CreateInstance(pcf, pUnkOuter, refiid, ppv);
141 IClassFactory_Release(pcf);
145 /* we look up the dll path in the registry */
146 SHStringFromGUIDW(myclsid, sClassID, sizeof(sClassID)/sizeof(WCHAR));
147 lstrcpyW(sKeyName, sCLSID);
148 lstrcatW(sKeyName, sClassID);
149 lstrcatW(sKeyName, sInProcServer32);
151 if (RegOpenKeyExW(HKEY_CLASSES_ROOT, sKeyName, 0, KEY_READ, &hKey))
152 return E_ACCESSDENIED;
154 /* if a special registry key is set, we load a shell extension without help of OLE32 */
155 if (!SHQueryValueExW(hKey, sLoadWithoutCOM, 0, 0, 0, 0))
157 /* load an external dll without ole32 */
159 typedef HRESULT (CALLBACK *DllGetClassObjectFunc)(REFCLSID clsid, REFIID iid, LPVOID *ppv);
160 DllGetClassObjectFunc DllGetClassObject;
162 dwSize = sizeof(sDllPath);
163 SHQueryValueExW(hKey, NULL, 0,0, sDllPath, &dwSize );
165 if ((hLibrary = LoadLibraryExW(sDllPath, 0, LOAD_WITH_ALTERED_SEARCH_PATH)) == 0) {
166 ERR("couldn't load InprocServer32 dll %s\n", debugstr_w(sDllPath));
167 hres = E_ACCESSDENIED;
169 } else if (!(DllGetClassObject = (DllGetClassObjectFunc)GetProcAddress(hLibrary, "DllGetClassObject"))) {
170 ERR("couldn't find function DllGetClassObject in %s\n", debugstr_w(sDllPath));
171 FreeLibrary( hLibrary );
172 hres = E_ACCESSDENIED;
174 } else if (FAILED(hres = DllGetClassObject(myclsid, &IID_IClassFactory, (LPVOID*)&pcf))) {
175 TRACE("GetClassObject failed 0x%08x\n", hres);
179 hres = IClassFactory_CreateInstance(pcf, pUnkOuter, refiid, ppv);
180 IClassFactory_Release(pcf);
183 /* load an external dll in the usual way */
184 hres = CoCreateInstance(myclsid, pUnkOuter, CLSCTX_INPROC_SERVER, refiid, ppv);
188 if (hKey) RegCloseKey(hKey);
191 ERR("failed (0x%08x) to create CLSID:%s IID:%s\n",
192 hres, shdebugstr_guid(myclsid), shdebugstr_guid(refiid));
193 ERR("class not found in registry\n");
196 TRACE("-- instance: %p\n",*ppv);
200 /*************************************************************************
201 * DllGetClassObject [SHELL32.@]
202 * SHDllGetClassObject [SHELL32.128]
204 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
206 HRESULT hres = E_OUTOFMEMORY;
207 IClassFactory * pcf = NULL;
210 TRACE("CLSID:%s,IID:%s\n",shdebugstr_guid(rclsid),shdebugstr_guid(iid));
212 if (!ppv) return E_INVALIDARG;
215 /* search our internal interface table */
216 for(i=0;InterfaceTable[i].riid;i++) {
217 if(IsEqualIID(InterfaceTable[i].riid, rclsid)) {
218 TRACE("index[%u]\n", i);
219 pcf = IDefClF_fnConstructor(InterfaceTable[i].lpfnCI, NULL, NULL);
224 FIXME("failed for CLSID=%s\n", shdebugstr_guid(rclsid));
225 return CLASS_E_CLASSNOTAVAILABLE;
228 hres = IClassFactory_QueryInterface(pcf, iid, ppv);
229 IClassFactory_Release(pcf);
231 TRACE("-- pointer to class factory: %p\n",*ppv);
235 /*************************************************************************
236 * SHCLSIDFromString [SHELL32.147]
238 * Under Windows 9x this was an ANSI version of CLSIDFromString. It also allowed
239 * to avoid dependency on ole32.dll (see SHLoadOLE for details).
241 * Under Windows NT/2000/XP this is equivalent to CLSIDFromString
244 * exported by ordinal
247 * CLSIDFromString, SHLoadOLE
249 DWORD WINAPI SHCLSIDFromStringA (LPCSTR clsid, CLSID *id)
252 TRACE("(%p(%s) %p)\n", clsid, clsid, id);
253 if (!MultiByteToWideChar( CP_ACP, 0, clsid, -1, buffer, sizeof(buffer)/sizeof(WCHAR) ))
254 return CO_E_CLASSSTRING;
255 return CLSIDFromString( buffer, id );
257 DWORD WINAPI SHCLSIDFromStringW (LPCWSTR clsid, CLSID *id)
259 TRACE("(%p(%s) %p)\n", clsid, debugstr_w(clsid), id);
260 return CLSIDFromString(clsid, id);
262 DWORD WINAPI SHCLSIDFromStringAW (LPCVOID clsid, CLSID *id)
264 if (SHELL_OsIsUnicode())
265 return SHCLSIDFromStringW (clsid, id);
266 return SHCLSIDFromStringA (clsid, id);
269 /*************************************************************************
270 * SHGetMalloc [SHELL32.@]
272 * Equivalent to CoGetMalloc(MEMCTX_TASK, ...). Under Windows 9x this function
273 * could use the shell32 built-in "mini-COM" without the need to load ole32.dll -
274 * see SHLoadOLE for details.
277 * lpmal [O] Destination for IMalloc interface.
280 * Success: S_OK. lpmal contains the shells IMalloc interface.
281 * Failure. An HRESULT error code.
284 * CoGetMalloc, SHLoadOLE
286 HRESULT WINAPI SHGetMalloc(LPMALLOC *lpmal)
288 TRACE("(%p)\n", lpmal);
289 return CoGetMalloc(MEMCTX_TASK, lpmal);
292 /*************************************************************************
293 * SHAlloc [SHELL32.196]
295 * Equivalent to CoTaskMemAlloc. Under Windows 9x this function could use
296 * the shell32 built-in "mini-COM" without the need to load ole32.dll -
297 * see SHLoadOLE for details.
300 * exported by ordinal
303 * CoTaskMemAlloc, SHLoadOLE
305 LPVOID WINAPI SHAlloc(DWORD len)
309 ret = CoTaskMemAlloc(len);
310 TRACE("%u bytes at %p\n",len, ret);
314 /*************************************************************************
315 * SHFree [SHELL32.195]
317 * Equivalent to CoTaskMemFree. Under Windows 9x this function could use
318 * the shell32 built-in "mini-COM" without the need to load ole32.dll -
319 * see SHLoadOLE for details.
322 * exported by ordinal
325 * CoTaskMemFree, SHLoadOLE
327 void WINAPI SHFree(LPVOID pv)
333 /*************************************************************************
334 * SHGetDesktopFolder [SHELL32.@]
336 HRESULT WINAPI SHGetDesktopFolder(IShellFolder **psf)
340 TRACE("(%p)\n", psf);
342 if(!psf) return E_INVALIDARG;
345 hres = ISF_Desktop_Constructor(NULL, &IID_IShellFolder, (LPVOID*)psf);
347 TRACE("-- %p->(%p) 0x%08x\n", psf, *psf, hres);
350 /**************************************************************************
351 * Default ClassFactory Implementation
353 * SHCreateDefClassObject
356 * Helper function for dlls without their own classfactory.
357 * A generic classfactory is returned.
358 * When the CreateInstance of the cf is called the callback is executed.
363 IClassFactory IClassFactory_iface;
366 LPFNCREATEINSTANCE lpfnCI;
367 const IID * riidInst;
368 LONG * pcRefDll; /* pointer to refcounter in external dll (ugrrr...) */
371 static inline IDefClFImpl *impl_from_IClassFactory(IClassFactory *iface)
373 return CONTAINING_RECORD(iface, IDefClFImpl, IClassFactory_iface);
376 static const IClassFactoryVtbl dclfvt;
378 /**************************************************************************
379 * IDefClF_fnConstructor
382 static IClassFactory * IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI, PLONG pcRefDll, REFIID riidInst)
386 lpclf = HeapAlloc(GetProcessHeap(),0,sizeof(IDefClFImpl));
388 lpclf->IClassFactory_iface.lpVtbl = &dclfvt;
389 lpclf->lpfnCI = lpfnCI;
390 lpclf->pcRefDll = pcRefDll;
392 if (pcRefDll) InterlockedIncrement(pcRefDll);
393 lpclf->riidInst = riidInst;
395 TRACE("(%p)%s\n",lpclf, shdebugstr_guid(riidInst));
396 return (LPCLASSFACTORY)lpclf;
398 /**************************************************************************
399 * IDefClF_fnQueryInterface
401 static HRESULT WINAPI IDefClF_fnQueryInterface(
402 LPCLASSFACTORY iface, REFIID riid, LPVOID *ppvObj)
404 IDefClFImpl *This = impl_from_IClassFactory(iface);
406 TRACE("(%p)->(%s)\n",This,shdebugstr_guid(riid));
410 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory)) {
412 InterlockedIncrement(&This->ref);
416 TRACE("-- E_NOINTERFACE\n");
417 return E_NOINTERFACE;
419 /******************************************************************************
422 static ULONG WINAPI IDefClF_fnAddRef(LPCLASSFACTORY iface)
424 IDefClFImpl *This = impl_from_IClassFactory(iface);
425 ULONG refCount = InterlockedIncrement(&This->ref);
427 TRACE("(%p)->(count=%u)\n", This, refCount - 1);
431 /******************************************************************************
434 static ULONG WINAPI IDefClF_fnRelease(LPCLASSFACTORY iface)
436 IDefClFImpl *This = impl_from_IClassFactory(iface);
437 ULONG refCount = InterlockedDecrement(&This->ref);
439 TRACE("(%p)->(count=%u)\n", This, refCount + 1);
443 if (This->pcRefDll) InterlockedDecrement(This->pcRefDll);
445 TRACE("-- destroying IClassFactory(%p)\n",This);
446 HeapFree(GetProcessHeap(),0,This);
451 /******************************************************************************
452 * IDefClF_fnCreateInstance
454 static HRESULT WINAPI IDefClF_fnCreateInstance(
455 LPCLASSFACTORY iface, LPUNKNOWN pUnkOuter, REFIID riid, LPVOID *ppvObject)
457 IDefClFImpl *This = impl_from_IClassFactory(iface);
459 TRACE("%p->(%p,%s,%p)\n",This,pUnkOuter,shdebugstr_guid(riid),ppvObject);
463 if ( This->riidInst==NULL ||
464 IsEqualCLSID(riid, This->riidInst) ||
465 IsEqualCLSID(riid, &IID_IUnknown) )
467 return This->lpfnCI(pUnkOuter, riid, ppvObject);
470 ERR("unknown IID requested %s\n",shdebugstr_guid(riid));
471 return E_NOINTERFACE;
473 /******************************************************************************
474 * IDefClF_fnLockServer
476 static HRESULT WINAPI IDefClF_fnLockServer(LPCLASSFACTORY iface, BOOL fLock)
478 IDefClFImpl *This = impl_from_IClassFactory(iface);
479 TRACE("%p->(0x%x), not implemented\n",This, fLock);
483 static const IClassFactoryVtbl dclfvt =
485 IDefClF_fnQueryInterface,
488 IDefClF_fnCreateInstance,
492 /******************************************************************************
493 * SHCreateDefClassObject [SHELL32.70]
495 HRESULT WINAPI SHCreateDefClassObject(
498 LPFNCREATEINSTANCE lpfnCI, /* [in] create instance callback entry */
499 LPDWORD pcRefDll, /* [in/out] ref count of the dll */
500 REFIID riidInst) /* [in] optional interface to the instance */
504 TRACE("%s %p %p %p %s\n",
505 shdebugstr_guid(riid), ppv, lpfnCI, pcRefDll, shdebugstr_guid(riidInst));
507 if (! IsEqualCLSID(riid, &IID_IClassFactory) ) return E_NOINTERFACE;
508 if (! (pcf = IDefClF_fnConstructor(lpfnCI, (PLONG)pcRefDll, riidInst))) return E_OUTOFMEMORY;
513 /*************************************************************************
514 * DragAcceptFiles [SHELL32.@]
516 void WINAPI DragAcceptFiles(HWND hWnd, BOOL b)
520 if( !IsWindow(hWnd) ) return;
521 exstyle = GetWindowLongA(hWnd,GWL_EXSTYLE);
523 exstyle |= WS_EX_ACCEPTFILES;
525 exstyle &= ~WS_EX_ACCEPTFILES;
526 SetWindowLongA(hWnd,GWL_EXSTYLE,exstyle);
529 /*************************************************************************
530 * DragFinish [SHELL32.@]
532 void WINAPI DragFinish(HDROP h)
538 /*************************************************************************
539 * DragQueryPoint [SHELL32.@]
541 BOOL WINAPI DragQueryPoint(HDROP hDrop, POINT *p)
543 DROPFILES *lpDropFileStruct;
548 lpDropFileStruct = GlobalLock(hDrop);
550 *p = lpDropFileStruct->pt;
551 bRet = lpDropFileStruct->fNC;
557 /*************************************************************************
558 * DragQueryFileA [SHELL32.@]
559 * DragQueryFile [SHELL32.@]
561 UINT WINAPI DragQueryFileA(
569 DROPFILES *lpDropFileStruct = GlobalLock(hDrop);
571 TRACE("(%p, %x, %p, %u)\n", hDrop,lFile,lpszFile,lLength);
573 if(!lpDropFileStruct) goto end;
575 lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->pFiles;
577 if(lpDropFileStruct->fWide) {
578 LPWSTR lpszFileW = NULL;
581 lpszFileW = HeapAlloc(GetProcessHeap(), 0, lLength*sizeof(WCHAR));
582 if(lpszFileW == NULL) {
586 i = DragQueryFileW(hDrop, lFile, lpszFileW, lLength);
589 WideCharToMultiByte(CP_ACP, 0, lpszFileW, -1, lpszFile, lLength, 0, NULL);
590 HeapFree(GetProcessHeap(), 0, lpszFileW);
597 while (*lpDrop++); /* skip filename */
600 i = (lFile == 0xFFFFFFFF) ? i : 0;
606 if (!lpszFile ) goto end; /* needed buffer size */
607 lstrcpynA (lpszFile, lpDrop, lLength);
613 /*************************************************************************
614 * DragQueryFileW [SHELL32.@]
616 UINT WINAPI DragQueryFileW(
624 DROPFILES *lpDropFileStruct = GlobalLock(hDrop);
626 TRACE("(%p, %x, %p, %u)\n", hDrop,lFile,lpszwFile,lLength);
628 if(!lpDropFileStruct) goto end;
630 lpwDrop = (LPWSTR) ((LPSTR)lpDropFileStruct + lpDropFileStruct->pFiles);
632 if(lpDropFileStruct->fWide == FALSE) {
633 LPSTR lpszFileA = NULL;
636 lpszFileA = HeapAlloc(GetProcessHeap(), 0, lLength);
637 if(lpszFileA == NULL) {
641 i = DragQueryFileA(hDrop, lFile, lpszFileA, lLength);
644 MultiByteToWideChar(CP_ACP, 0, lpszFileA, -1, lpszwFile, lLength);
645 HeapFree(GetProcessHeap(), 0, lpszFileA);
653 while (*lpwDrop++); /* skip filename */
656 i = (lFile == 0xFFFFFFFF) ? i : 0;
661 i = strlenW(lpwDrop);
662 if ( !lpszwFile) goto end; /* needed buffer size */
663 lstrcpynW (lpszwFile, lpwDrop, lLength);
669 /*************************************************************************
670 * SHPropStgCreate [SHELL32.685]
672 HRESULT WINAPI SHPropStgCreate(IPropertySetStorage *psstg, REFFMTID fmtid,
673 const CLSID *pclsid, DWORD grfFlags, DWORD grfMode,
674 DWORD dwDisposition, IPropertyStorage **ppstg, UINT *puCodePage)
680 TRACE("%p %s %s %x %x %x %p %p\n", psstg, debugstr_guid(fmtid), debugstr_guid(pclsid),
681 grfFlags, grfMode, dwDisposition, ppstg, puCodePage);
683 hres = IPropertySetStorage_Open(psstg, fmtid, grfMode, ppstg);
685 switch(dwDisposition) {
687 if(SUCCEEDED(hres)) {
688 IPropertyStorage_Release(*ppstg);
689 hres = IPropertySetStorage_Delete(psstg, fmtid);
698 hres = IPropertySetStorage_Create(psstg, fmtid, pclsid,
699 grfFlags, grfMode, ppstg);
706 prop.ulKind = PRSPEC_PROPID;
707 prop.u.propid = PID_CODEPAGE;
708 hres = IPropertyStorage_ReadMultiple(*ppstg, 1, &prop, &ret);
709 if(FAILED(hres) || ret.vt!=VT_I2)
712 *puCodePage = ret.u.iVal;
719 /*************************************************************************
720 * SHPropStgReadMultiple [SHELL32.688]
722 HRESULT WINAPI SHPropStgReadMultiple(IPropertyStorage *pps, UINT uCodePage,
723 ULONG cpspec, const PROPSPEC *rgpspec, PROPVARIANT *rgvar)
728 FIXME("%p %u %u %p %p\n", pps, uCodePage, cpspec, rgpspec, rgvar);
730 memset(rgvar, 0, cpspec*sizeof(PROPVARIANT));
731 hres = IPropertyStorage_ReadMultiple(pps, cpspec, rgpspec, rgvar);
739 prop.ulKind = PRSPEC_PROPID;
740 prop.u.propid = PID_CODEPAGE;
741 hres = IPropertyStorage_ReadMultiple(pps, 1, &prop, &ret);
742 if(FAILED(hres) || ret.vt!=VT_I2)
745 uCodePage = ret.u.iVal;
748 hres = IPropertyStorage_Stat(pps, &stat);
752 /* TODO: do something with codepage and stat */
756 /*************************************************************************
757 * SHPropStgWriteMultiple [SHELL32.689]
759 HRESULT WINAPI SHPropStgWriteMultiple(IPropertyStorage *pps, UINT *uCodePage,
760 ULONG cpspec, const PROPSPEC *rgpspec, PROPVARIANT *rgvar, PROPID propidNameFirst)
766 FIXME("%p %p %u %p %p %d\n", pps, uCodePage, cpspec, rgpspec, rgvar, propidNameFirst);
768 hres = IPropertyStorage_Stat(pps, &stat);
772 if(uCodePage && *uCodePage)
773 codepage = *uCodePage;
778 prop.ulKind = PRSPEC_PROPID;
779 prop.u.propid = PID_CODEPAGE;
780 hres = IPropertyStorage_ReadMultiple(pps, 1, &prop, &ret);
783 if(ret.vt!=VT_I2 || !ret.u.iVal)
786 codepage = ret.u.iVal;
788 *uCodePage = codepage;
791 /* TODO: do something with codepage and stat */
793 hres = IPropertyStorage_WriteMultiple(pps, cpspec, rgpspec, rgvar, propidNameFirst);