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
24 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
26 extern HRESULT WINAPI
IFSFolder_Constructor(IUnknown
* pUnkOuter
, REFIID riid
, LPVOID
* ppv
);
28 static const WCHAR sShell32
[12] = {'S','H','E','L','L','3','2','.','D','L','L','\0'};
29 static const GUID dummy1
= {0xD969A300, 0xE7FF, 0x11d0, {0xA9, 0x3B, 0x00, 0xA0, 0xC9, 0x0F, 0x27, 0x19} };
31 /**************************************************************************
32 * Default ClassFactory types
34 typedef HRESULT (CALLBACK
*LPFNCREATEINSTANCE
)(IUnknown
* pUnkOuter
, REFIID riid
, LPVOID
* ppvObject
);
35 static IClassFactory
* IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI
, PLONG pcRefDll
, REFIID riidInst
);
37 /* this table contains all CLSID's of shell32 objects */
40 LPFNCREATEINSTANCE lpfnCI
;
41 } InterfaceTable
[] = {
42 {&CLSID_ShellFSFolder
, &IFSFolder_Constructor
},
43 {&CLSID_MyComputer
, &ISF_MyComputer_Constructor
},
44 {&CLSID_NetworkPlaces
, &ISF_NetworkPlaces_Constructor
},
45 {&CLSID_ShellDesktop
, &ISF_Desktop_Constructor
},
46 {&CLSID_ShellItem
, &IShellItem_Constructor
},
47 {&CLSID_ShellLink
, &IShellLink_Constructor
},
48 {&CLSID_DragDropHelper
, &IDropTargetHelper_Constructor
},
49 {&CLSID_ControlPanel
, &IControlPanel_Constructor
},
50 {&CLSID_AutoComplete
, &IAutoComplete_Constructor
},
52 {&CLSID_UnixFolder
, &UnixFolder_Constructor
},
53 {&CLSID_UnixDosFolder
, &UnixDosFolder_Constructor
},
54 {&CLSID_FolderShortcut
, &FolderShortcut_Constructor
},
56 {&CLSID_MyDocuments
, &ISF_MyDocuments_Constructor
},
57 {&CLSID_FontsFolderShortcut
, &ISF_Fonts_Constructor
},
58 {&CLSID_Printers
, &ISF_Printers_Constructor
},
59 {&CLSID_AdminFolderShortcut
, &ISF_AdminTools_Constructor
},
60 {&CLSID_RecycleBin
, &RecycleBin_Constructor
},
61 {&CLSID_OpenWith
, &SHEOW_Constructor
},
62 {&dummy1
, &INewItem_Constructor
},
63 {&CLSID_StartMenu
, &StartMenu_Constructor
},
64 {&CLSID_MenuBandSite
, &MenuBandSite_Constructor
},
69 /* FIXME: this should be SHLWAPI.24 since we can't yet import by ordinal */
71 DWORD WINAPI
__SHGUIDToStringW (REFGUID guid
, LPWSTR str
)
73 WCHAR sFormat
[52] = {'{','%','0','8','l','x','-','%','0','4',
74 'x','-','%','0','4','x','-','%','0','2',
75 'x','%','0','2','x','-','%','0','2','x',
76 '%','0','2','x','%','0','2','x','%','0',
77 '2','x','%','0','2','x','%','0','2','x',
80 return swprintf ( str
, sFormat
,
81 guid
->Data1
, guid
->Data2
, guid
->Data3
,
82 guid
->Data4
[0], guid
->Data4
[1], guid
->Data4
[2], guid
->Data4
[3],
83 guid
->Data4
[4], guid
->Data4
[5], guid
->Data4
[6], guid
->Data4
[7] );
87 /*************************************************************************
88 * SHCoCreateInstance [SHELL32.102]
90 * Equivalent to CoCreateInstance. Under Windows 9x this function could sometimes
91 * use the shell32 built-in "mini-COM" without the need to load ole32.dll - see
92 * SHLoadOLE for details.
94 * Under wine if a "LoadWithoutCOM" value is present or the object resides in
95 * shell32.dll the function will load the object manually without the help of ole32
101 * CoCreateInstace, SHLoadOLE
103 HRESULT WINAPI
SHCoCreateInstance(
112 const CLSID
* myclsid
= clsid
;
113 WCHAR sKeyName
[MAX_PATH
];
114 const WCHAR sCLSID
[7] = {'C','L','S','I','D','\\','\0'};
116 const WCHAR sInProcServer32
[16] ={'\\','I','n','p','r','o','c','S','e','r','v','e','r','3','2','\0'};
117 const WCHAR sLoadWithoutCOM
[15] ={'L','o','a','d','W','i','t','h','o','u','t','C','O','M','\0'};
118 WCHAR sDllPath
[MAX_PATH
];
121 BOOLEAN bLoadFromShell32
= FALSE
;
122 BOOLEAN bLoadWithoutCOM
= FALSE
;
123 IClassFactory
* pcf
= NULL
;
125 if(!ppv
) return E_POINTER
;
128 /* if the clsid is a string, convert it */
131 if (!aclsid
) return REGDB_E_CLASSNOTREG
;
132 CLSIDFromString((LPOLESTR
)aclsid
, &iid
);
136 TRACE("(%p,%s,unk:%p,%s,%p)\n",
137 aclsid
,shdebugstr_guid(myclsid
),pUnkOuter
,shdebugstr_guid(refiid
),ppv
);
139 /* we look up the dll path in the registry */
140 __SHGUIDToStringW(myclsid
, sClassID
);
141 wcscpy(sKeyName
, sCLSID
);
142 wcscat(sKeyName
, sClassID
);
143 wcscat(sKeyName
, sInProcServer32
);
145 if (ERROR_SUCCESS
== RegOpenKeyExW(HKEY_CLASSES_ROOT
, sKeyName
, 0, KEY_READ
, &hKey
)) {
146 dwSize
= sizeof(sDllPath
);
147 SHQueryValueExW(hKey
, NULL
, 0,0, sDllPath
, &dwSize
);
149 /* if a special registry key is set, we load a shell extension without help of OLE32 */
150 bLoadWithoutCOM
= (ERROR_SUCCESS
== SHQueryValueExW(hKey
, sLoadWithoutCOM
, 0, 0, 0, 0));
152 /* if the com object is inside shell32, omit use of ole32 */
153 bLoadFromShell32
= (0==lstrcmpiW( PathFindFileNameW(sDllPath
), sShell32
));
157 /* since we can't find it in the registry we try internally */
158 bLoadFromShell32
= TRUE
;
161 TRACE("WithoutCom=%u FromShell=%u\n", bLoadWithoutCOM
, bLoadFromShell32
);
163 /* now we create an instance */
164 if (bLoadFromShell32
) {
165 if (! SUCCEEDED(DllGetClassObject(myclsid
, &IID_IClassFactory
,(LPVOID
*)&pcf
))) {
166 ERR("LoadFromShell failed for CLSID=%s\n", shdebugstr_guid(myclsid
));
168 } else if (bLoadWithoutCOM
) {
170 /* load an external dll without ole32 */
172 typedef HRESULT (CALLBACK
*DllGetClassObjectFunc
)(REFCLSID clsid
, REFIID iid
, LPVOID
*ppv
);
173 DllGetClassObjectFunc DllGetClassObject
;
175 if ((hLibrary
= LoadLibraryExW(sDllPath
, 0, LOAD_WITH_ALTERED_SEARCH_PATH
)) == 0) {
176 ERR("couldn't load InprocServer32 dll %s\n", debugstr_w(sDllPath
));
177 hres
= E_ACCESSDENIED
;
179 } else if (!(DllGetClassObject
= (DllGetClassObjectFunc
)GetProcAddress(hLibrary
, "DllGetClassObject"))) {
180 ERR("couldn't find function DllGetClassObject in %s\n", debugstr_w(sDllPath
));
181 FreeLibrary( hLibrary
);
182 hres
= E_ACCESSDENIED
;
184 } else if (FAILED(hres
= DllGetClassObject(myclsid
, &IID_IClassFactory
, (LPVOID
*)&pcf
))) {
185 TRACE("GetClassObject failed 0x%08x\n", hres
);
191 /* load an external dll in the usual way */
192 hres
= CoCreateInstance(myclsid
, pUnkOuter
, CLSCTX_INPROC_SERVER
, refiid
, ppv
);
196 /* here we should have a ClassFactory */
197 if (!pcf
) return E_ACCESSDENIED
;
199 hres
= IClassFactory_CreateInstance(pcf
, pUnkOuter
, refiid
, ppv
);
200 IClassFactory_Release(pcf
);
204 ERR("failed (0x%08x) to create CLSID:%s IID:%s\n",
205 hres
, shdebugstr_guid(myclsid
), shdebugstr_guid(refiid
));
206 ERR("class not found in registry\n");
209 TRACE("-- instance: %p\n",*ppv
);
213 /*************************************************************************
214 * DllGetClassObject [SHELL32.@]
215 * SHDllGetClassObject [SHELL32.128]
217 HRESULT WINAPI
DllGetClassObject(REFCLSID rclsid
, REFIID iid
, LPVOID
*ppv
)
219 HRESULT hres
= E_OUTOFMEMORY
;
220 IClassFactory
* pcf
= NULL
;
223 TRACE("CLSID:%s,IID:%s\n",shdebugstr_guid(rclsid
),shdebugstr_guid(iid
));
225 if (!ppv
) return E_INVALIDARG
;
228 /* search our internal interface table */
229 for(i
=0;InterfaceTable
[i
].riid
;i
++) {
230 if(IsEqualIID(InterfaceTable
[i
].riid
, rclsid
)) {
231 TRACE("index[%u]\n", i
);
232 pcf
= IDefClF_fnConstructor(InterfaceTable
[i
].lpfnCI
, NULL
, NULL
);
237 FIXME("failed for CLSID=%s\n", shdebugstr_guid(rclsid
));
238 return CLASS_E_CLASSNOTAVAILABLE
;
241 hres
= IClassFactory_QueryInterface(pcf
, iid
, ppv
);
242 IClassFactory_Release(pcf
);
244 TRACE("-- pointer to class factory: %p\n",*ppv
);
248 /*************************************************************************
249 * SHCLSIDFromString [SHELL32.147]
251 * Under Windows 9x this was an ANSI version of CLSIDFromString. It also allowed
252 * to avoid dependency on ole32.dll (see SHLoadOLE for details).
254 * Under Windows NT/2000/XP this is equivalent to CLSIDFromString
257 * exported by ordinal
260 * CLSIDFromString, SHLoadOLE
262 DWORD WINAPI
SHCLSIDFromStringA (LPCSTR clsid
, CLSID
*id
)
265 TRACE("(%p(%s) %p)\n", clsid
, clsid
, id
);
266 if (!MultiByteToWideChar( CP_ACP
, 0, clsid
, -1, buffer
, sizeof(buffer
)/sizeof(WCHAR
) ))
267 return CO_E_CLASSSTRING
;
268 return CLSIDFromString( buffer
, id
);
270 DWORD WINAPI
SHCLSIDFromStringW (LPCWSTR clsid
, CLSID
*id
)
272 TRACE("(%p(%s) %p)\n", clsid
, debugstr_w(clsid
), id
);
273 return CLSIDFromString((LPWSTR
)clsid
, id
);
275 DWORD WINAPI
SHCLSIDFromStringAW (LPCVOID clsid
, CLSID
*id
)
277 if (SHELL_OsIsUnicode())
278 return SHCLSIDFromStringW (clsid
, id
);
279 return SHCLSIDFromStringA (clsid
, id
);
282 /*************************************************************************
283 * SHGetMalloc [SHELL32.@]
285 * Equivalent to CoGetMalloc(MEMCTX_TASK, ...). Under Windows 9x this function
286 * could use the shell32 built-in "mini-COM" without the need to load ole32.dll -
287 * see SHLoadOLE for details.
290 * lpmal [O] Destination for IMalloc interface.
293 * Success: S_OK. lpmal contains the shells IMalloc interface.
294 * Failure. An HRESULT error code.
297 * CoGetMalloc, SHLoadOLE
299 HRESULT WINAPI
SHGetMalloc(LPMALLOC
*lpmal
)
301 TRACE("(%p)\n", lpmal
);
302 return CoGetMalloc(MEMCTX_TASK
, lpmal
);
305 /*************************************************************************
306 * SHAlloc [SHELL32.196]
308 * Equivalent to CoTaskMemAlloc. Under Windows 9x this function could use
309 * the shell32 built-in "mini-COM" without the need to load ole32.dll -
310 * see SHLoadOLE for details.
313 * exported by ordinal
316 * CoTaskMemAlloc, SHLoadOLE
318 LPVOID WINAPI
SHAlloc(DWORD len
)
322 ret
= CoTaskMemAlloc(len
);
323 TRACE("%u bytes at %p\n",len
, ret
);
327 /*************************************************************************
328 * SHFree [SHELL32.195]
330 * Equivalent to CoTaskMemFree. Under Windows 9x this function could use
331 * the shell32 built-in "mini-COM" without the need to load ole32.dll -
332 * see SHLoadOLE for details.
335 * exported by ordinal
338 * CoTaskMemFree, SHLoadOLE
340 void WINAPI
SHFree(LPVOID pv
)
346 /*************************************************************************
347 * SHGetDesktopFolder [SHELL32.@]
349 HRESULT WINAPI
SHGetDesktopFolder(IShellFolder
**psf
)
354 if(!psf
) return E_INVALIDARG
;
356 hres
= ISF_Desktop_Constructor(NULL
, &IID_IShellFolder
,(LPVOID
*)psf
);
358 TRACE("-- %p->(%p)\n",psf
, *psf
);
361 /**************************************************************************
362 * Default ClassFactory Implementation
364 * SHCreateDefClassObject
367 * Helper function for dlls without their own classfactory.
368 * A generic classfactory is returned.
369 * When the CreateInstance of the cf is called the callback is executed.
374 const IClassFactoryVtbl
*lpVtbl
;
377 LPFNCREATEINSTANCE lpfnCI
;
378 const IID
* riidInst
;
379 LONG
* pcRefDll
; /* pointer to refcounter in external dll (ugrrr...) */
382 static const IClassFactoryVtbl dclfvt
;
384 /**************************************************************************
385 * IDefClF_fnConstructor
388 static IClassFactory
* IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI
, PLONG pcRefDll
, REFIID riidInst
)
392 lpclf
= HeapAlloc(GetProcessHeap(),0,sizeof(IDefClFImpl
));
394 lpclf
->lpVtbl
= &dclfvt
;
395 lpclf
->lpfnCI
= lpfnCI
;
396 lpclf
->pcRefDll
= pcRefDll
;
398 if (pcRefDll
) InterlockedIncrement(pcRefDll
);
399 lpclf
->riidInst
= riidInst
;
401 TRACE("(%p)%s\n",lpclf
, shdebugstr_guid(riidInst
));
402 return (LPCLASSFACTORY
)lpclf
;
404 /**************************************************************************
405 * IDefClF_fnQueryInterface
407 static HRESULT WINAPI
IDefClF_fnQueryInterface(
408 LPCLASSFACTORY iface
, REFIID riid
, LPVOID
*ppvObj
)
410 IDefClFImpl
*This
= (IDefClFImpl
*)iface
;
412 TRACE("(%p)->(%s)\n",This
,shdebugstr_guid(riid
));
416 if(IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IClassFactory
)) {
418 InterlockedIncrement(&This
->ref
);
422 TRACE("-- E_NOINTERFACE\n");
423 return E_NOINTERFACE
;
425 /******************************************************************************
428 static ULONG WINAPI
IDefClF_fnAddRef(LPCLASSFACTORY iface
)
430 IDefClFImpl
*This
= (IDefClFImpl
*)iface
;
431 ULONG refCount
= InterlockedIncrement(&This
->ref
);
433 TRACE("(%p)->(count=%u)\n", This
, refCount
- 1);
437 /******************************************************************************
440 static ULONG WINAPI
IDefClF_fnRelease(LPCLASSFACTORY iface
)
442 IDefClFImpl
*This
= (IDefClFImpl
*)iface
;
443 ULONG refCount
= InterlockedDecrement(&This
->ref
);
445 TRACE("(%p)->(count=%u)\n", This
, refCount
+ 1);
449 if (This
->pcRefDll
) InterlockedDecrement(This
->pcRefDll
);
451 TRACE("-- destroying IClassFactory(%p)\n",This
);
452 HeapFree(GetProcessHeap(),0,This
);
457 /******************************************************************************
458 * IDefClF_fnCreateInstance
460 static HRESULT WINAPI
IDefClF_fnCreateInstance(
461 LPCLASSFACTORY iface
, LPUNKNOWN pUnkOuter
, REFIID riid
, LPVOID
*ppvObject
)
463 IDefClFImpl
*This
= (IDefClFImpl
*)iface
;
465 TRACE("%p->(%p,%s,%p)\n",This
,pUnkOuter
,shdebugstr_guid(riid
),ppvObject
);
469 if ( This
->riidInst
==NULL
||
470 IsEqualCLSID(riid
, This
->riidInst
) ||
471 IsEqualCLSID(riid
, &IID_IUnknown
) )
473 return This
->lpfnCI(pUnkOuter
, riid
, ppvObject
);
476 ERR("unknown IID requested %s\n",shdebugstr_guid(riid
));
477 return E_NOINTERFACE
;
479 /******************************************************************************
480 * IDefClF_fnLockServer
482 static HRESULT WINAPI
IDefClF_fnLockServer(LPCLASSFACTORY iface
, BOOL fLock
)
484 IDefClFImpl
*This
= (IDefClFImpl
*)iface
;
485 TRACE("%p->(0x%x), not implemented\n",This
, fLock
);
489 static const IClassFactoryVtbl dclfvt
=
491 IDefClF_fnQueryInterface
,
494 IDefClF_fnCreateInstance
,
498 /******************************************************************************
499 * SHCreateDefClassObject [SHELL32.70]
501 HRESULT WINAPI
SHCreateDefClassObject(
504 LPFNCREATEINSTANCE lpfnCI
, /* [in] create instance callback entry */
505 LPDWORD pcRefDll
, /* [in/out] ref count of the dll */
506 REFIID riidInst
) /* [in] optional interface to the instance */
510 TRACE("%s %p %p %p %s\n",
511 shdebugstr_guid(riid
), ppv
, lpfnCI
, pcRefDll
, shdebugstr_guid(riidInst
));
513 if (! IsEqualCLSID(riid
, &IID_IClassFactory
) ) return E_NOINTERFACE
;
514 if (! (pcf
= IDefClF_fnConstructor(lpfnCI
, (PLONG
)pcRefDll
, riidInst
))) return E_OUTOFMEMORY
;
519 /*************************************************************************
520 * DragAcceptFiles [SHELL32.@]
522 void WINAPI
DragAcceptFiles(HWND hWnd
, BOOL b
)
526 if( !IsWindow(hWnd
) ) return;
527 exstyle
= GetWindowLongPtrA(hWnd
,GWL_EXSTYLE
);
529 exstyle
|= WS_EX_ACCEPTFILES
;
531 exstyle
&= ~WS_EX_ACCEPTFILES
;
532 SetWindowLongPtrA(hWnd
,GWL_EXSTYLE
,exstyle
);
535 /*************************************************************************
536 * DragFinish [SHELL32.@]
538 void WINAPI
DragFinish(HDROP h
)
544 /*************************************************************************
545 * DragQueryPoint [SHELL32.@]
547 BOOL WINAPI
DragQueryPoint(HDROP hDrop
, POINT
*p
)
549 DROPFILES
*lpDropFileStruct
;
554 lpDropFileStruct
= GlobalLock(hDrop
);
556 *p
= lpDropFileStruct
->pt
;
557 bRet
= lpDropFileStruct
->fNC
;
563 /*************************************************************************
564 * DragQueryFileA [SHELL32.@]
565 * DragQueryFile [SHELL32.@]
567 UINT WINAPI
DragQueryFileA(
575 DROPFILES
*lpDropFileStruct
= GlobalLock(hDrop
);
577 TRACE("(%p, %x, %p, %u)\n", hDrop
,lFile
,lpszFile
,lLength
);
579 if(!lpDropFileStruct
) goto end
;
581 lpDrop
= (LPSTR
) lpDropFileStruct
+ lpDropFileStruct
->pFiles
;
583 if(lpDropFileStruct
->fWide
) {
584 LPWSTR lpszFileW
= NULL
;
587 lpszFileW
= HeapAlloc(GetProcessHeap(), 0, lLength
*sizeof(WCHAR
));
588 if(lpszFileW
== NULL
) {
592 i
= DragQueryFileW(hDrop
, lFile
, lpszFileW
, lLength
);
595 WideCharToMultiByte(CP_ACP
, 0, lpszFileW
, -1, lpszFile
, lLength
, 0, NULL
);
596 HeapFree(GetProcessHeap(), 0, lpszFileW
);
603 while (*lpDrop
++); /* skip filename */
606 i
= (lFile
== 0xFFFFFFFF) ? i
: 0;
612 if (!lpszFile
) goto end
; /* needed buffer size */
613 lstrcpynA (lpszFile
, lpDrop
, lLength
);
619 /*************************************************************************
620 * DragQueryFileW [SHELL32.@]
622 UINT WINAPI
DragQueryFileW(
630 DROPFILES
*lpDropFileStruct
= GlobalLock(hDrop
);
632 TRACE("(%p, %x, %p, %u)\n", hDrop
,lFile
,lpszwFile
,lLength
);
634 if(!lpDropFileStruct
) goto end
;
636 lpwDrop
= (LPWSTR
) ((LPSTR
)lpDropFileStruct
+ lpDropFileStruct
->pFiles
);
638 if(lpDropFileStruct
->fWide
== FALSE
) {
639 LPSTR lpszFileA
= NULL
;
642 lpszFileA
= HeapAlloc(GetProcessHeap(), 0, lLength
);
643 if(lpszFileA
== NULL
) {
647 i
= DragQueryFileA(hDrop
, lFile
, lpszFileA
, lLength
);
650 MultiByteToWideChar(CP_ACP
, 0, lpszFileA
, -1, lpszwFile
, lLength
);
651 HeapFree(GetProcessHeap(), 0, lpszFileA
);
659 while (*lpwDrop
++); /* skip filename */
662 i
= (lFile
== 0xFFFFFFFF) ? i
: 0;
668 if ( !lpszwFile
) goto end
; /* needed buffer size */
669 lstrcpynW (lpszwFile
, lpwDrop
, lLength
);