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
},
81 {&CLSID_KnownFolderManager
, KnownFolderManager_Constructor
},
85 /*************************************************************************
86 * SHCoCreateInstance [SHELL32.102]
88 * Equivalent to CoCreateInstance. Under Windows 9x this function could sometimes
89 * use the shell32 built-in "mini-COM" without the need to load ole32.dll - see
90 * SHLoadOLE for details.
92 * Under wine if a "LoadWithoutCOM" value is present or the object resides in
93 * shell32.dll the function will load the object manually without the help of ole32
99 * CoCreateInstace, SHLoadOLE
101 HRESULT WINAPI
SHCoCreateInstance(
110 const CLSID
* myclsid
= clsid
;
111 WCHAR sKeyName
[MAX_PATH
];
112 const WCHAR sCLSID
[7] = {'C','L','S','I','D','\\','\0'};
114 const WCHAR sInProcServer32
[16] ={'\\','I','n','p','r','o','c','S','e','r','v','e','r','3','2','\0'};
115 const WCHAR sLoadWithoutCOM
[15] ={'L','o','a','d','W','i','t','h','o','u','t','C','O','M','\0'};
116 WCHAR sDllPath
[MAX_PATH
];
119 IClassFactory
* pcf
= NULL
;
121 if(!ppv
) return E_POINTER
;
124 /* if the clsid is a string, convert it */
127 if (!aclsid
) return REGDB_E_CLASSNOTREG
;
128 SHCLSIDFromStringW(aclsid
, &iid
);
132 TRACE("(%p,%s,unk:%p,%s,%p)\n",
133 aclsid
,shdebugstr_guid(myclsid
),pUnkOuter
,shdebugstr_guid(refiid
),ppv
);
135 if (SUCCEEDED(DllGetClassObject(myclsid
, &IID_IClassFactory
,(LPVOID
*)&pcf
)))
137 hres
= IClassFactory_CreateInstance(pcf
, pUnkOuter
, refiid
, ppv
);
138 IClassFactory_Release(pcf
);
142 /* we look up the dll path in the registry */
143 SHStringFromGUIDW(myclsid
, sClassID
, sizeof(sClassID
)/sizeof(WCHAR
));
144 lstrcpyW(sKeyName
, sCLSID
);
145 lstrcatW(sKeyName
, sClassID
);
146 lstrcatW(sKeyName
, sInProcServer32
);
148 if (RegOpenKeyExW(HKEY_CLASSES_ROOT
, sKeyName
, 0, KEY_READ
, &hKey
))
149 return E_ACCESSDENIED
;
151 /* if a special registry key is set, we load a shell extension without help of OLE32 */
152 if (!SHQueryValueExW(hKey
, sLoadWithoutCOM
, 0, 0, 0, 0))
154 /* load an external dll without ole32 */
156 typedef HRESULT (CALLBACK
*DllGetClassObjectFunc
)(REFCLSID clsid
, REFIID iid
, LPVOID
*ppv
);
157 DllGetClassObjectFunc DllGetClassObject
;
159 dwSize
= sizeof(sDllPath
);
160 SHQueryValueExW(hKey
, NULL
, 0,0, sDllPath
, &dwSize
);
162 if ((hLibrary
= LoadLibraryExW(sDllPath
, 0, LOAD_WITH_ALTERED_SEARCH_PATH
)) == 0) {
163 ERR("couldn't load InprocServer32 dll %s\n", debugstr_w(sDllPath
));
164 hres
= E_ACCESSDENIED
;
166 } else if (!(DllGetClassObject
= (DllGetClassObjectFunc
)GetProcAddress(hLibrary
, "DllGetClassObject"))) {
167 ERR("couldn't find function DllGetClassObject in %s\n", debugstr_w(sDllPath
));
168 FreeLibrary( hLibrary
);
169 hres
= E_ACCESSDENIED
;
171 } else if (FAILED(hres
= DllGetClassObject(myclsid
, &IID_IClassFactory
, (LPVOID
*)&pcf
))) {
172 TRACE("GetClassObject failed 0x%08x\n", hres
);
176 hres
= IClassFactory_CreateInstance(pcf
, pUnkOuter
, refiid
, ppv
);
177 IClassFactory_Release(pcf
);
180 /* load an external dll in the usual way */
181 hres
= CoCreateInstance(myclsid
, pUnkOuter
, CLSCTX_INPROC_SERVER
, refiid
, ppv
);
185 if (hKey
) RegCloseKey(hKey
);
188 ERR("failed (0x%08x) to create CLSID:%s IID:%s\n",
189 hres
, shdebugstr_guid(myclsid
), shdebugstr_guid(refiid
));
190 ERR("class not found in registry\n");
193 TRACE("-- instance: %p\n",*ppv
);
197 /*************************************************************************
198 * DllGetClassObject [SHELL32.@]
199 * SHDllGetClassObject [SHELL32.128]
201 HRESULT WINAPI
DllGetClassObject(REFCLSID rclsid
, REFIID iid
, LPVOID
*ppv
)
203 HRESULT hres
= E_OUTOFMEMORY
;
204 IClassFactory
* pcf
= NULL
;
207 TRACE("CLSID:%s,IID:%s\n",shdebugstr_guid(rclsid
),shdebugstr_guid(iid
));
209 if (!ppv
) return E_INVALIDARG
;
212 /* search our internal interface table */
213 for(i
=0;InterfaceTable
[i
].riid
;i
++) {
214 if(IsEqualIID(InterfaceTable
[i
].riid
, rclsid
)) {
215 TRACE("index[%u]\n", i
);
216 pcf
= IDefClF_fnConstructor(InterfaceTable
[i
].lpfnCI
, NULL
, NULL
);
221 FIXME("failed for CLSID=%s\n", shdebugstr_guid(rclsid
));
222 return CLASS_E_CLASSNOTAVAILABLE
;
225 hres
= IClassFactory_QueryInterface(pcf
, iid
, ppv
);
226 IClassFactory_Release(pcf
);
228 TRACE("-- pointer to class factory: %p\n",*ppv
);
232 /*************************************************************************
233 * SHCLSIDFromString [SHELL32.147]
235 * Under Windows 9x this was an ANSI version of CLSIDFromString. It also allowed
236 * to avoid dependency on ole32.dll (see SHLoadOLE for details).
238 * Under Windows NT/2000/XP this is equivalent to CLSIDFromString
241 * exported by ordinal
244 * CLSIDFromString, SHLoadOLE
246 DWORD WINAPI
SHCLSIDFromStringA (LPCSTR clsid
, CLSID
*id
)
249 TRACE("(%p(%s) %p)\n", clsid
, clsid
, id
);
250 if (!MultiByteToWideChar( CP_ACP
, 0, clsid
, -1, buffer
, sizeof(buffer
)/sizeof(WCHAR
) ))
251 return CO_E_CLASSSTRING
;
252 return CLSIDFromString( buffer
, id
);
254 DWORD WINAPI
SHCLSIDFromStringW (LPCWSTR clsid
, CLSID
*id
)
256 TRACE("(%p(%s) %p)\n", clsid
, debugstr_w(clsid
), id
);
257 return CLSIDFromString(clsid
, id
);
259 DWORD WINAPI
SHCLSIDFromStringAW (LPCVOID clsid
, CLSID
*id
)
261 if (SHELL_OsIsUnicode())
262 return SHCLSIDFromStringW (clsid
, id
);
263 return SHCLSIDFromStringA (clsid
, id
);
266 /*************************************************************************
267 * SHGetMalloc [SHELL32.@]
269 * Equivalent to CoGetMalloc(MEMCTX_TASK, ...). Under Windows 9x this function
270 * could use the shell32 built-in "mini-COM" without the need to load ole32.dll -
271 * see SHLoadOLE for details.
274 * lpmal [O] Destination for IMalloc interface.
277 * Success: S_OK. lpmal contains the shells IMalloc interface.
278 * Failure. An HRESULT error code.
281 * CoGetMalloc, SHLoadOLE
283 HRESULT WINAPI
SHGetMalloc(LPMALLOC
*lpmal
)
285 TRACE("(%p)\n", lpmal
);
286 return CoGetMalloc(MEMCTX_TASK
, lpmal
);
289 /*************************************************************************
290 * SHAlloc [SHELL32.196]
292 * Equivalent to CoTaskMemAlloc. Under Windows 9x this function could use
293 * the shell32 built-in "mini-COM" without the need to load ole32.dll -
294 * see SHLoadOLE for details.
297 * exported by ordinal
300 * CoTaskMemAlloc, SHLoadOLE
302 LPVOID WINAPI
SHAlloc(DWORD len
)
306 ret
= CoTaskMemAlloc(len
);
307 TRACE("%u bytes at %p\n",len
, ret
);
311 /*************************************************************************
312 * SHFree [SHELL32.195]
314 * Equivalent to CoTaskMemFree. Under Windows 9x this function could use
315 * the shell32 built-in "mini-COM" without the need to load ole32.dll -
316 * see SHLoadOLE for details.
319 * exported by ordinal
322 * CoTaskMemFree, SHLoadOLE
324 void WINAPI
SHFree(LPVOID pv
)
330 /*************************************************************************
331 * SHGetDesktopFolder [SHELL32.@]
333 HRESULT WINAPI
SHGetDesktopFolder(IShellFolder
**psf
)
337 TRACE("(%p)\n", psf
);
339 if(!psf
) return E_INVALIDARG
;
342 hres
= ISF_Desktop_Constructor(NULL
, &IID_IShellFolder
, (LPVOID
*)psf
);
344 TRACE("-- %p->(%p) 0x%08x\n", psf
, *psf
, hres
);
347 /**************************************************************************
348 * Default ClassFactory Implementation
350 * SHCreateDefClassObject
353 * Helper function for dlls without their own classfactory.
354 * A generic classfactory is returned.
355 * When the CreateInstance of the cf is called the callback is executed.
360 const IClassFactoryVtbl
*lpVtbl
;
363 LPFNCREATEINSTANCE lpfnCI
;
364 const IID
* riidInst
;
365 LONG
* pcRefDll
; /* pointer to refcounter in external dll (ugrrr...) */
368 static const IClassFactoryVtbl dclfvt
;
370 /**************************************************************************
371 * IDefClF_fnConstructor
374 static IClassFactory
* IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI
, PLONG pcRefDll
, REFIID riidInst
)
378 lpclf
= HeapAlloc(GetProcessHeap(),0,sizeof(IDefClFImpl
));
380 lpclf
->lpVtbl
= &dclfvt
;
381 lpclf
->lpfnCI
= lpfnCI
;
382 lpclf
->pcRefDll
= pcRefDll
;
384 if (pcRefDll
) InterlockedIncrement(pcRefDll
);
385 lpclf
->riidInst
= riidInst
;
387 TRACE("(%p)%s\n",lpclf
, shdebugstr_guid(riidInst
));
388 return (LPCLASSFACTORY
)lpclf
;
390 /**************************************************************************
391 * IDefClF_fnQueryInterface
393 static HRESULT WINAPI
IDefClF_fnQueryInterface(
394 LPCLASSFACTORY iface
, REFIID riid
, LPVOID
*ppvObj
)
396 IDefClFImpl
*This
= (IDefClFImpl
*)iface
;
398 TRACE("(%p)->(%s)\n",This
,shdebugstr_guid(riid
));
402 if(IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IClassFactory
)) {
404 InterlockedIncrement(&This
->ref
);
408 TRACE("-- E_NOINTERFACE\n");
409 return E_NOINTERFACE
;
411 /******************************************************************************
414 static ULONG WINAPI
IDefClF_fnAddRef(LPCLASSFACTORY iface
)
416 IDefClFImpl
*This
= (IDefClFImpl
*)iface
;
417 ULONG refCount
= InterlockedIncrement(&This
->ref
);
419 TRACE("(%p)->(count=%u)\n", This
, refCount
- 1);
423 /******************************************************************************
426 static ULONG WINAPI
IDefClF_fnRelease(LPCLASSFACTORY iface
)
428 IDefClFImpl
*This
= (IDefClFImpl
*)iface
;
429 ULONG refCount
= InterlockedDecrement(&This
->ref
);
431 TRACE("(%p)->(count=%u)\n", This
, refCount
+ 1);
435 if (This
->pcRefDll
) InterlockedDecrement(This
->pcRefDll
);
437 TRACE("-- destroying IClassFactory(%p)\n",This
);
438 HeapFree(GetProcessHeap(),0,This
);
443 /******************************************************************************
444 * IDefClF_fnCreateInstance
446 static HRESULT WINAPI
IDefClF_fnCreateInstance(
447 LPCLASSFACTORY iface
, LPUNKNOWN pUnkOuter
, REFIID riid
, LPVOID
*ppvObject
)
449 IDefClFImpl
*This
= (IDefClFImpl
*)iface
;
451 TRACE("%p->(%p,%s,%p)\n",This
,pUnkOuter
,shdebugstr_guid(riid
),ppvObject
);
455 if ( This
->riidInst
==NULL
||
456 IsEqualCLSID(riid
, This
->riidInst
) ||
457 IsEqualCLSID(riid
, &IID_IUnknown
) )
459 return This
->lpfnCI(pUnkOuter
, riid
, ppvObject
);
462 ERR("unknown IID requested %s\n",shdebugstr_guid(riid
));
463 return E_NOINTERFACE
;
465 /******************************************************************************
466 * IDefClF_fnLockServer
468 static HRESULT WINAPI
IDefClF_fnLockServer(LPCLASSFACTORY iface
, BOOL fLock
)
470 IDefClFImpl
*This
= (IDefClFImpl
*)iface
;
471 TRACE("%p->(0x%x), not implemented\n",This
, fLock
);
475 static const IClassFactoryVtbl dclfvt
=
477 IDefClF_fnQueryInterface
,
480 IDefClF_fnCreateInstance
,
484 /******************************************************************************
485 * SHCreateDefClassObject [SHELL32.70]
487 HRESULT WINAPI
SHCreateDefClassObject(
490 LPFNCREATEINSTANCE lpfnCI
, /* [in] create instance callback entry */
491 LPDWORD pcRefDll
, /* [in/out] ref count of the dll */
492 REFIID riidInst
) /* [in] optional interface to the instance */
496 TRACE("%s %p %p %p %s\n",
497 shdebugstr_guid(riid
), ppv
, lpfnCI
, pcRefDll
, shdebugstr_guid(riidInst
));
499 if (! IsEqualCLSID(riid
, &IID_IClassFactory
) ) return E_NOINTERFACE
;
500 if (! (pcf
= IDefClF_fnConstructor(lpfnCI
, (PLONG
)pcRefDll
, riidInst
))) return E_OUTOFMEMORY
;
505 /*************************************************************************
506 * DragAcceptFiles [SHELL32.@]
508 void WINAPI
DragAcceptFiles(HWND hWnd
, BOOL b
)
512 if( !IsWindow(hWnd
) ) return;
513 exstyle
= GetWindowLongA(hWnd
,GWL_EXSTYLE
);
515 exstyle
|= WS_EX_ACCEPTFILES
;
517 exstyle
&= ~WS_EX_ACCEPTFILES
;
518 SetWindowLongA(hWnd
,GWL_EXSTYLE
,exstyle
);
521 /*************************************************************************
522 * DragFinish [SHELL32.@]
524 void WINAPI
DragFinish(HDROP h
)
530 /*************************************************************************
531 * DragQueryPoint [SHELL32.@]
533 BOOL WINAPI
DragQueryPoint(HDROP hDrop
, POINT
*p
)
535 DROPFILES
*lpDropFileStruct
;
540 lpDropFileStruct
= GlobalLock(hDrop
);
542 *p
= lpDropFileStruct
->pt
;
543 bRet
= lpDropFileStruct
->fNC
;
549 /*************************************************************************
550 * DragQueryFileA [SHELL32.@]
551 * DragQueryFile [SHELL32.@]
553 UINT WINAPI
DragQueryFileA(
561 DROPFILES
*lpDropFileStruct
= GlobalLock(hDrop
);
563 TRACE("(%p, %x, %p, %u)\n", hDrop
,lFile
,lpszFile
,lLength
);
565 if(!lpDropFileStruct
) goto end
;
567 lpDrop
= (LPSTR
) lpDropFileStruct
+ lpDropFileStruct
->pFiles
;
569 if(lpDropFileStruct
->fWide
) {
570 LPWSTR lpszFileW
= NULL
;
573 lpszFileW
= HeapAlloc(GetProcessHeap(), 0, lLength
*sizeof(WCHAR
));
574 if(lpszFileW
== NULL
) {
578 i
= DragQueryFileW(hDrop
, lFile
, lpszFileW
, lLength
);
581 WideCharToMultiByte(CP_ACP
, 0, lpszFileW
, -1, lpszFile
, lLength
, 0, NULL
);
582 HeapFree(GetProcessHeap(), 0, lpszFileW
);
589 while (*lpDrop
++); /* skip filename */
592 i
= (lFile
== 0xFFFFFFFF) ? i
: 0;
598 if (!lpszFile
) goto end
; /* needed buffer size */
599 lstrcpynA (lpszFile
, lpDrop
, lLength
);
605 /*************************************************************************
606 * DragQueryFileW [SHELL32.@]
608 UINT WINAPI
DragQueryFileW(
616 DROPFILES
*lpDropFileStruct
= GlobalLock(hDrop
);
618 TRACE("(%p, %x, %p, %u)\n", hDrop
,lFile
,lpszwFile
,lLength
);
620 if(!lpDropFileStruct
) goto end
;
622 lpwDrop
= (LPWSTR
) ((LPSTR
)lpDropFileStruct
+ lpDropFileStruct
->pFiles
);
624 if(lpDropFileStruct
->fWide
== FALSE
) {
625 LPSTR lpszFileA
= NULL
;
628 lpszFileA
= HeapAlloc(GetProcessHeap(), 0, lLength
);
629 if(lpszFileA
== NULL
) {
633 i
= DragQueryFileA(hDrop
, lFile
, lpszFileA
, lLength
);
636 MultiByteToWideChar(CP_ACP
, 0, lpszFileA
, -1, lpszwFile
, lLength
);
637 HeapFree(GetProcessHeap(), 0, lpszFileA
);
645 while (*lpwDrop
++); /* skip filename */
648 i
= (lFile
== 0xFFFFFFFF) ? i
: 0;
653 i
= strlenW(lpwDrop
);
654 if ( !lpszwFile
) goto end
; /* needed buffer size */
655 lstrcpynW (lpszwFile
, lpwDrop
, lLength
);