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
41 #include "undocshell.h"
42 #include "wine/unicode.h"
43 #include "shell32_main.h"
45 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(shell);
51 extern INT WINAPI SHStringFromGUIDW(REFGUID guid, LPWSTR lpszDest, INT cchMax); /* shlwapi.24 */
53 /**************************************************************************
54 * Default ClassFactory types
56 typedef HRESULT (CALLBACK *LPFNCREATEINSTANCE)(IUnknown* pUnkOuter, REFIID riid, LPVOID* ppvObject);
57 static IClassFactory * IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI, PLONG pcRefDll, REFIID riidInst);
59 /* this table contains all CLSID's of shell32 objects */
62 LPFNCREATEINSTANCE lpfnCI;
63 } InterfaceTable[] = {
65 {&CLSID_AutoComplete, IAutoComplete_Constructor},
66 {&CLSID_ControlPanel, IControlPanel_Constructor},
67 {&CLSID_DragDropHelper, IDropTargetHelper_Constructor},
68 {&CLSID_FolderShortcut, FolderShortcut_Constructor},
69 {&CLSID_MyComputer, ISF_MyComputer_Constructor},
70 {&CLSID_MyDocuments, MyDocuments_Constructor},
71 {&CLSID_NetworkPlaces, ISF_NetworkPlaces_Constructor},
72 {&CLSID_Printers, Printers_Constructor},
73 {&CLSID_QueryAssociations, QueryAssociations_Constructor},
74 {&CLSID_RecycleBin, RecycleBin_Constructor},
75 {&CLSID_ShellDesktop, ISF_Desktop_Constructor},
76 {&CLSID_ShellFSFolder, IFSFolder_Constructor},
77 {&CLSID_ShellItem, IShellItem_Constructor},
78 {&CLSID_ShellLink, IShellLink_Constructor},
79 {&CLSID_UnixDosFolder, UnixDosFolder_Constructor},
80 {&CLSID_UnixFolder, UnixFolder_Constructor},
81 {&CLSID_ExplorerBrowser,ExplorerBrowser_Constructor},
82 {&CLSID_KnownFolderManager, KnownFolderManager_Constructor},
86 /*************************************************************************
87 * SHCoCreateInstance [SHELL32.102]
89 * Equivalent to CoCreateInstance. Under Windows 9x this function could sometimes
90 * use the shell32 built-in "mini-COM" without the need to load ole32.dll - see
91 * SHLoadOLE for details.
93 * Under wine if a "LoadWithoutCOM" value is present or the object resides in
94 * shell32.dll the function will load the object manually without the help of ole32
100 * CoCreateInstace, SHLoadOLE
102 HRESULT WINAPI SHCoCreateInstance(
111 const CLSID * myclsid = clsid;
112 WCHAR sKeyName[MAX_PATH];
113 const WCHAR sCLSID[7] = {'C','L','S','I','D','\\','\0'};
115 const WCHAR sInProcServer32[16] ={'\\','I','n','p','r','o','c','S','e','r','v','e','r','3','2','\0'};
116 const WCHAR sLoadWithoutCOM[15] ={'L','o','a','d','W','i','t','h','o','u','t','C','O','M','\0'};
117 WCHAR sDllPath[MAX_PATH];
120 IClassFactory * pcf = NULL;
122 if(!ppv) return E_POINTER;
125 /* if the clsid is a string, convert it */
128 if (!aclsid) return REGDB_E_CLASSNOTREG;
129 SHCLSIDFromStringW(aclsid, &iid);
133 TRACE("(%p,%s,unk:%p,%s,%p)\n",
134 aclsid,shdebugstr_guid(myclsid),pUnkOuter,shdebugstr_guid(refiid),ppv);
136 if (SUCCEEDED(DllGetClassObject(myclsid, &IID_IClassFactory,(LPVOID*)&pcf)))
138 hres = IClassFactory_CreateInstance(pcf, pUnkOuter, refiid, ppv);
139 IClassFactory_Release(pcf);
143 /* we look up the dll path in the registry */
144 SHStringFromGUIDW(myclsid, sClassID, sizeof(sClassID)/sizeof(WCHAR));
145 lstrcpyW(sKeyName, sCLSID);
146 lstrcatW(sKeyName, sClassID);
147 lstrcatW(sKeyName, sInProcServer32);
149 if (RegOpenKeyExW(HKEY_CLASSES_ROOT, sKeyName, 0, KEY_READ, &hKey))
150 return E_ACCESSDENIED;
152 /* if a special registry key is set, we load a shell extension without help of OLE32 */
153 if (!SHQueryValueExW(hKey, sLoadWithoutCOM, 0, 0, 0, 0))
155 /* load an external dll without ole32 */
157 typedef HRESULT (CALLBACK *DllGetClassObjectFunc)(REFCLSID clsid, REFIID iid, LPVOID *ppv);
158 DllGetClassObjectFunc DllGetClassObject;
160 dwSize = sizeof(sDllPath);
161 SHQueryValueExW(hKey, NULL, 0,0, sDllPath, &dwSize );
163 if ((hLibrary = LoadLibraryExW(sDllPath, 0, LOAD_WITH_ALTERED_SEARCH_PATH)) == 0) {
164 ERR("couldn't load InprocServer32 dll %s\n", debugstr_w(sDllPath));
165 hres = E_ACCESSDENIED;
167 } else if (!(DllGetClassObject = (DllGetClassObjectFunc)GetProcAddress(hLibrary, "DllGetClassObject"))) {
168 ERR("couldn't find function DllGetClassObject in %s\n", debugstr_w(sDllPath));
169 FreeLibrary( hLibrary );
170 hres = E_ACCESSDENIED;
172 } else if (FAILED(hres = DllGetClassObject(myclsid, &IID_IClassFactory, (LPVOID*)&pcf))) {
173 TRACE("GetClassObject failed 0x%08x\n", hres);
177 hres = IClassFactory_CreateInstance(pcf, pUnkOuter, refiid, ppv);
178 IClassFactory_Release(pcf);
181 /* load an external dll in the usual way */
182 hres = CoCreateInstance(myclsid, pUnkOuter, CLSCTX_INPROC_SERVER, refiid, ppv);
186 if (hKey) RegCloseKey(hKey);
189 ERR("failed (0x%08x) to create CLSID:%s IID:%s\n",
190 hres, shdebugstr_guid(myclsid), shdebugstr_guid(refiid));
191 ERR("class not found in registry\n");
194 TRACE("-- instance: %p\n",*ppv);
198 /*************************************************************************
199 * DllGetClassObject [SHELL32.@]
200 * SHDllGetClassObject [SHELL32.128]
202 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
204 HRESULT hres = E_OUTOFMEMORY;
205 IClassFactory * pcf = NULL;
208 TRACE("CLSID:%s,IID:%s\n",shdebugstr_guid(rclsid),shdebugstr_guid(iid));
210 if (!ppv) return E_INVALIDARG;
213 /* search our internal interface table */
214 for(i=0;InterfaceTable[i].riid;i++) {
215 if(IsEqualIID(InterfaceTable[i].riid, rclsid)) {
216 TRACE("index[%u]\n", i);
217 pcf = IDefClF_fnConstructor(InterfaceTable[i].lpfnCI, NULL, NULL);
222 FIXME("failed for CLSID=%s\n", shdebugstr_guid(rclsid));
223 return CLASS_E_CLASSNOTAVAILABLE;
226 hres = IClassFactory_QueryInterface(pcf, iid, ppv);
227 IClassFactory_Release(pcf);
229 TRACE("-- pointer to class factory: %p\n",*ppv);
233 /*************************************************************************
234 * SHCLSIDFromString [SHELL32.147]
236 * Under Windows 9x this was an ANSI version of CLSIDFromString. It also allowed
237 * to avoid dependency on ole32.dll (see SHLoadOLE for details).
239 * Under Windows NT/2000/XP this is equivalent to CLSIDFromString
242 * exported by ordinal
245 * CLSIDFromString, SHLoadOLE
247 DWORD WINAPI SHCLSIDFromStringA (LPCSTR clsid, CLSID *id)
250 TRACE("(%p(%s) %p)\n", clsid, clsid, id);
251 if (!MultiByteToWideChar( CP_ACP, 0, clsid, -1, buffer, sizeof(buffer)/sizeof(WCHAR) ))
252 return CO_E_CLASSSTRING;
253 return CLSIDFromString( buffer, id );
255 DWORD WINAPI SHCLSIDFromStringW (LPCWSTR clsid, CLSID *id)
257 TRACE("(%p(%s) %p)\n", clsid, debugstr_w(clsid), id);
258 return CLSIDFromString(clsid, id);
260 DWORD WINAPI SHCLSIDFromStringAW (LPCVOID clsid, CLSID *id)
262 if (SHELL_OsIsUnicode())
263 return SHCLSIDFromStringW (clsid, id);
264 return SHCLSIDFromStringA (clsid, id);
267 /*************************************************************************
268 * SHGetMalloc [SHELL32.@]
270 * Equivalent to CoGetMalloc(MEMCTX_TASK, ...). Under Windows 9x this function
271 * could use the shell32 built-in "mini-COM" without the need to load ole32.dll -
272 * see SHLoadOLE for details.
275 * lpmal [O] Destination for IMalloc interface.
278 * Success: S_OK. lpmal contains the shells IMalloc interface.
279 * Failure. An HRESULT error code.
282 * CoGetMalloc, SHLoadOLE
284 HRESULT WINAPI SHGetMalloc(LPMALLOC *lpmal)
286 TRACE("(%p)\n", lpmal);
287 return CoGetMalloc(MEMCTX_TASK, lpmal);
290 /*************************************************************************
291 * SHAlloc [SHELL32.196]
293 * Equivalent to CoTaskMemAlloc. Under Windows 9x this function could use
294 * the shell32 built-in "mini-COM" without the need to load ole32.dll -
295 * see SHLoadOLE for details.
298 * exported by ordinal
301 * CoTaskMemAlloc, SHLoadOLE
303 LPVOID WINAPI SHAlloc(DWORD len)
307 ret = CoTaskMemAlloc(len);
308 TRACE("%u bytes at %p\n",len, ret);
312 /*************************************************************************
313 * SHFree [SHELL32.195]
315 * Equivalent to CoTaskMemFree. Under Windows 9x this function could use
316 * the shell32 built-in "mini-COM" without the need to load ole32.dll -
317 * see SHLoadOLE for details.
320 * exported by ordinal
323 * CoTaskMemFree, SHLoadOLE
325 void WINAPI SHFree(LPVOID pv)
331 /*************************************************************************
332 * SHGetDesktopFolder [SHELL32.@]
334 HRESULT WINAPI SHGetDesktopFolder(IShellFolder **psf)
338 TRACE("(%p)\n", psf);
340 if(!psf) return E_INVALIDARG;
343 hres = ISF_Desktop_Constructor(NULL, &IID_IShellFolder, (LPVOID*)psf);
345 TRACE("-- %p->(%p) 0x%08x\n", psf, *psf, hres);
348 /**************************************************************************
349 * Default ClassFactory Implementation
351 * SHCreateDefClassObject
354 * Helper function for dlls without their own classfactory.
355 * A generic classfactory is returned.
356 * When the CreateInstance of the cf is called the callback is executed.
361 const IClassFactoryVtbl *lpVtbl;
364 LPFNCREATEINSTANCE lpfnCI;
365 const IID * riidInst;
366 LONG * pcRefDll; /* pointer to refcounter in external dll (ugrrr...) */
369 static const IClassFactoryVtbl dclfvt;
371 /**************************************************************************
372 * IDefClF_fnConstructor
375 static IClassFactory * IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI, PLONG pcRefDll, REFIID riidInst)
379 lpclf = HeapAlloc(GetProcessHeap(),0,sizeof(IDefClFImpl));
381 lpclf->lpVtbl = &dclfvt;
382 lpclf->lpfnCI = lpfnCI;
383 lpclf->pcRefDll = pcRefDll;
385 if (pcRefDll) InterlockedIncrement(pcRefDll);
386 lpclf->riidInst = riidInst;
388 TRACE("(%p)%s\n",lpclf, shdebugstr_guid(riidInst));
389 return (LPCLASSFACTORY)lpclf;
391 /**************************************************************************
392 * IDefClF_fnQueryInterface
394 static HRESULT WINAPI IDefClF_fnQueryInterface(
395 LPCLASSFACTORY iface, REFIID riid, LPVOID *ppvObj)
397 IDefClFImpl *This = (IDefClFImpl *)iface;
399 TRACE("(%p)->(%s)\n",This,shdebugstr_guid(riid));
403 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory)) {
405 InterlockedIncrement(&This->ref);
409 TRACE("-- E_NOINTERFACE\n");
410 return E_NOINTERFACE;
412 /******************************************************************************
415 static ULONG WINAPI IDefClF_fnAddRef(LPCLASSFACTORY iface)
417 IDefClFImpl *This = (IDefClFImpl *)iface;
418 ULONG refCount = InterlockedIncrement(&This->ref);
420 TRACE("(%p)->(count=%u)\n", This, refCount - 1);
424 /******************************************************************************
427 static ULONG WINAPI IDefClF_fnRelease(LPCLASSFACTORY iface)
429 IDefClFImpl *This = (IDefClFImpl *)iface;
430 ULONG refCount = InterlockedDecrement(&This->ref);
432 TRACE("(%p)->(count=%u)\n", This, refCount + 1);
436 if (This->pcRefDll) InterlockedDecrement(This->pcRefDll);
438 TRACE("-- destroying IClassFactory(%p)\n",This);
439 HeapFree(GetProcessHeap(),0,This);
444 /******************************************************************************
445 * IDefClF_fnCreateInstance
447 static HRESULT WINAPI IDefClF_fnCreateInstance(
448 LPCLASSFACTORY iface, LPUNKNOWN pUnkOuter, REFIID riid, LPVOID *ppvObject)
450 IDefClFImpl *This = (IDefClFImpl *)iface;
452 TRACE("%p->(%p,%s,%p)\n",This,pUnkOuter,shdebugstr_guid(riid),ppvObject);
456 if ( This->riidInst==NULL ||
457 IsEqualCLSID(riid, This->riidInst) ||
458 IsEqualCLSID(riid, &IID_IUnknown) )
460 return This->lpfnCI(pUnkOuter, riid, ppvObject);
463 ERR("unknown IID requested %s\n",shdebugstr_guid(riid));
464 return E_NOINTERFACE;
466 /******************************************************************************
467 * IDefClF_fnLockServer
469 static HRESULT WINAPI IDefClF_fnLockServer(LPCLASSFACTORY iface, BOOL fLock)
471 IDefClFImpl *This = (IDefClFImpl *)iface;
472 TRACE("%p->(0x%x), not implemented\n",This, fLock);
476 static const IClassFactoryVtbl dclfvt =
478 IDefClF_fnQueryInterface,
481 IDefClF_fnCreateInstance,
485 /******************************************************************************
486 * SHCreateDefClassObject [SHELL32.70]
488 HRESULT WINAPI SHCreateDefClassObject(
491 LPFNCREATEINSTANCE lpfnCI, /* [in] create instance callback entry */
492 LPDWORD pcRefDll, /* [in/out] ref count of the dll */
493 REFIID riidInst) /* [in] optional interface to the instance */
497 TRACE("%s %p %p %p %s\n",
498 shdebugstr_guid(riid), ppv, lpfnCI, pcRefDll, shdebugstr_guid(riidInst));
500 if (! IsEqualCLSID(riid, &IID_IClassFactory) ) return E_NOINTERFACE;
501 if (! (pcf = IDefClF_fnConstructor(lpfnCI, (PLONG)pcRefDll, riidInst))) return E_OUTOFMEMORY;
506 /*************************************************************************
507 * DragAcceptFiles [SHELL32.@]
509 void WINAPI DragAcceptFiles(HWND hWnd, BOOL b)
513 if( !IsWindow(hWnd) ) return;
514 exstyle = GetWindowLongA(hWnd,GWL_EXSTYLE);
516 exstyle |= WS_EX_ACCEPTFILES;
518 exstyle &= ~WS_EX_ACCEPTFILES;
519 SetWindowLongA(hWnd,GWL_EXSTYLE,exstyle);
522 /*************************************************************************
523 * DragFinish [SHELL32.@]
525 void WINAPI DragFinish(HDROP h)
531 /*************************************************************************
532 * DragQueryPoint [SHELL32.@]
534 BOOL WINAPI DragQueryPoint(HDROP hDrop, POINT *p)
536 DROPFILES *lpDropFileStruct;
541 lpDropFileStruct = GlobalLock(hDrop);
543 *p = lpDropFileStruct->pt;
544 bRet = lpDropFileStruct->fNC;
550 /*************************************************************************
551 * DragQueryFileA [SHELL32.@]
552 * DragQueryFile [SHELL32.@]
554 UINT WINAPI DragQueryFileA(
562 DROPFILES *lpDropFileStruct = GlobalLock(hDrop);
564 TRACE("(%p, %x, %p, %u)\n", hDrop,lFile,lpszFile,lLength);
566 if(!lpDropFileStruct) goto end;
568 lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->pFiles;
570 if(lpDropFileStruct->fWide) {
571 LPWSTR lpszFileW = NULL;
574 lpszFileW = HeapAlloc(GetProcessHeap(), 0, lLength*sizeof(WCHAR));
575 if(lpszFileW == NULL) {
579 i = DragQueryFileW(hDrop, lFile, lpszFileW, lLength);
582 WideCharToMultiByte(CP_ACP, 0, lpszFileW, -1, lpszFile, lLength, 0, NULL);
583 HeapFree(GetProcessHeap(), 0, lpszFileW);
590 while (*lpDrop++); /* skip filename */
593 i = (lFile == 0xFFFFFFFF) ? i : 0;
599 if (!lpszFile ) goto end; /* needed buffer size */
600 lstrcpynA (lpszFile, lpDrop, lLength);
606 /*************************************************************************
607 * DragQueryFileW [SHELL32.@]
609 UINT WINAPI DragQueryFileW(
617 DROPFILES *lpDropFileStruct = GlobalLock(hDrop);
619 TRACE("(%p, %x, %p, %u)\n", hDrop,lFile,lpszwFile,lLength);
621 if(!lpDropFileStruct) goto end;
623 lpwDrop = (LPWSTR) ((LPSTR)lpDropFileStruct + lpDropFileStruct->pFiles);
625 if(lpDropFileStruct->fWide == FALSE) {
626 LPSTR lpszFileA = NULL;
629 lpszFileA = HeapAlloc(GetProcessHeap(), 0, lLength);
630 if(lpszFileA == NULL) {
634 i = DragQueryFileA(hDrop, lFile, lpszFileA, lLength);
637 MultiByteToWideChar(CP_ACP, 0, lpszFileA, -1, lpszwFile, lLength);
638 HeapFree(GetProcessHeap(), 0, lpszFileA);
646 while (*lpwDrop++); /* skip filename */
649 i = (lFile == 0xFFFFFFFF) ? i : 0;
654 i = strlenW(lpwDrop);
655 if ( !lpszwFile) goto end; /* needed buffer size */
656 lstrcpynW (lpszwFile, lpwDrop, lLength);
662 /*************************************************************************
663 * SHPropStgCreate [SHELL32.685]
665 HRESULT WINAPI SHPropStgCreate(IPropertySetStorage *psstg, REFFMTID fmtid,
666 const CLSID *pclsid, DWORD grfFlags, DWORD grfMode,
667 DWORD dwDisposition, IPropertyStorage **ppstg, UINT *puCodePage)
673 TRACE("%p %s %s %x %x %x %p %p\n", psstg, debugstr_guid(fmtid), debugstr_guid(pclsid),
674 grfFlags, grfMode, dwDisposition, ppstg, puCodePage);
676 hres = IPropertySetStorage_Open(psstg, fmtid, grfMode, ppstg);
678 switch(dwDisposition) {
680 if(SUCCEEDED(hres)) {
681 IPropertyStorage_Release(*ppstg);
682 hres = IPropertySetStorage_Delete(psstg, fmtid);
691 hres = IPropertySetStorage_Create(psstg, fmtid, pclsid,
692 grfFlags, grfMode, ppstg);
699 prop.ulKind = PRSPEC_PROPID;
700 prop.u.propid = PID_CODEPAGE;
701 hres = IPropertyStorage_ReadMultiple(*ppstg, 1, &prop, &ret);
702 if(FAILED(hres) || ret.vt!=VT_I2)
705 *puCodePage = ret.u.iVal;
712 /*************************************************************************
713 * SHPropStgReadMultiple [SHELL32.688]
715 HRESULT WINAPI SHPropStgReadMultiple(IPropertyStorage *pps, UINT uCodePage,
716 ULONG cpspec, const PROPSPEC *rgpspec, PROPVARIANT *rgvar)
721 FIXME("%p %u %u %p %p\n", pps, uCodePage, cpspec, rgpspec, rgvar);
723 memset(rgvar, 0, cpspec*sizeof(PROPVARIANT));
724 hres = IPropertyStorage_ReadMultiple(pps, cpspec, rgpspec, rgvar);
732 prop.ulKind = PRSPEC_PROPID;
733 prop.u.propid = PID_CODEPAGE;
734 hres = IPropertyStorage_ReadMultiple(pps, 1, &prop, &ret);
735 if(FAILED(hres) || ret.vt!=VT_I2)
738 uCodePage = ret.u.iVal;
741 hres = IPropertyStorage_Stat(pps, &stat);
745 /* TODO: do something with codepage and stat */
749 /*************************************************************************
750 * SHPropStgWriteMultiple [SHELL32.689]
752 HRESULT WINAPI SHPropStgWriteMultiple(IPropertyStorage *pps, UINT *uCodePage,
753 ULONG cpspec, const PROPSPEC *rgpspec, PROPVARIANT *rgvar, PROPID propidNameFirst)
759 FIXME("%p %p %u %p %p %d\n", pps, uCodePage, cpspec, rgpspec, rgvar, propidNameFirst);
761 hres = IPropertyStorage_Stat(pps, &stat);
765 if(uCodePage && *uCodePage)
766 codepage = *uCodePage;
771 prop.ulKind = PRSPEC_PROPID;
772 prop.u.propid = PID_CODEPAGE;
773 hres = IPropertyStorage_ReadMultiple(pps, 1, &prop, &ret);
776 if(ret.vt!=VT_I2 || !ret.u.iVal)
779 codepage = ret.u.iVal;
781 *uCodePage = codepage;
784 /* TODO: do something with codepage and stat */
786 hres = IPropertyStorage_WriteMultiple(pps, cpspec, rgpspec, rgvar, propidNameFirst);