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 INT WINAPI
SHStringFromGUIDW(REFGUID guid
, LPWSTR lpszDest
, INT cchMax
); /* shlwapi.24 */
52 /**************************************************************************
53 * Default ClassFactory types
55 typedef HRESULT (CALLBACK
*LPFNCREATEINSTANCE
)(IUnknown
* pUnkOuter
, REFIID riid
, LPVOID
* ppvObject
);
56 static IClassFactory
* IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI
, PLONG pcRefDll
, REFIID riidInst
);
58 /* this table contains all CLSID's of shell32 objects */
61 LPFNCREATEINSTANCE lpfnCI
;
62 } InterfaceTable
[] = {
63 {&CLSID_ShellFSFolder
, IFSFolder_Constructor
},
64 {&CLSID_MyComputer
, ISF_MyComputer_Constructor
},
65 {&CLSID_NetworkPlaces
, ISF_NetworkPlaces_Constructor
},
66 {&CLSID_ShellDesktop
, ISF_Desktop_Constructor
},
67 {&CLSID_ShellLink
, IShellLink_Constructor
},
68 {&CLSID_DragDropHelper
, IDropTargetHelper_Constructor
},
69 {&CLSID_ControlPanel
, IControlPanel_Constructor
},
70 {&CLSID_AutoComplete
, IAutoComplete_Constructor
},
71 {&CLSID_UnixFolder
, UnixFolder_Constructor
},
72 {&CLSID_UnixDosFolder
, UnixDosFolder_Constructor
},
73 {&CLSID_FolderShortcut
, FolderShortcut_Constructor
},
74 {&CLSID_MyDocuments
, MyDocuments_Constructor
},
75 {&CLSID_RecycleBin
, RecycleBin_Constructor
},
81 /*************************************************************************
82 * SHCoCreateInstance [SHELL32.102]
84 * Equivalent to CoCreateInstance. Under Windows 9x this function could sometimes
85 * use the shell32 built-in "mini-COM" without the need to load ole32.dll - see
86 * SHLoadOLE for details.
88 * Under wine if a "LoadWithoutCOM" value is present or the object resides in
89 * shell32.dll the function will load the object manually without the help of ole32
95 * CoCreateInstace, SHLoadOLE
97 HRESULT WINAPI
SHCoCreateInstance(
106 const CLSID
* myclsid
= clsid
;
107 WCHAR sKeyName
[MAX_PATH
];
108 const WCHAR sCLSID
[7] = {'C','L','S','I','D','\\','\0'};
110 const WCHAR sInProcServer32
[16] ={'\\','I','n','p','r','o','c','S','e','r','v','e','r','3','2','\0'};
111 const WCHAR sLoadWithoutCOM
[15] ={'L','o','a','d','W','i','t','h','o','u','t','C','O','M','\0'};
112 WCHAR sDllPath
[MAX_PATH
];
115 IClassFactory
* pcf
= NULL
;
117 if(!ppv
) return E_POINTER
;
120 /* if the clsid is a string, convert it */
123 if (!aclsid
) return REGDB_E_CLASSNOTREG
;
124 SHCLSIDFromStringW(aclsid
, &iid
);
128 TRACE("(%p,%s,unk:%p,%s,%p)\n",
129 aclsid
,shdebugstr_guid(myclsid
),pUnkOuter
,shdebugstr_guid(refiid
),ppv
);
131 if (SUCCEEDED(DllGetClassObject(myclsid
, &IID_IClassFactory
,(LPVOID
*)&pcf
)))
133 hres
= IClassFactory_CreateInstance(pcf
, pUnkOuter
, refiid
, ppv
);
134 IClassFactory_Release(pcf
);
138 /* we look up the dll path in the registry */
139 SHStringFromGUIDW(myclsid
, sClassID
, sizeof(sClassID
)/sizeof(WCHAR
));
140 lstrcpyW(sKeyName
, sCLSID
);
141 lstrcatW(sKeyName
, sClassID
);
142 lstrcatW(sKeyName
, sInProcServer32
);
144 if (RegOpenKeyExW(HKEY_CLASSES_ROOT
, sKeyName
, 0, KEY_READ
, &hKey
))
145 return E_ACCESSDENIED
;
147 /* if a special registry key is set, we load a shell extension without help of OLE32 */
148 if (!SHQueryValueExW(hKey
, sLoadWithoutCOM
, 0, 0, 0, 0))
150 /* load an external dll without ole32 */
152 typedef HRESULT (CALLBACK
*DllGetClassObjectFunc
)(REFCLSID clsid
, REFIID iid
, LPVOID
*ppv
);
153 DllGetClassObjectFunc DllGetClassObject
;
155 dwSize
= sizeof(sDllPath
);
156 SHQueryValueExW(hKey
, NULL
, 0,0, sDllPath
, &dwSize
);
158 if ((hLibrary
= LoadLibraryExW(sDllPath
, 0, LOAD_WITH_ALTERED_SEARCH_PATH
)) == 0) {
159 ERR("couldn't load InprocServer32 dll %s\n", debugstr_w(sDllPath
));
160 hres
= E_ACCESSDENIED
;
162 } else if (!(DllGetClassObject
= (DllGetClassObjectFunc
)GetProcAddress(hLibrary
, "DllGetClassObject"))) {
163 ERR("couldn't find function DllGetClassObject in %s\n", debugstr_w(sDllPath
));
164 FreeLibrary( hLibrary
);
165 hres
= E_ACCESSDENIED
;
167 } else if (FAILED(hres
= DllGetClassObject(myclsid
, &IID_IClassFactory
, (LPVOID
*)&pcf
))) {
168 TRACE("GetClassObject failed 0x%08x\n", hres
);
172 hres
= IClassFactory_CreateInstance(pcf
, pUnkOuter
, refiid
, ppv
);
173 IClassFactory_Release(pcf
);
176 /* load an external dll in the usual way */
177 hres
= CoCreateInstance(myclsid
, pUnkOuter
, CLSCTX_INPROC_SERVER
, refiid
, ppv
);
181 if (hKey
) RegCloseKey(hKey
);
184 ERR("failed (0x%08x) to create CLSID:%s IID:%s\n",
185 hres
, shdebugstr_guid(myclsid
), shdebugstr_guid(refiid
));
186 ERR("class not found in registry\n");
189 TRACE("-- instance: %p\n",*ppv
);
193 /*************************************************************************
194 * DllGetClassObject [SHELL32.@]
195 * SHDllGetClassObject [SHELL32.128]
197 HRESULT WINAPI
DllGetClassObject(REFCLSID rclsid
, REFIID iid
, LPVOID
*ppv
)
199 HRESULT hres
= E_OUTOFMEMORY
;
200 IClassFactory
* pcf
= NULL
;
203 TRACE("CLSID:%s,IID:%s\n",shdebugstr_guid(rclsid
),shdebugstr_guid(iid
));
205 if (!ppv
) return E_INVALIDARG
;
208 /* search our internal interface table */
209 for(i
=0;InterfaceTable
[i
].riid
;i
++) {
210 if(IsEqualIID(InterfaceTable
[i
].riid
, rclsid
)) {
211 TRACE("index[%u]\n", i
);
212 pcf
= IDefClF_fnConstructor(InterfaceTable
[i
].lpfnCI
, NULL
, NULL
);
217 FIXME("failed for CLSID=%s\n", shdebugstr_guid(rclsid
));
218 return CLASS_E_CLASSNOTAVAILABLE
;
221 hres
= IClassFactory_QueryInterface(pcf
, iid
, ppv
);
222 IClassFactory_Release(pcf
);
224 TRACE("-- pointer to class factory: %p\n",*ppv
);
228 /*************************************************************************
229 * SHCLSIDFromString [SHELL32.147]
231 * Under Windows 9x this was an ANSI version of CLSIDFromString. It also allowed
232 * to avoid dependency on ole32.dll (see SHLoadOLE for details).
234 * Under Windows NT/2000/XP this is equivalent to CLSIDFromString
237 * exported by ordinal
240 * CLSIDFromString, SHLoadOLE
242 DWORD WINAPI
SHCLSIDFromStringA (LPCSTR clsid
, CLSID
*id
)
245 TRACE("(%p(%s) %p)\n", clsid
, clsid
, id
);
246 if (!MultiByteToWideChar( CP_ACP
, 0, clsid
, -1, buffer
, sizeof(buffer
)/sizeof(WCHAR
) ))
247 return CO_E_CLASSSTRING
;
248 return CLSIDFromString( buffer
, id
);
250 DWORD WINAPI
SHCLSIDFromStringW (LPCWSTR clsid
, CLSID
*id
)
252 TRACE("(%p(%s) %p)\n", clsid
, debugstr_w(clsid
), id
);
253 return CLSIDFromString((LPWSTR
)clsid
, id
);
255 DWORD WINAPI
SHCLSIDFromStringAW (LPCVOID clsid
, CLSID
*id
)
257 if (SHELL_OsIsUnicode())
258 return SHCLSIDFromStringW (clsid
, id
);
259 return SHCLSIDFromStringA (clsid
, id
);
262 /*************************************************************************
263 * SHGetMalloc [SHELL32.@]
265 * Equivalent to CoGetMalloc(MEMCTX_TASK, ...). Under Windows 9x this function
266 * could use the shell32 built-in "mini-COM" without the need to load ole32.dll -
267 * see SHLoadOLE for details.
270 * lpmal [O] Destination for IMalloc interface.
273 * Success: S_OK. lpmal contains the shells IMalloc interface.
274 * Failure. An HRESULT error code.
277 * CoGetMalloc, SHLoadOLE
279 HRESULT WINAPI
SHGetMalloc(LPMALLOC
*lpmal
)
281 TRACE("(%p)\n", lpmal
);
282 return CoGetMalloc(MEMCTX_TASK
, lpmal
);
285 /*************************************************************************
286 * SHAlloc [SHELL32.196]
288 * Equivalent to CoTaskMemAlloc. Under Windows 9x this function could use
289 * the shell32 built-in "mini-COM" without the need to load ole32.dll -
290 * see SHLoadOLE for details.
293 * exported by ordinal
296 * CoTaskMemAlloc, SHLoadOLE
298 LPVOID WINAPI
SHAlloc(DWORD len
)
302 ret
= CoTaskMemAlloc(len
);
303 TRACE("%u bytes at %p\n",len
, ret
);
307 /*************************************************************************
308 * SHFree [SHELL32.195]
310 * Equivalent to CoTaskMemFree. Under Windows 9x this function could use
311 * the shell32 built-in "mini-COM" without the need to load ole32.dll -
312 * see SHLoadOLE for details.
315 * exported by ordinal
318 * CoTaskMemFree, SHLoadOLE
320 void WINAPI
SHFree(LPVOID pv
)
326 /*************************************************************************
327 * SHGetDesktopFolder [SHELL32.@]
329 HRESULT WINAPI
SHGetDesktopFolder(IShellFolder
**psf
)
334 if(!psf
) return E_INVALIDARG
;
336 hres
= ISF_Desktop_Constructor(NULL
, &IID_IShellFolder
,(LPVOID
*)psf
);
338 TRACE("-- %p->(%p)\n",psf
, *psf
);
341 /**************************************************************************
342 * Default ClassFactory Implementation
344 * SHCreateDefClassObject
347 * Helper function for dlls without their own classfactory.
348 * A generic classfactory is returned.
349 * When the CreateInstance of the cf is called the callback is executed.
354 const IClassFactoryVtbl
*lpVtbl
;
357 LPFNCREATEINSTANCE lpfnCI
;
358 const IID
* riidInst
;
359 LONG
* pcRefDll
; /* pointer to refcounter in external dll (ugrrr...) */
362 static const IClassFactoryVtbl dclfvt
;
364 /**************************************************************************
365 * IDefClF_fnConstructor
368 static IClassFactory
* IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI
, PLONG pcRefDll
, REFIID riidInst
)
372 lpclf
= HeapAlloc(GetProcessHeap(),0,sizeof(IDefClFImpl
));
374 lpclf
->lpVtbl
= &dclfvt
;
375 lpclf
->lpfnCI
= lpfnCI
;
376 lpclf
->pcRefDll
= pcRefDll
;
378 if (pcRefDll
) InterlockedIncrement(pcRefDll
);
379 lpclf
->riidInst
= riidInst
;
381 TRACE("(%p)%s\n",lpclf
, shdebugstr_guid(riidInst
));
382 return (LPCLASSFACTORY
)lpclf
;
384 /**************************************************************************
385 * IDefClF_fnQueryInterface
387 static HRESULT WINAPI
IDefClF_fnQueryInterface(
388 LPCLASSFACTORY iface
, REFIID riid
, LPVOID
*ppvObj
)
390 IDefClFImpl
*This
= (IDefClFImpl
*)iface
;
392 TRACE("(%p)->(%s)\n",This
,shdebugstr_guid(riid
));
396 if(IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IClassFactory
)) {
398 InterlockedIncrement(&This
->ref
);
402 TRACE("-- E_NOINTERFACE\n");
403 return E_NOINTERFACE
;
405 /******************************************************************************
408 static ULONG WINAPI
IDefClF_fnAddRef(LPCLASSFACTORY iface
)
410 IDefClFImpl
*This
= (IDefClFImpl
*)iface
;
411 ULONG refCount
= InterlockedIncrement(&This
->ref
);
413 TRACE("(%p)->(count=%u)\n", This
, refCount
- 1);
417 /******************************************************************************
420 static ULONG WINAPI
IDefClF_fnRelease(LPCLASSFACTORY iface
)
422 IDefClFImpl
*This
= (IDefClFImpl
*)iface
;
423 ULONG refCount
= InterlockedDecrement(&This
->ref
);
425 TRACE("(%p)->(count=%u)\n", This
, refCount
+ 1);
429 if (This
->pcRefDll
) InterlockedDecrement(This
->pcRefDll
);
431 TRACE("-- destroying IClassFactory(%p)\n",This
);
432 HeapFree(GetProcessHeap(),0,This
);
437 /******************************************************************************
438 * IDefClF_fnCreateInstance
440 static HRESULT WINAPI
IDefClF_fnCreateInstance(
441 LPCLASSFACTORY iface
, LPUNKNOWN pUnkOuter
, REFIID riid
, LPVOID
*ppvObject
)
443 IDefClFImpl
*This
= (IDefClFImpl
*)iface
;
445 TRACE("%p->(%p,%s,%p)\n",This
,pUnkOuter
,shdebugstr_guid(riid
),ppvObject
);
449 if ( This
->riidInst
==NULL
||
450 IsEqualCLSID(riid
, This
->riidInst
) ||
451 IsEqualCLSID(riid
, &IID_IUnknown
) )
453 return This
->lpfnCI(pUnkOuter
, riid
, ppvObject
);
456 ERR("unknown IID requested %s\n",shdebugstr_guid(riid
));
457 return E_NOINTERFACE
;
459 /******************************************************************************
460 * IDefClF_fnLockServer
462 static HRESULT WINAPI
IDefClF_fnLockServer(LPCLASSFACTORY iface
, BOOL fLock
)
464 IDefClFImpl
*This
= (IDefClFImpl
*)iface
;
465 TRACE("%p->(0x%x), not implemented\n",This
, fLock
);
469 static const IClassFactoryVtbl dclfvt
=
471 IDefClF_fnQueryInterface
,
474 IDefClF_fnCreateInstance
,
478 /******************************************************************************
479 * SHCreateDefClassObject [SHELL32.70]
481 HRESULT WINAPI
SHCreateDefClassObject(
484 LPFNCREATEINSTANCE lpfnCI
, /* [in] create instance callback entry */
485 LPDWORD pcRefDll
, /* [in/out] ref count of the dll */
486 REFIID riidInst
) /* [in] optional interface to the instance */
490 TRACE("%s %p %p %p %s\n",
491 shdebugstr_guid(riid
), ppv
, lpfnCI
, pcRefDll
, shdebugstr_guid(riidInst
));
493 if (! IsEqualCLSID(riid
, &IID_IClassFactory
) ) return E_NOINTERFACE
;
494 if (! (pcf
= IDefClF_fnConstructor(lpfnCI
, (PLONG
)pcRefDll
, riidInst
))) return E_OUTOFMEMORY
;
499 /*************************************************************************
500 * DragAcceptFiles [SHELL32.@]
502 void WINAPI
DragAcceptFiles(HWND hWnd
, BOOL b
)
506 if( !IsWindow(hWnd
) ) return;
507 exstyle
= GetWindowLongA(hWnd
,GWL_EXSTYLE
);
509 exstyle
|= WS_EX_ACCEPTFILES
;
511 exstyle
&= ~WS_EX_ACCEPTFILES
;
512 SetWindowLongA(hWnd
,GWL_EXSTYLE
,exstyle
);
515 /*************************************************************************
516 * DragFinish [SHELL32.@]
518 void WINAPI
DragFinish(HDROP h
)
521 GlobalFree((HGLOBAL
)h
);
524 /*************************************************************************
525 * DragQueryPoint [SHELL32.@]
527 BOOL WINAPI
DragQueryPoint(HDROP hDrop
, POINT
*p
)
529 DROPFILES
*lpDropFileStruct
;
534 lpDropFileStruct
= GlobalLock(hDrop
);
536 *p
= lpDropFileStruct
->pt
;
537 bRet
= lpDropFileStruct
->fNC
;
543 /*************************************************************************
544 * DragQueryFileA [SHELL32.@]
545 * DragQueryFile [SHELL32.@]
547 UINT WINAPI
DragQueryFileA(
555 DROPFILES
*lpDropFileStruct
= GlobalLock(hDrop
);
557 TRACE("(%p, %x, %p, %u)\n", hDrop
,lFile
,lpszFile
,lLength
);
559 if(!lpDropFileStruct
) goto end
;
561 lpDrop
= (LPSTR
) lpDropFileStruct
+ lpDropFileStruct
->pFiles
;
563 if(lpDropFileStruct
->fWide
) {
564 LPWSTR lpszFileW
= NULL
;
567 lpszFileW
= HeapAlloc(GetProcessHeap(), 0, lLength
*sizeof(WCHAR
));
568 if(lpszFileW
== NULL
) {
572 i
= DragQueryFileW(hDrop
, lFile
, lpszFileW
, lLength
);
575 WideCharToMultiByte(CP_ACP
, 0, lpszFileW
, -1, lpszFile
, lLength
, 0, NULL
);
576 HeapFree(GetProcessHeap(), 0, lpszFileW
);
583 while (*lpDrop
++); /* skip filename */
586 i
= (lFile
== 0xFFFFFFFF) ? i
: 0;
592 if (!lpszFile
) goto end
; /* needed buffer size */
593 lstrcpynA (lpszFile
, lpDrop
, lLength
);
599 /*************************************************************************
600 * DragQueryFileW [SHELL32.@]
602 UINT WINAPI
DragQueryFileW(
610 DROPFILES
*lpDropFileStruct
= GlobalLock(hDrop
);
612 TRACE("(%p, %x, %p, %u)\n", hDrop
,lFile
,lpszwFile
,lLength
);
614 if(!lpDropFileStruct
) goto end
;
616 lpwDrop
= (LPWSTR
) ((LPSTR
)lpDropFileStruct
+ lpDropFileStruct
->pFiles
);
618 if(lpDropFileStruct
->fWide
== FALSE
) {
619 LPSTR lpszFileA
= NULL
;
622 lpszFileA
= HeapAlloc(GetProcessHeap(), 0, lLength
);
623 if(lpszFileA
== NULL
) {
627 i
= DragQueryFileA(hDrop
, lFile
, lpszFileA
, lLength
);
630 MultiByteToWideChar(CP_ACP
, 0, lpszFileA
, -1, lpszwFile
, lLength
);
631 HeapFree(GetProcessHeap(), 0, lpszFileA
);
639 while (*lpwDrop
++); /* skip filename */
642 i
= (lFile
== 0xFFFFFFFF) ? i
: 0;
647 i
= strlenW(lpwDrop
);
648 if ( !lpszwFile
) goto end
; /* needed buffer size */
649 lstrcpynW (lpszwFile
, lpwDrop
, lLength
);