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 IClassFactory
* pcf
= NULL
;
135 if(!ppv
) return E_POINTER
;
138 /* if the clsid is a string, convert it */
141 if (!aclsid
) return REGDB_E_CLASSNOTREG
;
142 SHCLSIDFromStringW(aclsid
, &iid
);
146 TRACE("(%p,%s,unk:%p,%s,%p)\n",
147 aclsid
,shdebugstr_guid(myclsid
),pUnkOuter
,shdebugstr_guid(refiid
),ppv
);
149 if (SUCCEEDED(DllGetClassObject(myclsid
, &IID_IClassFactory
,(LPVOID
*)&pcf
)))
151 hres
= IClassFactory_CreateInstance(pcf
, pUnkOuter
, refiid
, ppv
);
152 IClassFactory_Release(pcf
);
156 /* we look up the dll path in the registry */
157 __SHGUIDToStringW(myclsid
, sClassID
);
158 lstrcpyW(sKeyName
, sCLSID
);
159 lstrcatW(sKeyName
, sClassID
);
160 lstrcatW(sKeyName
, sInProcServer32
);
162 if (RegOpenKeyExW(HKEY_CLASSES_ROOT
, sKeyName
, 0, KEY_READ
, &hKey
))
163 return E_ACCESSDENIED
;
165 /* if a special registry key is set, we load a shell extension without help of OLE32 */
166 if (!SHQueryValueExW(hKey
, sLoadWithoutCOM
, 0, 0, 0, 0))
168 /* load an external dll without ole32 */
170 typedef HRESULT (CALLBACK
*DllGetClassObjectFunc
)(REFCLSID clsid
, REFIID iid
, LPVOID
*ppv
);
171 DllGetClassObjectFunc DllGetClassObject
;
173 dwSize
= sizeof(sDllPath
);
174 SHQueryValueExW(hKey
, NULL
, 0,0, sDllPath
, &dwSize
);
176 if ((hLibrary
= LoadLibraryExW(sDllPath
, 0, LOAD_WITH_ALTERED_SEARCH_PATH
)) == 0) {
177 ERR("couldn't load InprocServer32 dll %s\n", debugstr_w(sDllPath
));
178 hres
= E_ACCESSDENIED
;
180 } else if (!(DllGetClassObject
= (DllGetClassObjectFunc
)GetProcAddress(hLibrary
, "DllGetClassObject"))) {
181 ERR("couldn't find function DllGetClassObject in %s\n", debugstr_w(sDllPath
));
182 FreeLibrary( hLibrary
);
183 hres
= E_ACCESSDENIED
;
185 } else if (! SUCCEEDED(hres
= DllGetClassObject(myclsid
, &IID_IClassFactory
, (LPVOID
*)&pcf
))) {
186 TRACE("GetClassObject failed 0x%08x\n", hres
);
190 hres
= IClassFactory_CreateInstance(pcf
, pUnkOuter
, refiid
, ppv
);
191 IClassFactory_Release(pcf
);
194 /* load an external dll in the usual way */
195 hres
= CoCreateInstance(myclsid
, pUnkOuter
, CLSCTX_INPROC_SERVER
, refiid
, ppv
);
199 if (hKey
) RegCloseKey(hKey
);
202 ERR("failed (0x%08x) to create CLSID:%s IID:%s\n",
203 hres
, shdebugstr_guid(myclsid
), shdebugstr_guid(refiid
));
204 ERR("class not found in registry\n");
207 TRACE("-- instance: %p\n",*ppv
);
211 /*************************************************************************
212 * DllGetClassObject [SHELL32.@]
213 * SHDllGetClassObject [SHELL32.128]
215 HRESULT WINAPI
DllGetClassObject(REFCLSID rclsid
, REFIID iid
, LPVOID
*ppv
)
217 HRESULT hres
= E_OUTOFMEMORY
;
218 IClassFactory
* pcf
= NULL
;
221 TRACE("CLSID:%s,IID:%s\n",shdebugstr_guid(rclsid
),shdebugstr_guid(iid
));
223 if (!ppv
) return E_INVALIDARG
;
226 /* search our internal interface table */
227 for(i
=0;InterfaceTable
[i
].riid
;i
++) {
228 if(IsEqualIID(InterfaceTable
[i
].riid
, rclsid
)) {
229 TRACE("index[%u]\n", i
);
230 pcf
= IDefClF_fnConstructor(InterfaceTable
[i
].lpfnCI
, NULL
, NULL
);
235 FIXME("failed for CLSID=%s\n", shdebugstr_guid(rclsid
));
236 return CLASS_E_CLASSNOTAVAILABLE
;
239 hres
= IClassFactory_QueryInterface(pcf
, iid
, ppv
);
240 IClassFactory_Release(pcf
);
242 TRACE("-- pointer to class factory: %p\n",*ppv
);
246 /*************************************************************************
247 * SHCLSIDFromString [SHELL32.147]
249 * Under Windows 9x this was an ANSI version of CLSIDFromString. It also allowed
250 * to avoid dependency on ole32.dll (see SHLoadOLE for details).
252 * Under Windows NT/2000/XP this is equivalent to CLSIDFromString
255 * exported by ordinal
258 * CLSIDFromString, SHLoadOLE
260 DWORD WINAPI
SHCLSIDFromStringA (LPCSTR clsid
, CLSID
*id
)
263 TRACE("(%p(%s) %p)\n", clsid
, clsid
, id
);
264 if (!MultiByteToWideChar( CP_ACP
, 0, clsid
, -1, buffer
, sizeof(buffer
)/sizeof(WCHAR
) ))
265 return CO_E_CLASSSTRING
;
266 return CLSIDFromString( buffer
, id
);
268 DWORD WINAPI
SHCLSIDFromStringW (LPCWSTR clsid
, CLSID
*id
)
270 TRACE("(%p(%s) %p)\n", clsid
, debugstr_w(clsid
), id
);
271 return CLSIDFromString((LPWSTR
)clsid
, id
);
273 DWORD WINAPI
SHCLSIDFromStringAW (LPCVOID clsid
, CLSID
*id
)
275 if (SHELL_OsIsUnicode())
276 return SHCLSIDFromStringW (clsid
, id
);
277 return SHCLSIDFromStringA (clsid
, id
);
280 /*************************************************************************
281 * SHGetMalloc [SHELL32.@]
283 * Equivalent to CoGetMalloc(MEMCTX_TASK, ...). Under Windows 9x this function
284 * could use the shell32 built-in "mini-COM" without the need to load ole32.dll -
285 * see SHLoadOLE for details.
288 * lpmal [O] Destination for IMalloc interface.
291 * Success: S_OK. lpmal contains the shells IMalloc interface.
292 * Failure. An HRESULT error code.
295 * CoGetMalloc, SHLoadOLE
297 HRESULT WINAPI
SHGetMalloc(LPMALLOC
*lpmal
)
299 TRACE("(%p)\n", lpmal
);
300 return CoGetMalloc(MEMCTX_TASK
, lpmal
);
303 /*************************************************************************
304 * SHAlloc [SHELL32.196]
306 * Equivalent to CoTaskMemAlloc. Under Windows 9x this function could use
307 * the shell32 built-in "mini-COM" without the need to load ole32.dll -
308 * see SHLoadOLE for details.
311 * exported by ordinal
314 * CoTaskMemAlloc, SHLoadOLE
316 LPVOID WINAPI
SHAlloc(DWORD len
)
320 ret
= CoTaskMemAlloc(len
);
321 TRACE("%u bytes at %p\n",len
, ret
);
325 /*************************************************************************
326 * SHFree [SHELL32.195]
328 * Equivalent to CoTaskMemFree. Under Windows 9x this function could use
329 * the shell32 built-in "mini-COM" without the need to load ole32.dll -
330 * see SHLoadOLE for details.
333 * exported by ordinal
336 * CoTaskMemFree, SHLoadOLE
338 void WINAPI
SHFree(LPVOID pv
)
344 /*************************************************************************
345 * SHGetDesktopFolder [SHELL32.@]
347 HRESULT WINAPI
SHGetDesktopFolder(IShellFolder
**psf
)
352 if(!psf
) return E_INVALIDARG
;
354 hres
= ISF_Desktop_Constructor(NULL
, &IID_IShellFolder
,(LPVOID
*)psf
);
356 TRACE("-- %p->(%p)\n",psf
, *psf
);
359 /**************************************************************************
360 * Default ClassFactory Implementation
362 * SHCreateDefClassObject
365 * Helper function for dlls without their own classfactory.
366 * A generic classfactory is returned.
367 * When the CreateInstance of the cf is called the callback is executed.
372 const IClassFactoryVtbl
*lpVtbl
;
375 LPFNCREATEINSTANCE lpfnCI
;
376 const IID
* riidInst
;
377 LONG
* pcRefDll
; /* pointer to refcounter in external dll (ugrrr...) */
380 static const IClassFactoryVtbl dclfvt
;
382 /**************************************************************************
383 * IDefClF_fnConstructor
386 static IClassFactory
* IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI
, PLONG pcRefDll
, REFIID riidInst
)
390 lpclf
= HeapAlloc(GetProcessHeap(),0,sizeof(IDefClFImpl
));
392 lpclf
->lpVtbl
= &dclfvt
;
393 lpclf
->lpfnCI
= lpfnCI
;
394 lpclf
->pcRefDll
= pcRefDll
;
396 if (pcRefDll
) InterlockedIncrement(pcRefDll
);
397 lpclf
->riidInst
= riidInst
;
399 TRACE("(%p)%s\n",lpclf
, shdebugstr_guid(riidInst
));
400 return (LPCLASSFACTORY
)lpclf
;
402 /**************************************************************************
403 * IDefClF_fnQueryInterface
405 static HRESULT WINAPI
IDefClF_fnQueryInterface(
406 LPCLASSFACTORY iface
, REFIID riid
, LPVOID
*ppvObj
)
408 IDefClFImpl
*This
= (IDefClFImpl
*)iface
;
410 TRACE("(%p)->(%s)\n",This
,shdebugstr_guid(riid
));
414 if(IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IClassFactory
)) {
416 InterlockedIncrement(&This
->ref
);
420 TRACE("-- E_NOINTERFACE\n");
421 return E_NOINTERFACE
;
423 /******************************************************************************
426 static ULONG WINAPI
IDefClF_fnAddRef(LPCLASSFACTORY iface
)
428 IDefClFImpl
*This
= (IDefClFImpl
*)iface
;
429 ULONG refCount
= InterlockedIncrement(&This
->ref
);
431 TRACE("(%p)->(count=%u)\n", This
, refCount
- 1);
435 /******************************************************************************
438 static ULONG WINAPI
IDefClF_fnRelease(LPCLASSFACTORY iface
)
440 IDefClFImpl
*This
= (IDefClFImpl
*)iface
;
441 ULONG refCount
= InterlockedDecrement(&This
->ref
);
443 TRACE("(%p)->(count=%u)\n", This
, refCount
+ 1);
447 if (This
->pcRefDll
) InterlockedDecrement(This
->pcRefDll
);
449 TRACE("-- destroying IClassFactory(%p)\n",This
);
450 HeapFree(GetProcessHeap(),0,This
);
455 /******************************************************************************
456 * IDefClF_fnCreateInstance
458 static HRESULT WINAPI
IDefClF_fnCreateInstance(
459 LPCLASSFACTORY iface
, LPUNKNOWN pUnkOuter
, REFIID riid
, LPVOID
*ppvObject
)
461 IDefClFImpl
*This
= (IDefClFImpl
*)iface
;
463 TRACE("%p->(%p,%s,%p)\n",This
,pUnkOuter
,shdebugstr_guid(riid
),ppvObject
);
467 if ( This
->riidInst
==NULL
||
468 IsEqualCLSID(riid
, This
->riidInst
) ||
469 IsEqualCLSID(riid
, &IID_IUnknown
) )
471 return This
->lpfnCI(pUnkOuter
, riid
, ppvObject
);
474 ERR("unknown IID requested %s\n",shdebugstr_guid(riid
));
475 return E_NOINTERFACE
;
477 /******************************************************************************
478 * IDefClF_fnLockServer
480 static HRESULT WINAPI
IDefClF_fnLockServer(LPCLASSFACTORY iface
, BOOL fLock
)
482 IDefClFImpl
*This
= (IDefClFImpl
*)iface
;
483 TRACE("%p->(0x%x), not implemented\n",This
, fLock
);
487 static const IClassFactoryVtbl dclfvt
=
489 IDefClF_fnQueryInterface
,
492 IDefClF_fnCreateInstance
,
496 /******************************************************************************
497 * SHCreateDefClassObject [SHELL32.70]
499 HRESULT WINAPI
SHCreateDefClassObject(
502 LPFNCREATEINSTANCE lpfnCI
, /* [in] create instance callback entry */
503 LPDWORD pcRefDll
, /* [in/out] ref count of the dll */
504 REFIID riidInst
) /* [in] optional interface to the instance */
508 TRACE("%s %p %p %p %s\n",
509 shdebugstr_guid(riid
), ppv
, lpfnCI
, pcRefDll
, shdebugstr_guid(riidInst
));
511 if (! IsEqualCLSID(riid
, &IID_IClassFactory
) ) return E_NOINTERFACE
;
512 if (! (pcf
= IDefClF_fnConstructor(lpfnCI
, (PLONG
)pcRefDll
, riidInst
))) return E_OUTOFMEMORY
;
517 /*************************************************************************
518 * DragAcceptFiles [SHELL32.@]
520 void WINAPI
DragAcceptFiles(HWND hWnd
, BOOL b
)
524 if( !IsWindow(hWnd
) ) return;
525 exstyle
= GetWindowLongA(hWnd
,GWL_EXSTYLE
);
527 exstyle
|= WS_EX_ACCEPTFILES
;
529 exstyle
&= ~WS_EX_ACCEPTFILES
;
530 SetWindowLongA(hWnd
,GWL_EXSTYLE
,exstyle
);
533 /*************************************************************************
534 * DragFinish [SHELL32.@]
536 void WINAPI
DragFinish(HDROP h
)
539 GlobalFree((HGLOBAL
)h
);
542 /*************************************************************************
543 * DragQueryPoint [SHELL32.@]
545 BOOL WINAPI
DragQueryPoint(HDROP hDrop
, POINT
*p
)
547 DROPFILES
*lpDropFileStruct
;
552 lpDropFileStruct
= (DROPFILES
*) GlobalLock(hDrop
);
554 *p
= lpDropFileStruct
->pt
;
555 bRet
= lpDropFileStruct
->fNC
;
561 /*************************************************************************
562 * DragQueryFileA [SHELL32.@]
563 * DragQueryFile [SHELL32.@]
565 UINT WINAPI
DragQueryFileA(
573 DROPFILES
*lpDropFileStruct
= (DROPFILES
*) GlobalLock(hDrop
);
575 TRACE("(%p, %x, %p, %u)\n", hDrop
,lFile
,lpszFile
,lLength
);
577 if(!lpDropFileStruct
) goto end
;
579 lpDrop
= (LPSTR
) lpDropFileStruct
+ lpDropFileStruct
->pFiles
;
581 if(lpDropFileStruct
->fWide
) {
582 LPWSTR lpszFileW
= NULL
;
585 lpszFileW
= HeapAlloc(GetProcessHeap(), 0, lLength
*sizeof(WCHAR
));
586 if(lpszFileW
== NULL
) {
590 i
= DragQueryFileW(hDrop
, lFile
, lpszFileW
, lLength
);
593 WideCharToMultiByte(CP_ACP
, 0, lpszFileW
, -1, lpszFile
, lLength
, 0, NULL
);
594 HeapFree(GetProcessHeap(), 0, lpszFileW
);
601 while (*lpDrop
++); /* skip filename */
604 i
= (lFile
== 0xFFFFFFFF) ? i
: 0;
610 if (!lpszFile
) goto end
; /* needed buffer size */
611 lstrcpynA (lpszFile
, lpDrop
, lLength
);
617 /*************************************************************************
618 * DragQueryFileW [SHELL32.@]
620 UINT WINAPI
DragQueryFileW(
628 DROPFILES
*lpDropFileStruct
= (DROPFILES
*) GlobalLock(hDrop
);
630 TRACE("(%p, %x, %p, %u)\n", hDrop
,lFile
,lpszwFile
,lLength
);
632 if(!lpDropFileStruct
) goto end
;
634 lpwDrop
= (LPWSTR
) ((LPSTR
)lpDropFileStruct
+ lpDropFileStruct
->pFiles
);
636 if(lpDropFileStruct
->fWide
== FALSE
) {
637 LPSTR lpszFileA
= NULL
;
640 lpszFileA
= HeapAlloc(GetProcessHeap(), 0, lLength
);
641 if(lpszFileA
== NULL
) {
645 i
= DragQueryFileA(hDrop
, lFile
, lpszFileA
, lLength
);
648 MultiByteToWideChar(CP_ACP
, 0, lpszFileA
, -1, lpszwFile
, lLength
);
649 HeapFree(GetProcessHeap(), 0, lpszFileA
);
657 while (*lpwDrop
++); /* skip filename */
660 i
= (lFile
== 0xFFFFFFFF) ? i
: 0;
665 i
= strlenW(lpwDrop
);
666 if ( !lpszwFile
) goto end
; /* needed buffer size */
667 lstrcpynW (lpszwFile
, lpwDrop
, lLength
);