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_ShellDesktop
, &ISF_Desktop_Constructor
},
68 {&CLSID_ShellLink
, &IShellLink_Constructor
},
69 {&CLSID_DragDropHelper
, &IDropTargetHelper_Constructor
},
70 {&CLSID_ControlPanel
, &IControlPanel_Constructor
},
71 {&CLSID_AutoComplete
, &IAutoComplete_Constructor
},
72 {&CLSID_UnixFolder
, &UnixFolder_Constructor
},
73 {&CLSID_UnixDosFolder
, &UnixDosFolder_Constructor
},
74 {&CLSID_FolderShortcut
, &FolderShortcut_Constructor
},
75 {&CLSID_MyDocuments
, &MyDocuments_Constructor
},
76 {&CLSID_RecycleBin
, &RecycleBin_Constructor
},
81 /* FIXME: this should be SHLWAPI.24 since we can't yet import by ordinal */
83 DWORD WINAPI
__SHGUIDToStringW (REFGUID guid
, LPWSTR str
)
85 WCHAR sFormat
[52] = {'{','%','0','8','l','x','-','%','0','4',
86 'x','-','%','0','4','x','-','%','0','2',
87 'x','%','0','2','x','-','%','0','2','x',
88 '%','0','2','x','%','0','2','x','%','0',
89 '2','x','%','0','2','x','%','0','2','x',
92 return wsprintfW ( str
, sFormat
,
93 guid
->Data1
, guid
->Data2
, guid
->Data3
,
94 guid
->Data4
[0], guid
->Data4
[1], guid
->Data4
[2], guid
->Data4
[3],
95 guid
->Data4
[4], guid
->Data4
[5], guid
->Data4
[6], guid
->Data4
[7] );
99 /*************************************************************************
100 * SHCoCreateInstance [SHELL32.102]
102 * Equivalent to CoCreateInstance. Under Windows 9x this function could sometimes
103 * use the shell32 built-in "mini-COM" without the need to load ole32.dll - see
104 * SHLoadOLE for details.
106 * Under wine if a "LoadWithoutCOM" value is present or the object resides in
107 * shell32.dll the function will load the object manually without the help of ole32
110 * exported by ordinal
113 * CoCreateInstace, SHLoadOLE
115 HRESULT WINAPI
SHCoCreateInstance(
124 const CLSID
* myclsid
= clsid
;
125 WCHAR sKeyName
[MAX_PATH
];
126 const WCHAR sCLSID
[7] = {'C','L','S','I','D','\\','\0'};
128 const WCHAR sInProcServer32
[16] ={'\\','I','n','p','r','o','c','S','e','r','v','e','r','3','2','\0'};
129 const WCHAR sLoadWithoutCOM
[15] ={'L','o','a','d','W','i','t','h','o','u','t','C','O','M','\0'};
130 WCHAR sDllPath
[MAX_PATH
];
133 BOOLEAN bLoadFromShell32
= FALSE
;
134 BOOLEAN bLoadWithoutCOM
= FALSE
;
135 IClassFactory
* pcf
= NULL
;
137 if(!ppv
) return E_POINTER
;
140 /* if the clsid is a string, convert it */
143 if (!aclsid
) return REGDB_E_CLASSNOTREG
;
144 SHCLSIDFromStringW(aclsid
, &iid
);
148 TRACE("(%p,%s,unk:%p,%s,%p)\n",
149 aclsid
,shdebugstr_guid(myclsid
),pUnkOuter
,shdebugstr_guid(refiid
),ppv
);
151 /* we look up the dll path in the registry */
152 __SHGUIDToStringW(myclsid
, sClassID
);
153 lstrcpyW(sKeyName
, sCLSID
);
154 lstrcatW(sKeyName
, sClassID
);
155 lstrcatW(sKeyName
, sInProcServer32
);
157 if (ERROR_SUCCESS
== RegOpenKeyExW(HKEY_CLASSES_ROOT
, sKeyName
, 0, KEY_READ
, &hKey
)) {
158 dwSize
= sizeof(sDllPath
);
159 SHQueryValueExW(hKey
, NULL
, 0,0, sDllPath
, &dwSize
);
161 /* if a special registry key is set, we load a shell extension without help of OLE32 */
162 bLoadWithoutCOM
= (ERROR_SUCCESS
== SHQueryValueExW(hKey
, sLoadWithoutCOM
, 0, 0, 0, 0));
164 /* if the com object is inside shell32, omit use of ole32 */
165 bLoadFromShell32
= (0==lstrcmpiW( PathFindFileNameW(sDllPath
), sShell32
));
169 /* since we can't find it in the registry we try internally */
170 bLoadFromShell32
= TRUE
;
173 TRACE("WithoutCom=%u FromShell=%u\n", bLoadWithoutCOM
, bLoadFromShell32
);
175 /* now we create an instance */
176 if (bLoadFromShell32
) {
177 if (! SUCCEEDED(DllGetClassObject(myclsid
, &IID_IClassFactory
,(LPVOID
*)&pcf
))) {
178 ERR("LoadFromShell failed for CLSID=%s\n", shdebugstr_guid(myclsid
));
180 } else if (bLoadWithoutCOM
) {
182 /* load an external dll without ole32 */
184 typedef HRESULT (CALLBACK
*DllGetClassObjectFunc
)(REFCLSID clsid
, REFIID iid
, LPVOID
*ppv
);
185 DllGetClassObjectFunc DllGetClassObject
;
187 if ((hLibrary
= LoadLibraryExW(sDllPath
, 0, LOAD_WITH_ALTERED_SEARCH_PATH
)) == 0) {
188 ERR("couldn't load InprocServer32 dll %s\n", debugstr_w(sDllPath
));
189 hres
= E_ACCESSDENIED
;
191 } else if (!(DllGetClassObject
= (DllGetClassObjectFunc
)GetProcAddress(hLibrary
, "DllGetClassObject"))) {
192 ERR("couldn't find function DllGetClassObject in %s\n", debugstr_w(sDllPath
));
193 FreeLibrary( hLibrary
);
194 hres
= E_ACCESSDENIED
;
196 } else if (! SUCCEEDED(hres
= DllGetClassObject(myclsid
, &IID_IClassFactory
, (LPVOID
*)&pcf
))) {
197 TRACE("GetClassObject failed 0x%08x\n", hres
);
203 /* load an external dll in the usual way */
204 hres
= CoCreateInstance(myclsid
, pUnkOuter
, CLSCTX_INPROC_SERVER
, refiid
, ppv
);
208 /* here we should have a ClassFactory */
209 if (!pcf
) return E_ACCESSDENIED
;
211 hres
= IClassFactory_CreateInstance(pcf
, pUnkOuter
, refiid
, ppv
);
212 IClassFactory_Release(pcf
);
216 ERR("failed (0x%08x) to create CLSID:%s IID:%s\n",
217 hres
, shdebugstr_guid(myclsid
), shdebugstr_guid(refiid
));
218 ERR("class not found in registry\n");
221 TRACE("-- instance: %p\n",*ppv
);
225 /*************************************************************************
226 * DllGetClassObject [SHELL32.@]
227 * SHDllGetClassObject [SHELL32.128]
229 HRESULT WINAPI
DllGetClassObject(REFCLSID rclsid
, REFIID iid
, LPVOID
*ppv
)
231 HRESULT hres
= E_OUTOFMEMORY
;
232 IClassFactory
* pcf
= NULL
;
235 TRACE("CLSID:%s,IID:%s\n",shdebugstr_guid(rclsid
),shdebugstr_guid(iid
));
237 if (!ppv
) return E_INVALIDARG
;
240 /* search our internal interface table */
241 for(i
=0;InterfaceTable
[i
].riid
;i
++) {
242 if(IsEqualIID(InterfaceTable
[i
].riid
, rclsid
)) {
243 TRACE("index[%u]\n", i
);
244 pcf
= IDefClF_fnConstructor(InterfaceTable
[i
].lpfnCI
, NULL
, NULL
);
249 FIXME("failed for CLSID=%s\n", shdebugstr_guid(rclsid
));
250 return CLASS_E_CLASSNOTAVAILABLE
;
253 hres
= IClassFactory_QueryInterface(pcf
, iid
, ppv
);
254 IClassFactory_Release(pcf
);
256 TRACE("-- pointer to class factory: %p\n",*ppv
);
260 /*************************************************************************
261 * SHCLSIDFromString [SHELL32.147]
263 * Under Windows 9x this was an ANSI version of CLSIDFromString. It also allowed
264 * to avoid dependency on ole32.dll (see SHLoadOLE for details).
266 * Under Windows NT/2000/XP this is equivalent to CLSIDFromString
269 * exported by ordinal
272 * CLSIDFromString, SHLoadOLE
274 DWORD WINAPI
SHCLSIDFromStringA (LPCSTR clsid
, CLSID
*id
)
277 TRACE("(%p(%s) %p)\n", clsid
, clsid
, id
);
278 if (!MultiByteToWideChar( CP_ACP
, 0, clsid
, -1, buffer
, sizeof(buffer
)/sizeof(WCHAR
) ))
279 return CO_E_CLASSSTRING
;
280 return CLSIDFromString( buffer
, id
);
282 DWORD WINAPI
SHCLSIDFromStringW (LPCWSTR clsid
, CLSID
*id
)
284 TRACE("(%p(%s) %p)\n", clsid
, debugstr_w(clsid
), id
);
285 return CLSIDFromString((LPWSTR
)clsid
, id
);
287 DWORD WINAPI
SHCLSIDFromStringAW (LPVOID clsid
, CLSID
*id
)
289 if (SHELL_OsIsUnicode())
290 return SHCLSIDFromStringW (clsid
, id
);
291 return SHCLSIDFromStringA (clsid
, id
);
294 /*************************************************************************
295 * SHGetMalloc [SHELL32.@]
297 * Equivalent to CoGetMalloc(MEMCTX_TASK, ...). Under Windows 9x this function
298 * could use the shell32 built-in "mini-COM" without the need to load ole32.dll -
299 * see SHLoadOLE for details.
302 * lpmal [O] Destination for IMalloc interface.
305 * Success: S_OK. lpmal contains the shells IMalloc interface.
306 * Failure. An HRESULT error code.
309 * CoGetMalloc, SHLoadOLE
311 HRESULT WINAPI
SHGetMalloc(LPMALLOC
*lpmal
)
313 TRACE("(%p)\n", lpmal
);
314 return CoGetMalloc(MEMCTX_TASK
, lpmal
);
317 /*************************************************************************
318 * SHAlloc [SHELL32.196]
320 * Equivalent to CoTaskMemAlloc. Under Windows 9x this function could use
321 * the shell32 built-in "mini-COM" without the need to load ole32.dll -
322 * see SHLoadOLE for details.
325 * exported by ordinal
328 * CoTaskMemAlloc, SHLoadOLE
330 LPVOID WINAPI
SHAlloc(DWORD len
)
334 ret
= CoTaskMemAlloc(len
);
335 TRACE("%u bytes at %p\n",len
, ret
);
339 /*************************************************************************
340 * SHFree [SHELL32.195]
342 * Equivalent to CoTaskMemFree. Under Windows 9x this function could use
343 * the shell32 built-in "mini-COM" without the need to load ole32.dll -
344 * see SHLoadOLE for details.
347 * exported by ordinal
350 * CoTaskMemFree, SHLoadOLE
352 void WINAPI
SHFree(LPVOID pv
)
358 /*************************************************************************
359 * SHGetDesktopFolder [SHELL32.@]
361 HRESULT WINAPI
SHGetDesktopFolder(IShellFolder
**psf
)
366 if(!psf
) return E_INVALIDARG
;
368 hres
= ISF_Desktop_Constructor(NULL
, &IID_IShellFolder
,(LPVOID
*)psf
);
370 TRACE("-- %p->(%p)\n",psf
, *psf
);
373 /**************************************************************************
374 * Default ClassFactory Implementation
376 * SHCreateDefClassObject
379 * Helper function for dlls without their own classfactory.
380 * A generic classfactory is returned.
381 * When the CreateInstance of the cf is called the callback is executed.
386 const IClassFactoryVtbl
*lpVtbl
;
389 LPFNCREATEINSTANCE lpfnCI
;
390 const IID
* riidInst
;
391 LONG
* pcRefDll
; /* pointer to refcounter in external dll (ugrrr...) */
394 static const IClassFactoryVtbl dclfvt
;
396 /**************************************************************************
397 * IDefClF_fnConstructor
400 static IClassFactory
* IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI
, PLONG pcRefDll
, REFIID riidInst
)
404 lpclf
= HeapAlloc(GetProcessHeap(),0,sizeof(IDefClFImpl
));
406 lpclf
->lpVtbl
= &dclfvt
;
407 lpclf
->lpfnCI
= lpfnCI
;
408 lpclf
->pcRefDll
= pcRefDll
;
410 if (pcRefDll
) InterlockedIncrement(pcRefDll
);
411 lpclf
->riidInst
= riidInst
;
413 TRACE("(%p)%s\n",lpclf
, shdebugstr_guid(riidInst
));
414 return (LPCLASSFACTORY
)lpclf
;
416 /**************************************************************************
417 * IDefClF_fnQueryInterface
419 static HRESULT WINAPI
IDefClF_fnQueryInterface(
420 LPCLASSFACTORY iface
, REFIID riid
, LPVOID
*ppvObj
)
422 IDefClFImpl
*This
= (IDefClFImpl
*)iface
;
424 TRACE("(%p)->(%s)\n",This
,shdebugstr_guid(riid
));
428 if(IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IClassFactory
)) {
430 InterlockedIncrement(&This
->ref
);
434 TRACE("-- E_NOINTERFACE\n");
435 return E_NOINTERFACE
;
437 /******************************************************************************
440 static ULONG WINAPI
IDefClF_fnAddRef(LPCLASSFACTORY iface
)
442 IDefClFImpl
*This
= (IDefClFImpl
*)iface
;
443 ULONG refCount
= InterlockedIncrement(&This
->ref
);
445 TRACE("(%p)->(count=%u)\n", This
, refCount
- 1);
449 /******************************************************************************
452 static ULONG WINAPI
IDefClF_fnRelease(LPCLASSFACTORY iface
)
454 IDefClFImpl
*This
= (IDefClFImpl
*)iface
;
455 ULONG refCount
= InterlockedDecrement(&This
->ref
);
457 TRACE("(%p)->(count=%u)\n", This
, refCount
+ 1);
461 if (This
->pcRefDll
) InterlockedDecrement(This
->pcRefDll
);
463 TRACE("-- destroying IClassFactory(%p)\n",This
);
464 HeapFree(GetProcessHeap(),0,This
);
469 /******************************************************************************
470 * IDefClF_fnCreateInstance
472 static HRESULT WINAPI
IDefClF_fnCreateInstance(
473 LPCLASSFACTORY iface
, LPUNKNOWN pUnkOuter
, REFIID riid
, LPVOID
*ppvObject
)
475 IDefClFImpl
*This
= (IDefClFImpl
*)iface
;
477 TRACE("%p->(%p,%s,%p)\n",This
,pUnkOuter
,shdebugstr_guid(riid
),ppvObject
);
481 if ( This
->riidInst
==NULL
||
482 IsEqualCLSID(riid
, This
->riidInst
) ||
483 IsEqualCLSID(riid
, &IID_IUnknown
) )
485 return This
->lpfnCI(pUnkOuter
, riid
, ppvObject
);
488 ERR("unknown IID requested %s\n",shdebugstr_guid(riid
));
489 return E_NOINTERFACE
;
491 /******************************************************************************
492 * IDefClF_fnLockServer
494 static HRESULT WINAPI
IDefClF_fnLockServer(LPCLASSFACTORY iface
, BOOL fLock
)
496 IDefClFImpl
*This
= (IDefClFImpl
*)iface
;
497 TRACE("%p->(0x%x), not implemented\n",This
, fLock
);
501 static const IClassFactoryVtbl dclfvt
=
503 IDefClF_fnQueryInterface
,
506 IDefClF_fnCreateInstance
,
510 /******************************************************************************
511 * SHCreateDefClassObject [SHELL32.70]
513 HRESULT WINAPI
SHCreateDefClassObject(
516 LPFNCREATEINSTANCE lpfnCI
, /* [in] create instance callback entry */
517 LPDWORD pcRefDll
, /* [in/out] ref count of the dll */
518 REFIID riidInst
) /* [in] optional interface to the instance */
522 TRACE("%s %p %p %p %s\n",
523 shdebugstr_guid(riid
), ppv
, lpfnCI
, pcRefDll
, shdebugstr_guid(riidInst
));
525 if (! IsEqualCLSID(riid
, &IID_IClassFactory
) ) return E_NOINTERFACE
;
526 if (! (pcf
= IDefClF_fnConstructor(lpfnCI
, (PLONG
)pcRefDll
, riidInst
))) return E_OUTOFMEMORY
;
531 /*************************************************************************
532 * DragAcceptFiles [SHELL32.@]
534 void WINAPI
DragAcceptFiles(HWND hWnd
, BOOL b
)
538 if( !IsWindow(hWnd
) ) return;
539 exstyle
= GetWindowLongA(hWnd
,GWL_EXSTYLE
);
541 exstyle
|= WS_EX_ACCEPTFILES
;
543 exstyle
&= ~WS_EX_ACCEPTFILES
;
544 SetWindowLongA(hWnd
,GWL_EXSTYLE
,exstyle
);
547 /*************************************************************************
548 * DragFinish [SHELL32.@]
550 void WINAPI
DragFinish(HDROP h
)
553 GlobalFree((HGLOBAL
)h
);
556 /*************************************************************************
557 * DragQueryPoint [SHELL32.@]
559 BOOL WINAPI
DragQueryPoint(HDROP hDrop
, POINT
*p
)
561 DROPFILES
*lpDropFileStruct
;
566 lpDropFileStruct
= (DROPFILES
*) GlobalLock(hDrop
);
568 *p
= lpDropFileStruct
->pt
;
569 bRet
= lpDropFileStruct
->fNC
;
575 /*************************************************************************
576 * DragQueryFileA [SHELL32.@]
577 * DragQueryFile [SHELL32.@]
579 UINT WINAPI
DragQueryFileA(
587 DROPFILES
*lpDropFileStruct
= (DROPFILES
*) GlobalLock(hDrop
);
589 TRACE("(%p, %x, %p, %u)\n", hDrop
,lFile
,lpszFile
,lLength
);
591 if(!lpDropFileStruct
) goto end
;
593 lpDrop
= (LPSTR
) lpDropFileStruct
+ lpDropFileStruct
->pFiles
;
595 if(lpDropFileStruct
->fWide
) {
596 LPWSTR lpszFileW
= NULL
;
599 lpszFileW
= HeapAlloc(GetProcessHeap(), 0, lLength
*sizeof(WCHAR
));
600 if(lpszFileW
== NULL
) {
604 i
= DragQueryFileW(hDrop
, lFile
, lpszFileW
, lLength
);
607 WideCharToMultiByte(CP_ACP
, 0, lpszFileW
, -1, lpszFile
, lLength
, 0, NULL
);
608 HeapFree(GetProcessHeap(), 0, lpszFileW
);
615 while (*lpDrop
++); /* skip filename */
618 i
= (lFile
== 0xFFFFFFFF) ? i
: 0;
624 if (!lpszFile
) goto end
; /* needed buffer size */
625 lstrcpynA (lpszFile
, lpDrop
, lLength
);
631 /*************************************************************************
632 * DragQueryFileW [SHELL32.@]
634 UINT WINAPI
DragQueryFileW(
642 DROPFILES
*lpDropFileStruct
= (DROPFILES
*) GlobalLock(hDrop
);
644 TRACE("(%p, %x, %p, %u)\n", hDrop
,lFile
,lpszwFile
,lLength
);
646 if(!lpDropFileStruct
) goto end
;
648 lpwDrop
= (LPWSTR
) ((LPSTR
)lpDropFileStruct
+ lpDropFileStruct
->pFiles
);
650 if(lpDropFileStruct
->fWide
== FALSE
) {
651 LPSTR lpszFileA
= NULL
;
654 lpszFileA
= HeapAlloc(GetProcessHeap(), 0, lLength
);
655 if(lpszFileA
== NULL
) {
659 i
= DragQueryFileA(hDrop
, lFile
, lpszFileA
, lLength
);
662 MultiByteToWideChar(CP_ACP
, 0, lpszFileA
, -1, lpszwFile
, lLength
);
663 HeapFree(GetProcessHeap(), 0, lpszFileA
);
671 while (*lpwDrop
++); /* skip filename */
674 i
= (lFile
== 0xFFFFFFFF) ? i
: 0;
679 i
= strlenW(lpwDrop
);
680 if ( !lpszwFile
) goto end
; /* needed buffer size */
681 lstrcpynW (lpszwFile
, lpwDrop
, lLength
);