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 BOOLEAN bLoadFromShell32
= FALSE
;
135 BOOLEAN bLoadWithoutCOM
= FALSE
;
136 IClassFactory
* pcf
= NULL
;
138 if(!ppv
) return E_POINTER
;
141 /* if the clsid is a string, convert it */
144 if (!aclsid
) return REGDB_E_CLASSNOTREG
;
145 SHCLSIDFromStringW(aclsid
, &iid
);
149 TRACE("(%p,%s,unk:%p,%s,%p)\n",
150 aclsid
,shdebugstr_guid(myclsid
),pUnkOuter
,shdebugstr_guid(refiid
),ppv
);
152 /* we look up the dll path in the registry */
153 __SHGUIDToStringW(myclsid
, sClassID
);
154 lstrcpyW(sKeyName
, sCLSID
);
155 lstrcatW(sKeyName
, sClassID
);
156 lstrcatW(sKeyName
, sInProcServer32
);
158 if (ERROR_SUCCESS
== RegOpenKeyExW(HKEY_CLASSES_ROOT
, sKeyName
, 0, KEY_READ
, &hKey
)) {
159 dwSize
= sizeof(sDllPath
);
160 SHQueryValueExW(hKey
, NULL
, 0,0, sDllPath
, &dwSize
);
162 /* if a special registry key is set, we load a shell extension without help of OLE32 */
163 bLoadWithoutCOM
= (ERROR_SUCCESS
== SHQueryValueExW(hKey
, sLoadWithoutCOM
, 0, 0, 0, 0));
165 /* if the com object is inside shell32, omit use of ole32 */
166 bLoadFromShell32
= (0==lstrcmpiW( PathFindFileNameW(sDllPath
), sShell32
));
170 /* since we can't find it in the registry we try internally */
171 bLoadFromShell32
= TRUE
;
174 TRACE("WithoutCom=%u FromShell=%u\n", bLoadWithoutCOM
, bLoadFromShell32
);
176 /* now we create an instance */
177 if (bLoadFromShell32
) {
178 if (! SUCCEEDED(DllGetClassObject(myclsid
, &IID_IClassFactory
,(LPVOID
*)&pcf
))) {
179 ERR("LoadFromShell failed for CLSID=%s\n", shdebugstr_guid(myclsid
));
181 } else if (bLoadWithoutCOM
) {
183 /* load an external dll without ole32 */
185 typedef HRESULT (CALLBACK
*DllGetClassObjectFunc
)(REFCLSID clsid
, REFIID iid
, LPVOID
*ppv
);
186 DllGetClassObjectFunc DllGetClassObject
;
188 if ((hLibrary
= LoadLibraryExW(sDllPath
, 0, LOAD_WITH_ALTERED_SEARCH_PATH
)) == 0) {
189 ERR("couldn't load InprocServer32 dll %s\n", debugstr_w(sDllPath
));
190 hres
= E_ACCESSDENIED
;
192 } else if (!(DllGetClassObject
= (DllGetClassObjectFunc
)GetProcAddress(hLibrary
, "DllGetClassObject"))) {
193 ERR("couldn't find function DllGetClassObject in %s\n", debugstr_w(sDllPath
));
194 FreeLibrary( hLibrary
);
195 hres
= E_ACCESSDENIED
;
197 } else if (! SUCCEEDED(hres
= DllGetClassObject(myclsid
, &IID_IClassFactory
, (LPVOID
*)&pcf
))) {
198 TRACE("GetClassObject failed 0x%08x\n", hres
);
204 /* load an external dll in the usual way */
205 hres
= CoCreateInstance(myclsid
, pUnkOuter
, CLSCTX_INPROC_SERVER
, refiid
, ppv
);
209 /* here we should have a ClassFactory */
210 if (!pcf
) return E_ACCESSDENIED
;
212 hres
= IClassFactory_CreateInstance(pcf
, pUnkOuter
, refiid
, ppv
);
213 IClassFactory_Release(pcf
);
217 ERR("failed (0x%08x) to create CLSID:%s IID:%s\n",
218 hres
, shdebugstr_guid(myclsid
), shdebugstr_guid(refiid
));
219 ERR("class not found in registry\n");
222 TRACE("-- instance: %p\n",*ppv
);
226 /*************************************************************************
227 * DllGetClassObject [SHELL32.@]
228 * SHDllGetClassObject [SHELL32.128]
230 HRESULT WINAPI
DllGetClassObject(REFCLSID rclsid
, REFIID iid
, LPVOID
*ppv
)
232 HRESULT hres
= E_OUTOFMEMORY
;
233 IClassFactory
* pcf
= NULL
;
236 TRACE("CLSID:%s,IID:%s\n",shdebugstr_guid(rclsid
),shdebugstr_guid(iid
));
238 if (!ppv
) return E_INVALIDARG
;
241 /* search our internal interface table */
242 for(i
=0;InterfaceTable
[i
].riid
;i
++) {
243 if(IsEqualIID(InterfaceTable
[i
].riid
, rclsid
)) {
244 TRACE("index[%u]\n", i
);
245 pcf
= IDefClF_fnConstructor(InterfaceTable
[i
].lpfnCI
, NULL
, NULL
);
250 FIXME("failed for CLSID=%s\n", shdebugstr_guid(rclsid
));
251 return CLASS_E_CLASSNOTAVAILABLE
;
254 hres
= IClassFactory_QueryInterface(pcf
, iid
, ppv
);
255 IClassFactory_Release(pcf
);
257 TRACE("-- pointer to class factory: %p\n",*ppv
);
261 /*************************************************************************
262 * SHCLSIDFromString [SHELL32.147]
264 * Under Windows 9x this was an ANSI version of CLSIDFromString. It also allowed
265 * to avoid dependency on ole32.dll (see SHLoadOLE for details).
267 * Under Windows NT/2000/XP this is equivalent to CLSIDFromString
270 * exported by ordinal
273 * CLSIDFromString, SHLoadOLE
275 DWORD WINAPI
SHCLSIDFromStringA (LPCSTR clsid
, CLSID
*id
)
278 TRACE("(%p(%s) %p)\n", clsid
, clsid
, id
);
279 if (!MultiByteToWideChar( CP_ACP
, 0, clsid
, -1, buffer
, sizeof(buffer
)/sizeof(WCHAR
) ))
280 return CO_E_CLASSSTRING
;
281 return CLSIDFromString( buffer
, id
);
283 DWORD WINAPI
SHCLSIDFromStringW (LPCWSTR clsid
, CLSID
*id
)
285 TRACE("(%p(%s) %p)\n", clsid
, debugstr_w(clsid
), id
);
286 return CLSIDFromString((LPWSTR
)clsid
, id
);
288 DWORD WINAPI
SHCLSIDFromStringAW (LPVOID clsid
, CLSID
*id
)
290 if (SHELL_OsIsUnicode())
291 return SHCLSIDFromStringW (clsid
, id
);
292 return SHCLSIDFromStringA (clsid
, id
);
295 /*************************************************************************
296 * SHGetMalloc [SHELL32.@]
298 * Equivalent to CoGetMalloc(MEMCTX_TASK, ...). Under Windows 9x this function
299 * could use the shell32 built-in "mini-COM" without the need to load ole32.dll -
300 * see SHLoadOLE for details.
303 * lpmal [O] Destination for IMalloc interface.
306 * Success: S_OK. lpmal contains the shells IMalloc interface.
307 * Failure. An HRESULT error code.
310 * CoGetMalloc, SHLoadOLE
312 HRESULT WINAPI
SHGetMalloc(LPMALLOC
*lpmal
)
314 TRACE("(%p)\n", lpmal
);
315 return CoGetMalloc(MEMCTX_TASK
, lpmal
);
318 /*************************************************************************
319 * SHAlloc [SHELL32.196]
321 * Equivalent to CoTaskMemAlloc. Under Windows 9x this function could use
322 * the shell32 built-in "mini-COM" without the need to load ole32.dll -
323 * see SHLoadOLE for details.
326 * exported by ordinal
329 * CoTaskMemAlloc, SHLoadOLE
331 LPVOID WINAPI
SHAlloc(DWORD len
)
335 ret
= CoTaskMemAlloc(len
);
336 TRACE("%u bytes at %p\n",len
, ret
);
340 /*************************************************************************
341 * SHFree [SHELL32.195]
343 * Equivalent to CoTaskMemFree. Under Windows 9x this function could use
344 * the shell32 built-in "mini-COM" without the need to load ole32.dll -
345 * see SHLoadOLE for details.
348 * exported by ordinal
351 * CoTaskMemFree, SHLoadOLE
353 void WINAPI
SHFree(LPVOID pv
)
359 /*************************************************************************
360 * SHGetDesktopFolder [SHELL32.@]
362 HRESULT WINAPI
SHGetDesktopFolder(IShellFolder
**psf
)
367 if(!psf
) return E_INVALIDARG
;
369 hres
= ISF_Desktop_Constructor(NULL
, &IID_IShellFolder
,(LPVOID
*)psf
);
371 TRACE("-- %p->(%p)\n",psf
, *psf
);
374 /**************************************************************************
375 * Default ClassFactory Implementation
377 * SHCreateDefClassObject
380 * Helper function for dlls without their own classfactory.
381 * A generic classfactory is returned.
382 * When the CreateInstance of the cf is called the callback is executed.
387 const IClassFactoryVtbl
*lpVtbl
;
390 LPFNCREATEINSTANCE lpfnCI
;
391 const IID
* riidInst
;
392 LONG
* pcRefDll
; /* pointer to refcounter in external dll (ugrrr...) */
395 static const IClassFactoryVtbl dclfvt
;
397 /**************************************************************************
398 * IDefClF_fnConstructor
401 static IClassFactory
* IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI
, PLONG pcRefDll
, REFIID riidInst
)
405 lpclf
= HeapAlloc(GetProcessHeap(),0,sizeof(IDefClFImpl
));
407 lpclf
->lpVtbl
= &dclfvt
;
408 lpclf
->lpfnCI
= lpfnCI
;
409 lpclf
->pcRefDll
= pcRefDll
;
411 if (pcRefDll
) InterlockedIncrement(pcRefDll
);
412 lpclf
->riidInst
= riidInst
;
414 TRACE("(%p)%s\n",lpclf
, shdebugstr_guid(riidInst
));
415 return (LPCLASSFACTORY
)lpclf
;
417 /**************************************************************************
418 * IDefClF_fnQueryInterface
420 static HRESULT WINAPI
IDefClF_fnQueryInterface(
421 LPCLASSFACTORY iface
, REFIID riid
, LPVOID
*ppvObj
)
423 IDefClFImpl
*This
= (IDefClFImpl
*)iface
;
425 TRACE("(%p)->(%s)\n",This
,shdebugstr_guid(riid
));
429 if(IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IClassFactory
)) {
431 InterlockedIncrement(&This
->ref
);
435 TRACE("-- E_NOINTERFACE\n");
436 return E_NOINTERFACE
;
438 /******************************************************************************
441 static ULONG WINAPI
IDefClF_fnAddRef(LPCLASSFACTORY iface
)
443 IDefClFImpl
*This
= (IDefClFImpl
*)iface
;
444 ULONG refCount
= InterlockedIncrement(&This
->ref
);
446 TRACE("(%p)->(count=%u)\n", This
, refCount
- 1);
450 /******************************************************************************
453 static ULONG WINAPI
IDefClF_fnRelease(LPCLASSFACTORY iface
)
455 IDefClFImpl
*This
= (IDefClFImpl
*)iface
;
456 ULONG refCount
= InterlockedDecrement(&This
->ref
);
458 TRACE("(%p)->(count=%u)\n", This
, refCount
+ 1);
462 if (This
->pcRefDll
) InterlockedDecrement(This
->pcRefDll
);
464 TRACE("-- destroying IClassFactory(%p)\n",This
);
465 HeapFree(GetProcessHeap(),0,This
);
470 /******************************************************************************
471 * IDefClF_fnCreateInstance
473 static HRESULT WINAPI
IDefClF_fnCreateInstance(
474 LPCLASSFACTORY iface
, LPUNKNOWN pUnkOuter
, REFIID riid
, LPVOID
*ppvObject
)
476 IDefClFImpl
*This
= (IDefClFImpl
*)iface
;
478 TRACE("%p->(%p,%s,%p)\n",This
,pUnkOuter
,shdebugstr_guid(riid
),ppvObject
);
482 if ( This
->riidInst
==NULL
||
483 IsEqualCLSID(riid
, This
->riidInst
) ||
484 IsEqualCLSID(riid
, &IID_IUnknown
) )
486 return This
->lpfnCI(pUnkOuter
, riid
, ppvObject
);
489 ERR("unknown IID requested %s\n",shdebugstr_guid(riid
));
490 return E_NOINTERFACE
;
492 /******************************************************************************
493 * IDefClF_fnLockServer
495 static HRESULT WINAPI
IDefClF_fnLockServer(LPCLASSFACTORY iface
, BOOL fLock
)
497 IDefClFImpl
*This
= (IDefClFImpl
*)iface
;
498 TRACE("%p->(0x%x), not implemented\n",This
, fLock
);
502 static const IClassFactoryVtbl dclfvt
=
504 IDefClF_fnQueryInterface
,
507 IDefClF_fnCreateInstance
,
511 /******************************************************************************
512 * SHCreateDefClassObject [SHELL32.70]
514 HRESULT WINAPI
SHCreateDefClassObject(
517 LPFNCREATEINSTANCE lpfnCI
, /* [in] create instance callback entry */
518 LPDWORD pcRefDll
, /* [in/out] ref count of the dll */
519 REFIID riidInst
) /* [in] optional interface to the instance */
523 TRACE("%s %p %p %p %s\n",
524 shdebugstr_guid(riid
), ppv
, lpfnCI
, pcRefDll
, shdebugstr_guid(riidInst
));
526 if (! IsEqualCLSID(riid
, &IID_IClassFactory
) ) return E_NOINTERFACE
;
527 if (! (pcf
= IDefClF_fnConstructor(lpfnCI
, (PLONG
)pcRefDll
, riidInst
))) return E_OUTOFMEMORY
;
532 /*************************************************************************
533 * DragAcceptFiles [SHELL32.@]
535 void WINAPI
DragAcceptFiles(HWND hWnd
, BOOL b
)
539 if( !IsWindow(hWnd
) ) return;
540 exstyle
= GetWindowLongA(hWnd
,GWL_EXSTYLE
);
542 exstyle
|= WS_EX_ACCEPTFILES
;
544 exstyle
&= ~WS_EX_ACCEPTFILES
;
545 SetWindowLongA(hWnd
,GWL_EXSTYLE
,exstyle
);
548 /*************************************************************************
549 * DragFinish [SHELL32.@]
551 void WINAPI
DragFinish(HDROP h
)
554 GlobalFree((HGLOBAL
)h
);
557 /*************************************************************************
558 * DragQueryPoint [SHELL32.@]
560 BOOL WINAPI
DragQueryPoint(HDROP hDrop
, POINT
*p
)
562 DROPFILES
*lpDropFileStruct
;
567 lpDropFileStruct
= (DROPFILES
*) GlobalLock(hDrop
);
569 *p
= lpDropFileStruct
->pt
;
570 bRet
= lpDropFileStruct
->fNC
;
576 /*************************************************************************
577 * DragQueryFileA [SHELL32.@]
578 * DragQueryFile [SHELL32.@]
580 UINT WINAPI
DragQueryFileA(
588 DROPFILES
*lpDropFileStruct
= (DROPFILES
*) GlobalLock(hDrop
);
590 TRACE("(%p, %x, %p, %u)\n", hDrop
,lFile
,lpszFile
,lLength
);
592 if(!lpDropFileStruct
) goto end
;
594 lpDrop
= (LPSTR
) lpDropFileStruct
+ lpDropFileStruct
->pFiles
;
596 if(lpDropFileStruct
->fWide
) {
597 LPWSTR lpszFileW
= NULL
;
600 lpszFileW
= HeapAlloc(GetProcessHeap(), 0, lLength
*sizeof(WCHAR
));
601 if(lpszFileW
== NULL
) {
605 i
= DragQueryFileW(hDrop
, lFile
, lpszFileW
, lLength
);
608 WideCharToMultiByte(CP_ACP
, 0, lpszFileW
, -1, lpszFile
, lLength
, 0, NULL
);
609 HeapFree(GetProcessHeap(), 0, lpszFileW
);
616 while (*lpDrop
++); /* skip filename */
619 i
= (lFile
== 0xFFFFFFFF) ? i
: 0;
626 if (!lpszFile
) goto end
; /* needed buffer size */
627 i
= (lLength
> i
) ? i
: lLength
;
628 lstrcpynA (lpszFile
, lpDrop
, i
);
634 /*************************************************************************
635 * DragQueryFileW [SHELL32.@]
637 UINT WINAPI
DragQueryFileW(
645 DROPFILES
*lpDropFileStruct
= (DROPFILES
*) GlobalLock(hDrop
);
647 TRACE("(%p, %x, %p, %u)\n", hDrop
,lFile
,lpszwFile
,lLength
);
649 if(!lpDropFileStruct
) goto end
;
651 lpwDrop
= (LPWSTR
) ((LPSTR
)lpDropFileStruct
+ lpDropFileStruct
->pFiles
);
653 if(lpDropFileStruct
->fWide
== FALSE
) {
654 LPSTR lpszFileA
= NULL
;
657 lpszFileA
= HeapAlloc(GetProcessHeap(), 0, lLength
);
658 if(lpszFileA
== NULL
) {
662 i
= DragQueryFileA(hDrop
, lFile
, lpszFileA
, lLength
);
665 MultiByteToWideChar(CP_ACP
, 0, lpszFileA
, -1, lpszwFile
, lLength
);
666 HeapFree(GetProcessHeap(), 0, lpszFileA
);
674 while (*lpwDrop
++); /* skip filename */
677 i
= (lFile
== 0xFFFFFFFF) ? i
: 0;
682 i
= strlenW(lpwDrop
);
684 if ( !lpszwFile
) goto end
; /* needed buffer size */
686 i
= (lLength
> i
) ? i
: lLength
;
687 lstrcpynW (lpszwFile
, lpwDrop
, i
);