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 HRESULT WINAPI
IFSFolder_Constructor(IUnknown
* pUnkOuter
, REFIID riid
, LPVOID
* ppv
);
52 static const WCHAR sShell32
[12] = {'S','H','E','L','L','3','2','.','D','L','L','\0'};
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
[] = {
65 {&CLSID_ShellFSFolder
, &IFSFolder_Constructor
},
66 {&CLSID_MyComputer
, &ISF_MyComputer_Constructor
},
67 {&CLSID_NetworkPlaces
, &ISF_NetworkPlaces_Constructor
},
68 {&CLSID_ShellDesktop
, &ISF_Desktop_Constructor
},
69 {&CLSID_ShellLink
, &IShellLink_Constructor
},
70 {&CLSID_DragDropHelper
, &IDropTargetHelper_Constructor
},
71 {&CLSID_ControlPanel
, &IControlPanel_Constructor
},
72 {&CLSID_AutoComplete
, &IAutoComplete_Constructor
},
73 {&CLSID_UnixFolder
, &UnixFolder_Constructor
},
74 {&CLSID_UnixDosFolder
, &UnixDosFolder_Constructor
},
75 {&CLSID_FolderShortcut
, &FolderShortcut_Constructor
},
76 {&CLSID_MyDocuments
, &MyDocuments_Constructor
},
77 {&CLSID_RecycleBin
, &RecycleBin_Constructor
},
82 /* FIXME: this should be SHLWAPI.24 since we can't yet import by ordinal */
84 DWORD WINAPI
__SHGUIDToStringW (REFGUID guid
, LPWSTR str
)
86 WCHAR sFormat
[52] = {'{','%','0','8','l','x','-','%','0','4',
87 'x','-','%','0','4','x','-','%','0','2',
88 'x','%','0','2','x','-','%','0','2','x',
89 '%','0','2','x','%','0','2','x','%','0',
90 '2','x','%','0','2','x','%','0','2','x',
93 return wsprintfW ( str
, sFormat
,
94 guid
->Data1
, guid
->Data2
, guid
->Data3
,
95 guid
->Data4
[0], guid
->Data4
[1], guid
->Data4
[2], guid
->Data4
[3],
96 guid
->Data4
[4], guid
->Data4
[5], guid
->Data4
[6], guid
->Data4
[7] );
100 /*************************************************************************
101 * SHCoCreateInstance [SHELL32.102]
103 * Equivalent to CoCreateInstance. Under Windows 9x this function could sometimes
104 * use the shell32 built-in "mini-COM" without the need to load ole32.dll - see
105 * SHLoadOLE for details.
107 * Under wine if a "LoadWithoutCOM" value is present or the object resides in
108 * shell32.dll the function will load the object manually without the help of ole32
111 * exported by ordinal
114 * CoCreateInstace, SHLoadOLE
116 HRESULT WINAPI
SHCoCreateInstance(
125 const CLSID
* myclsid
= clsid
;
126 WCHAR sKeyName
[MAX_PATH
];
127 const WCHAR sCLSID
[7] = {'C','L','S','I','D','\\','\0'};
129 const WCHAR sInProcServer32
[16] ={'\\','I','n','p','r','o','c','S','e','r','v','e','r','3','2','\0'};
130 const WCHAR sLoadWithoutCOM
[15] ={'L','o','a','d','W','i','t','h','o','u','t','C','O','M','\0'};
131 WCHAR sDllPath
[MAX_PATH
];
134 IClassFactory
* pcf
= NULL
;
136 if(!ppv
) return E_POINTER
;
139 /* if the clsid is a string, convert it */
142 if (!aclsid
) return REGDB_E_CLASSNOTREG
;
143 SHCLSIDFromStringW(aclsid
, &iid
);
147 TRACE("(%p,%s,unk:%p,%s,%p)\n",
148 aclsid
,shdebugstr_guid(myclsid
),pUnkOuter
,shdebugstr_guid(refiid
),ppv
);
150 if (SUCCEEDED(DllGetClassObject(myclsid
, &IID_IClassFactory
,(LPVOID
*)&pcf
)))
152 hres
= IClassFactory_CreateInstance(pcf
, pUnkOuter
, refiid
, ppv
);
153 IClassFactory_Release(pcf
);
157 /* we look up the dll path in the registry */
158 __SHGUIDToStringW(myclsid
, sClassID
);
159 lstrcpyW(sKeyName
, sCLSID
);
160 lstrcatW(sKeyName
, sClassID
);
161 lstrcatW(sKeyName
, sInProcServer32
);
163 if (RegOpenKeyExW(HKEY_CLASSES_ROOT
, sKeyName
, 0, KEY_READ
, &hKey
))
164 return E_ACCESSDENIED
;
166 /* if a special registry key is set, we load a shell extension without help of OLE32 */
167 if (!SHQueryValueExW(hKey
, sLoadWithoutCOM
, 0, 0, 0, 0))
169 /* load an external dll without ole32 */
171 typedef HRESULT (CALLBACK
*DllGetClassObjectFunc
)(REFCLSID clsid
, REFIID iid
, LPVOID
*ppv
);
172 DllGetClassObjectFunc DllGetClassObject
;
174 dwSize
= sizeof(sDllPath
);
175 SHQueryValueExW(hKey
, NULL
, 0,0, sDllPath
, &dwSize
);
177 if ((hLibrary
= LoadLibraryExW(sDllPath
, 0, LOAD_WITH_ALTERED_SEARCH_PATH
)) == 0) {
178 ERR("couldn't load InprocServer32 dll %s\n", debugstr_w(sDllPath
));
179 hres
= E_ACCESSDENIED
;
181 } else if (!(DllGetClassObject
= (DllGetClassObjectFunc
)GetProcAddress(hLibrary
, "DllGetClassObject"))) {
182 ERR("couldn't find function DllGetClassObject in %s\n", debugstr_w(sDllPath
));
183 FreeLibrary( hLibrary
);
184 hres
= E_ACCESSDENIED
;
186 } else if (! SUCCEEDED(hres
= DllGetClassObject(myclsid
, &IID_IClassFactory
, (LPVOID
*)&pcf
))) {
187 TRACE("GetClassObject failed 0x%08x\n", hres
);
191 hres
= IClassFactory_CreateInstance(pcf
, pUnkOuter
, refiid
, ppv
);
192 IClassFactory_Release(pcf
);
195 /* load an external dll in the usual way */
196 hres
= CoCreateInstance(myclsid
, pUnkOuter
, CLSCTX_INPROC_SERVER
, refiid
, ppv
);
200 if (hKey
) RegCloseKey(hKey
);
203 ERR("failed (0x%08x) to create CLSID:%s IID:%s\n",
204 hres
, shdebugstr_guid(myclsid
), shdebugstr_guid(refiid
));
205 ERR("class not found in registry\n");
208 TRACE("-- instance: %p\n",*ppv
);
212 /*************************************************************************
213 * DllGetClassObject [SHELL32.@]
214 * SHDllGetClassObject [SHELL32.128]
216 HRESULT WINAPI
DllGetClassObject(REFCLSID rclsid
, REFIID iid
, LPVOID
*ppv
)
218 HRESULT hres
= E_OUTOFMEMORY
;
219 IClassFactory
* pcf
= NULL
;
222 TRACE("CLSID:%s,IID:%s\n",shdebugstr_guid(rclsid
),shdebugstr_guid(iid
));
224 if (!ppv
) return E_INVALIDARG
;
227 /* search our internal interface table */
228 for(i
=0;InterfaceTable
[i
].riid
;i
++) {
229 if(IsEqualIID(InterfaceTable
[i
].riid
, rclsid
)) {
230 TRACE("index[%u]\n", i
);
231 pcf
= IDefClF_fnConstructor(InterfaceTable
[i
].lpfnCI
, NULL
, NULL
);
236 FIXME("failed for CLSID=%s\n", shdebugstr_guid(rclsid
));
237 return CLASS_E_CLASSNOTAVAILABLE
;
240 hres
= IClassFactory_QueryInterface(pcf
, iid
, ppv
);
241 IClassFactory_Release(pcf
);
243 TRACE("-- pointer to class factory: %p\n",*ppv
);
247 /*************************************************************************
248 * SHCLSIDFromString [SHELL32.147]
250 * Under Windows 9x this was an ANSI version of CLSIDFromString. It also allowed
251 * to avoid dependency on ole32.dll (see SHLoadOLE for details).
253 * Under Windows NT/2000/XP this is equivalent to CLSIDFromString
256 * exported by ordinal
259 * CLSIDFromString, SHLoadOLE
261 DWORD WINAPI
SHCLSIDFromStringA (LPCSTR clsid
, CLSID
*id
)
264 TRACE("(%p(%s) %p)\n", clsid
, clsid
, id
);
265 if (!MultiByteToWideChar( CP_ACP
, 0, clsid
, -1, buffer
, sizeof(buffer
)/sizeof(WCHAR
) ))
266 return CO_E_CLASSSTRING
;
267 return CLSIDFromString( buffer
, id
);
269 DWORD WINAPI
SHCLSIDFromStringW (LPCWSTR clsid
, CLSID
*id
)
271 TRACE("(%p(%s) %p)\n", clsid
, debugstr_w(clsid
), id
);
272 return CLSIDFromString((LPWSTR
)clsid
, id
);
274 DWORD WINAPI
SHCLSIDFromStringAW (LPCVOID clsid
, CLSID
*id
)
276 if (SHELL_OsIsUnicode())
277 return SHCLSIDFromStringW (clsid
, id
);
278 return SHCLSIDFromStringA (clsid
, id
);
281 /*************************************************************************
282 * SHGetMalloc [SHELL32.@]
284 * Equivalent to CoGetMalloc(MEMCTX_TASK, ...). Under Windows 9x this function
285 * could use the shell32 built-in "mini-COM" without the need to load ole32.dll -
286 * see SHLoadOLE for details.
289 * lpmal [O] Destination for IMalloc interface.
292 * Success: S_OK. lpmal contains the shells IMalloc interface.
293 * Failure. An HRESULT error code.
296 * CoGetMalloc, SHLoadOLE
298 HRESULT WINAPI
SHGetMalloc(LPMALLOC
*lpmal
)
300 TRACE("(%p)\n", lpmal
);
301 return CoGetMalloc(MEMCTX_TASK
, lpmal
);
304 /*************************************************************************
305 * SHAlloc [SHELL32.196]
307 * Equivalent to CoTaskMemAlloc. Under Windows 9x this function could use
308 * the shell32 built-in "mini-COM" without the need to load ole32.dll -
309 * see SHLoadOLE for details.
312 * exported by ordinal
315 * CoTaskMemAlloc, SHLoadOLE
317 LPVOID WINAPI
SHAlloc(DWORD len
)
321 ret
= CoTaskMemAlloc(len
);
322 TRACE("%u bytes at %p\n",len
, ret
);
326 /*************************************************************************
327 * SHFree [SHELL32.195]
329 * Equivalent to CoTaskMemFree. Under Windows 9x this function could use
330 * the shell32 built-in "mini-COM" without the need to load ole32.dll -
331 * see SHLoadOLE for details.
334 * exported by ordinal
337 * CoTaskMemFree, SHLoadOLE
339 void WINAPI
SHFree(LPVOID pv
)
345 /*************************************************************************
346 * SHGetDesktopFolder [SHELL32.@]
348 HRESULT WINAPI
SHGetDesktopFolder(IShellFolder
**psf
)
353 if(!psf
) return E_INVALIDARG
;
355 hres
= ISF_Desktop_Constructor(NULL
, &IID_IShellFolder
,(LPVOID
*)psf
);
357 TRACE("-- %p->(%p)\n",psf
, *psf
);
360 /**************************************************************************
361 * Default ClassFactory Implementation
363 * SHCreateDefClassObject
366 * Helper function for dlls without their own classfactory.
367 * A generic classfactory is returned.
368 * When the CreateInstance of the cf is called the callback is executed.
373 const IClassFactoryVtbl
*lpVtbl
;
376 LPFNCREATEINSTANCE lpfnCI
;
377 const IID
* riidInst
;
378 LONG
* pcRefDll
; /* pointer to refcounter in external dll (ugrrr...) */
381 static const IClassFactoryVtbl dclfvt
;
383 /**************************************************************************
384 * IDefClF_fnConstructor
387 static IClassFactory
* IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI
, PLONG pcRefDll
, REFIID riidInst
)
391 lpclf
= HeapAlloc(GetProcessHeap(),0,sizeof(IDefClFImpl
));
393 lpclf
->lpVtbl
= &dclfvt
;
394 lpclf
->lpfnCI
= lpfnCI
;
395 lpclf
->pcRefDll
= pcRefDll
;
397 if (pcRefDll
) InterlockedIncrement(pcRefDll
);
398 lpclf
->riidInst
= riidInst
;
400 TRACE("(%p)%s\n",lpclf
, shdebugstr_guid(riidInst
));
401 return (LPCLASSFACTORY
)lpclf
;
403 /**************************************************************************
404 * IDefClF_fnQueryInterface
406 static HRESULT WINAPI
IDefClF_fnQueryInterface(
407 LPCLASSFACTORY iface
, REFIID riid
, LPVOID
*ppvObj
)
409 IDefClFImpl
*This
= (IDefClFImpl
*)iface
;
411 TRACE("(%p)->(%s)\n",This
,shdebugstr_guid(riid
));
415 if(IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IClassFactory
)) {
417 InterlockedIncrement(&This
->ref
);
421 TRACE("-- E_NOINTERFACE\n");
422 return E_NOINTERFACE
;
424 /******************************************************************************
427 static ULONG WINAPI
IDefClF_fnAddRef(LPCLASSFACTORY iface
)
429 IDefClFImpl
*This
= (IDefClFImpl
*)iface
;
430 ULONG refCount
= InterlockedIncrement(&This
->ref
);
432 TRACE("(%p)->(count=%u)\n", This
, refCount
- 1);
436 /******************************************************************************
439 static ULONG WINAPI
IDefClF_fnRelease(LPCLASSFACTORY iface
)
441 IDefClFImpl
*This
= (IDefClFImpl
*)iface
;
442 ULONG refCount
= InterlockedDecrement(&This
->ref
);
444 TRACE("(%p)->(count=%u)\n", This
, refCount
+ 1);
448 if (This
->pcRefDll
) InterlockedDecrement(This
->pcRefDll
);
450 TRACE("-- destroying IClassFactory(%p)\n",This
);
451 HeapFree(GetProcessHeap(),0,This
);
456 /******************************************************************************
457 * IDefClF_fnCreateInstance
459 static HRESULT WINAPI
IDefClF_fnCreateInstance(
460 LPCLASSFACTORY iface
, LPUNKNOWN pUnkOuter
, REFIID riid
, LPVOID
*ppvObject
)
462 IDefClFImpl
*This
= (IDefClFImpl
*)iface
;
464 TRACE("%p->(%p,%s,%p)\n",This
,pUnkOuter
,shdebugstr_guid(riid
),ppvObject
);
468 if ( This
->riidInst
==NULL
||
469 IsEqualCLSID(riid
, This
->riidInst
) ||
470 IsEqualCLSID(riid
, &IID_IUnknown
) )
472 return This
->lpfnCI(pUnkOuter
, riid
, ppvObject
);
475 ERR("unknown IID requested %s\n",shdebugstr_guid(riid
));
476 return E_NOINTERFACE
;
478 /******************************************************************************
479 * IDefClF_fnLockServer
481 static HRESULT WINAPI
IDefClF_fnLockServer(LPCLASSFACTORY iface
, BOOL fLock
)
483 IDefClFImpl
*This
= (IDefClFImpl
*)iface
;
484 TRACE("%p->(0x%x), not implemented\n",This
, fLock
);
488 static const IClassFactoryVtbl dclfvt
=
490 IDefClF_fnQueryInterface
,
493 IDefClF_fnCreateInstance
,
497 /******************************************************************************
498 * SHCreateDefClassObject [SHELL32.70]
500 HRESULT WINAPI
SHCreateDefClassObject(
503 LPFNCREATEINSTANCE lpfnCI
, /* [in] create instance callback entry */
504 LPDWORD pcRefDll
, /* [in/out] ref count of the dll */
505 REFIID riidInst
) /* [in] optional interface to the instance */
509 TRACE("%s %p %p %p %s\n",
510 shdebugstr_guid(riid
), ppv
, lpfnCI
, pcRefDll
, shdebugstr_guid(riidInst
));
512 if (! IsEqualCLSID(riid
, &IID_IClassFactory
) ) return E_NOINTERFACE
;
513 if (! (pcf
= IDefClF_fnConstructor(lpfnCI
, (PLONG
)pcRefDll
, riidInst
))) return E_OUTOFMEMORY
;
518 /*************************************************************************
519 * DragAcceptFiles [SHELL32.@]
521 void WINAPI
DragAcceptFiles(HWND hWnd
, BOOL b
)
525 if( !IsWindow(hWnd
) ) return;
526 exstyle
= GetWindowLongA(hWnd
,GWL_EXSTYLE
);
528 exstyle
|= WS_EX_ACCEPTFILES
;
530 exstyle
&= ~WS_EX_ACCEPTFILES
;
531 SetWindowLongA(hWnd
,GWL_EXSTYLE
,exstyle
);
534 /*************************************************************************
535 * DragFinish [SHELL32.@]
537 void WINAPI
DragFinish(HDROP h
)
540 GlobalFree((HGLOBAL
)h
);
543 /*************************************************************************
544 * DragQueryPoint [SHELL32.@]
546 BOOL WINAPI
DragQueryPoint(HDROP hDrop
, POINT
*p
)
548 DROPFILES
*lpDropFileStruct
;
553 lpDropFileStruct
= (DROPFILES
*) GlobalLock(hDrop
);
555 *p
= lpDropFileStruct
->pt
;
556 bRet
= lpDropFileStruct
->fNC
;
562 /*************************************************************************
563 * DragQueryFileA [SHELL32.@]
564 * DragQueryFile [SHELL32.@]
566 UINT WINAPI
DragQueryFileA(
574 DROPFILES
*lpDropFileStruct
= (DROPFILES
*) GlobalLock(hDrop
);
576 TRACE("(%p, %x, %p, %u)\n", hDrop
,lFile
,lpszFile
,lLength
);
578 if(!lpDropFileStruct
) goto end
;
580 lpDrop
= (LPSTR
) lpDropFileStruct
+ lpDropFileStruct
->pFiles
;
582 if(lpDropFileStruct
->fWide
) {
583 LPWSTR lpszFileW
= NULL
;
586 lpszFileW
= HeapAlloc(GetProcessHeap(), 0, lLength
*sizeof(WCHAR
));
587 if(lpszFileW
== NULL
) {
591 i
= DragQueryFileW(hDrop
, lFile
, lpszFileW
, lLength
);
594 WideCharToMultiByte(CP_ACP
, 0, lpszFileW
, -1, lpszFile
, lLength
, 0, NULL
);
595 HeapFree(GetProcessHeap(), 0, lpszFileW
);
602 while (*lpDrop
++); /* skip filename */
605 i
= (lFile
== 0xFFFFFFFF) ? i
: 0;
611 if (!lpszFile
) goto end
; /* needed buffer size */
612 lstrcpynA (lpszFile
, lpDrop
, lLength
);
618 /*************************************************************************
619 * DragQueryFileW [SHELL32.@]
621 UINT WINAPI
DragQueryFileW(
629 DROPFILES
*lpDropFileStruct
= (DROPFILES
*) GlobalLock(hDrop
);
631 TRACE("(%p, %x, %p, %u)\n", hDrop
,lFile
,lpszwFile
,lLength
);
633 if(!lpDropFileStruct
) goto end
;
635 lpwDrop
= (LPWSTR
) ((LPSTR
)lpDropFileStruct
+ lpDropFileStruct
->pFiles
);
637 if(lpDropFileStruct
->fWide
== FALSE
) {
638 LPSTR lpszFileA
= NULL
;
641 lpszFileA
= HeapAlloc(GetProcessHeap(), 0, lLength
);
642 if(lpszFileA
== NULL
) {
646 i
= DragQueryFileA(hDrop
, lFile
, lpszFileA
, lLength
);
649 MultiByteToWideChar(CP_ACP
, 0, lpszFileA
, -1, lpszwFile
, lLength
);
650 HeapFree(GetProcessHeap(), 0, lpszFileA
);
658 while (*lpwDrop
++); /* skip filename */
661 i
= (lFile
== 0xFFFFFFFF) ? i
: 0;
666 i
= strlenW(lpwDrop
);
667 if ( !lpszwFile
) goto end
; /* needed buffer size */
668 lstrcpynW (lpszwFile
, lpwDrop
, lLength
);