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
[] = {
64 {&CLSID_AutoComplete
, IAutoComplete_Constructor
},
65 {&CLSID_ControlPanel
, IControlPanel_Constructor
},
66 {&CLSID_DragDropHelper
, IDropTargetHelper_Constructor
},
67 {&CLSID_FolderShortcut
, FolderShortcut_Constructor
},
68 {&CLSID_MyComputer
, ISF_MyComputer_Constructor
},
69 {&CLSID_MyDocuments
, MyDocuments_Constructor
},
70 {&CLSID_NetworkPlaces
, ISF_NetworkPlaces_Constructor
},
71 {&CLSID_Printers
, Printers_Constructor
},
72 {&CLSID_QueryAssociations
, QueryAssociations_Constructor
},
73 {&CLSID_RecycleBin
, RecycleBin_Constructor
},
74 {&CLSID_ShellDesktop
, ISF_Desktop_Constructor
},
75 {&CLSID_ShellFSFolder
, IFSFolder_Constructor
},
76 {&CLSID_ShellItem
, IShellItem_Constructor
},
77 {&CLSID_ShellLink
, IShellLink_Constructor
},
78 {&CLSID_UnixDosFolder
, UnixDosFolder_Constructor
},
79 {&CLSID_UnixFolder
, UnixFolder_Constructor
},
83 /*************************************************************************
84 * SHCoCreateInstance [SHELL32.102]
86 * Equivalent to CoCreateInstance. Under Windows 9x this function could sometimes
87 * use the shell32 built-in "mini-COM" without the need to load ole32.dll - see
88 * SHLoadOLE for details.
90 * Under wine if a "LoadWithoutCOM" value is present or the object resides in
91 * shell32.dll the function will load the object manually without the help of ole32
97 * CoCreateInstace, SHLoadOLE
99 HRESULT WINAPI
SHCoCreateInstance(
108 const CLSID
* myclsid
= clsid
;
109 WCHAR sKeyName
[MAX_PATH
];
110 const WCHAR sCLSID
[7] = {'C','L','S','I','D','\\','\0'};
112 const WCHAR sInProcServer32
[16] ={'\\','I','n','p','r','o','c','S','e','r','v','e','r','3','2','\0'};
113 const WCHAR sLoadWithoutCOM
[15] ={'L','o','a','d','W','i','t','h','o','u','t','C','O','M','\0'};
114 WCHAR sDllPath
[MAX_PATH
];
117 IClassFactory
* pcf
= NULL
;
119 if(!ppv
) return E_POINTER
;
122 /* if the clsid is a string, convert it */
125 if (!aclsid
) return REGDB_E_CLASSNOTREG
;
126 SHCLSIDFromStringW(aclsid
, &iid
);
130 TRACE("(%p,%s,unk:%p,%s,%p)\n",
131 aclsid
,shdebugstr_guid(myclsid
),pUnkOuter
,shdebugstr_guid(refiid
),ppv
);
133 if (SUCCEEDED(DllGetClassObject(myclsid
, &IID_IClassFactory
,(LPVOID
*)&pcf
)))
135 hres
= IClassFactory_CreateInstance(pcf
, pUnkOuter
, refiid
, ppv
);
136 IClassFactory_Release(pcf
);
140 /* we look up the dll path in the registry */
141 SHStringFromGUIDW(myclsid
, sClassID
, sizeof(sClassID
)/sizeof(WCHAR
));
142 lstrcpyW(sKeyName
, sCLSID
);
143 lstrcatW(sKeyName
, sClassID
);
144 lstrcatW(sKeyName
, sInProcServer32
);
146 if (RegOpenKeyExW(HKEY_CLASSES_ROOT
, sKeyName
, 0, KEY_READ
, &hKey
))
147 return E_ACCESSDENIED
;
149 /* if a special registry key is set, we load a shell extension without help of OLE32 */
150 if (!SHQueryValueExW(hKey
, sLoadWithoutCOM
, 0, 0, 0, 0))
152 /* load an external dll without ole32 */
154 typedef HRESULT (CALLBACK
*DllGetClassObjectFunc
)(REFCLSID clsid
, REFIID iid
, LPVOID
*ppv
);
155 DllGetClassObjectFunc DllGetClassObject
;
157 dwSize
= sizeof(sDllPath
);
158 SHQueryValueExW(hKey
, NULL
, 0,0, sDllPath
, &dwSize
);
160 if ((hLibrary
= LoadLibraryExW(sDllPath
, 0, LOAD_WITH_ALTERED_SEARCH_PATH
)) == 0) {
161 ERR("couldn't load InprocServer32 dll %s\n", debugstr_w(sDllPath
));
162 hres
= E_ACCESSDENIED
;
164 } else if (!(DllGetClassObject
= (DllGetClassObjectFunc
)GetProcAddress(hLibrary
, "DllGetClassObject"))) {
165 ERR("couldn't find function DllGetClassObject in %s\n", debugstr_w(sDllPath
));
166 FreeLibrary( hLibrary
);
167 hres
= E_ACCESSDENIED
;
169 } else if (FAILED(hres
= DllGetClassObject(myclsid
, &IID_IClassFactory
, (LPVOID
*)&pcf
))) {
170 TRACE("GetClassObject failed 0x%08x\n", hres
);
174 hres
= IClassFactory_CreateInstance(pcf
, pUnkOuter
, refiid
, ppv
);
175 IClassFactory_Release(pcf
);
178 /* load an external dll in the usual way */
179 hres
= CoCreateInstance(myclsid
, pUnkOuter
, CLSCTX_INPROC_SERVER
, refiid
, ppv
);
183 if (hKey
) RegCloseKey(hKey
);
186 ERR("failed (0x%08x) to create CLSID:%s IID:%s\n",
187 hres
, shdebugstr_guid(myclsid
), shdebugstr_guid(refiid
));
188 ERR("class not found in registry\n");
191 TRACE("-- instance: %p\n",*ppv
);
195 /*************************************************************************
196 * DllGetClassObject [SHELL32.@]
197 * SHDllGetClassObject [SHELL32.128]
199 HRESULT WINAPI
DllGetClassObject(REFCLSID rclsid
, REFIID iid
, LPVOID
*ppv
)
201 HRESULT hres
= E_OUTOFMEMORY
;
202 IClassFactory
* pcf
= NULL
;
205 TRACE("CLSID:%s,IID:%s\n",shdebugstr_guid(rclsid
),shdebugstr_guid(iid
));
207 if (!ppv
) return E_INVALIDARG
;
210 /* search our internal interface table */
211 for(i
=0;InterfaceTable
[i
].riid
;i
++) {
212 if(IsEqualIID(InterfaceTable
[i
].riid
, rclsid
)) {
213 TRACE("index[%u]\n", i
);
214 pcf
= IDefClF_fnConstructor(InterfaceTable
[i
].lpfnCI
, NULL
, NULL
);
219 FIXME("failed for CLSID=%s\n", shdebugstr_guid(rclsid
));
220 return CLASS_E_CLASSNOTAVAILABLE
;
223 hres
= IClassFactory_QueryInterface(pcf
, iid
, ppv
);
224 IClassFactory_Release(pcf
);
226 TRACE("-- pointer to class factory: %p\n",*ppv
);
230 /*************************************************************************
231 * SHCLSIDFromString [SHELL32.147]
233 * Under Windows 9x this was an ANSI version of CLSIDFromString. It also allowed
234 * to avoid dependency on ole32.dll (see SHLoadOLE for details).
236 * Under Windows NT/2000/XP this is equivalent to CLSIDFromString
239 * exported by ordinal
242 * CLSIDFromString, SHLoadOLE
244 DWORD WINAPI
SHCLSIDFromStringA (LPCSTR clsid
, CLSID
*id
)
247 TRACE("(%p(%s) %p)\n", clsid
, clsid
, id
);
248 if (!MultiByteToWideChar( CP_ACP
, 0, clsid
, -1, buffer
, sizeof(buffer
)/sizeof(WCHAR
) ))
249 return CO_E_CLASSSTRING
;
250 return CLSIDFromString( buffer
, id
);
252 DWORD WINAPI
SHCLSIDFromStringW (LPCWSTR clsid
, CLSID
*id
)
254 TRACE("(%p(%s) %p)\n", clsid
, debugstr_w(clsid
), id
);
255 return CLSIDFromString(clsid
, id
);
257 DWORD WINAPI
SHCLSIDFromStringAW (LPCVOID clsid
, CLSID
*id
)
259 if (SHELL_OsIsUnicode())
260 return SHCLSIDFromStringW (clsid
, id
);
261 return SHCLSIDFromStringA (clsid
, id
);
264 /*************************************************************************
265 * SHGetMalloc [SHELL32.@]
267 * Equivalent to CoGetMalloc(MEMCTX_TASK, ...). Under Windows 9x this function
268 * could use the shell32 built-in "mini-COM" without the need to load ole32.dll -
269 * see SHLoadOLE for details.
272 * lpmal [O] Destination for IMalloc interface.
275 * Success: S_OK. lpmal contains the shells IMalloc interface.
276 * Failure. An HRESULT error code.
279 * CoGetMalloc, SHLoadOLE
281 HRESULT WINAPI
SHGetMalloc(LPMALLOC
*lpmal
)
283 TRACE("(%p)\n", lpmal
);
284 return CoGetMalloc(MEMCTX_TASK
, lpmal
);
287 /*************************************************************************
288 * SHAlloc [SHELL32.196]
290 * Equivalent to CoTaskMemAlloc. Under Windows 9x this function could use
291 * the shell32 built-in "mini-COM" without the need to load ole32.dll -
292 * see SHLoadOLE for details.
295 * exported by ordinal
298 * CoTaskMemAlloc, SHLoadOLE
300 LPVOID WINAPI
SHAlloc(DWORD len
)
304 ret
= CoTaskMemAlloc(len
);
305 TRACE("%u bytes at %p\n",len
, ret
);
309 /*************************************************************************
310 * SHFree [SHELL32.195]
312 * Equivalent to CoTaskMemFree. Under Windows 9x this function could use
313 * the shell32 built-in "mini-COM" without the need to load ole32.dll -
314 * see SHLoadOLE for details.
317 * exported by ordinal
320 * CoTaskMemFree, SHLoadOLE
322 void WINAPI
SHFree(LPVOID pv
)
328 /*************************************************************************
329 * SHGetDesktopFolder [SHELL32.@]
331 HRESULT WINAPI
SHGetDesktopFolder(IShellFolder
**psf
)
335 TRACE("(%p)\n", psf
);
337 if(!psf
) return E_INVALIDARG
;
340 hres
= ISF_Desktop_Constructor(NULL
, &IID_IShellFolder
, (LPVOID
*)psf
);
342 TRACE("-- %p->(%p) 0x%08x\n", psf
, *psf
, hres
);
345 /**************************************************************************
346 * Default ClassFactory Implementation
348 * SHCreateDefClassObject
351 * Helper function for dlls without their own classfactory.
352 * A generic classfactory is returned.
353 * When the CreateInstance of the cf is called the callback is executed.
358 const IClassFactoryVtbl
*lpVtbl
;
361 LPFNCREATEINSTANCE lpfnCI
;
362 const IID
* riidInst
;
363 LONG
* pcRefDll
; /* pointer to refcounter in external dll (ugrrr...) */
366 static const IClassFactoryVtbl dclfvt
;
368 /**************************************************************************
369 * IDefClF_fnConstructor
372 static IClassFactory
* IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI
, PLONG pcRefDll
, REFIID riidInst
)
376 lpclf
= HeapAlloc(GetProcessHeap(),0,sizeof(IDefClFImpl
));
378 lpclf
->lpVtbl
= &dclfvt
;
379 lpclf
->lpfnCI
= lpfnCI
;
380 lpclf
->pcRefDll
= pcRefDll
;
382 if (pcRefDll
) InterlockedIncrement(pcRefDll
);
383 lpclf
->riidInst
= riidInst
;
385 TRACE("(%p)%s\n",lpclf
, shdebugstr_guid(riidInst
));
386 return (LPCLASSFACTORY
)lpclf
;
388 /**************************************************************************
389 * IDefClF_fnQueryInterface
391 static HRESULT WINAPI
IDefClF_fnQueryInterface(
392 LPCLASSFACTORY iface
, REFIID riid
, LPVOID
*ppvObj
)
394 IDefClFImpl
*This
= (IDefClFImpl
*)iface
;
396 TRACE("(%p)->(%s)\n",This
,shdebugstr_guid(riid
));
400 if(IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IClassFactory
)) {
402 InterlockedIncrement(&This
->ref
);
406 TRACE("-- E_NOINTERFACE\n");
407 return E_NOINTERFACE
;
409 /******************************************************************************
412 static ULONG WINAPI
IDefClF_fnAddRef(LPCLASSFACTORY iface
)
414 IDefClFImpl
*This
= (IDefClFImpl
*)iface
;
415 ULONG refCount
= InterlockedIncrement(&This
->ref
);
417 TRACE("(%p)->(count=%u)\n", This
, refCount
- 1);
421 /******************************************************************************
424 static ULONG WINAPI
IDefClF_fnRelease(LPCLASSFACTORY iface
)
426 IDefClFImpl
*This
= (IDefClFImpl
*)iface
;
427 ULONG refCount
= InterlockedDecrement(&This
->ref
);
429 TRACE("(%p)->(count=%u)\n", This
, refCount
+ 1);
433 if (This
->pcRefDll
) InterlockedDecrement(This
->pcRefDll
);
435 TRACE("-- destroying IClassFactory(%p)\n",This
);
436 HeapFree(GetProcessHeap(),0,This
);
441 /******************************************************************************
442 * IDefClF_fnCreateInstance
444 static HRESULT WINAPI
IDefClF_fnCreateInstance(
445 LPCLASSFACTORY iface
, LPUNKNOWN pUnkOuter
, REFIID riid
, LPVOID
*ppvObject
)
447 IDefClFImpl
*This
= (IDefClFImpl
*)iface
;
449 TRACE("%p->(%p,%s,%p)\n",This
,pUnkOuter
,shdebugstr_guid(riid
),ppvObject
);
453 if ( This
->riidInst
==NULL
||
454 IsEqualCLSID(riid
, This
->riidInst
) ||
455 IsEqualCLSID(riid
, &IID_IUnknown
) )
457 return This
->lpfnCI(pUnkOuter
, riid
, ppvObject
);
460 ERR("unknown IID requested %s\n",shdebugstr_guid(riid
));
461 return E_NOINTERFACE
;
463 /******************************************************************************
464 * IDefClF_fnLockServer
466 static HRESULT WINAPI
IDefClF_fnLockServer(LPCLASSFACTORY iface
, BOOL fLock
)
468 IDefClFImpl
*This
= (IDefClFImpl
*)iface
;
469 TRACE("%p->(0x%x), not implemented\n",This
, fLock
);
473 static const IClassFactoryVtbl dclfvt
=
475 IDefClF_fnQueryInterface
,
478 IDefClF_fnCreateInstance
,
482 /******************************************************************************
483 * SHCreateDefClassObject [SHELL32.70]
485 HRESULT WINAPI
SHCreateDefClassObject(
488 LPFNCREATEINSTANCE lpfnCI
, /* [in] create instance callback entry */
489 LPDWORD pcRefDll
, /* [in/out] ref count of the dll */
490 REFIID riidInst
) /* [in] optional interface to the instance */
494 TRACE("%s %p %p %p %s\n",
495 shdebugstr_guid(riid
), ppv
, lpfnCI
, pcRefDll
, shdebugstr_guid(riidInst
));
497 if (! IsEqualCLSID(riid
, &IID_IClassFactory
) ) return E_NOINTERFACE
;
498 if (! (pcf
= IDefClF_fnConstructor(lpfnCI
, (PLONG
)pcRefDll
, riidInst
))) return E_OUTOFMEMORY
;
503 /*************************************************************************
504 * DragAcceptFiles [SHELL32.@]
506 void WINAPI
DragAcceptFiles(HWND hWnd
, BOOL b
)
510 if( !IsWindow(hWnd
) ) return;
511 exstyle
= GetWindowLongA(hWnd
,GWL_EXSTYLE
);
513 exstyle
|= WS_EX_ACCEPTFILES
;
515 exstyle
&= ~WS_EX_ACCEPTFILES
;
516 SetWindowLongA(hWnd
,GWL_EXSTYLE
,exstyle
);
519 /*************************************************************************
520 * DragFinish [SHELL32.@]
522 void WINAPI
DragFinish(HDROP h
)
528 /*************************************************************************
529 * DragQueryPoint [SHELL32.@]
531 BOOL WINAPI
DragQueryPoint(HDROP hDrop
, POINT
*p
)
533 DROPFILES
*lpDropFileStruct
;
538 lpDropFileStruct
= GlobalLock(hDrop
);
540 *p
= lpDropFileStruct
->pt
;
541 bRet
= lpDropFileStruct
->fNC
;
547 /*************************************************************************
548 * DragQueryFileA [SHELL32.@]
549 * DragQueryFile [SHELL32.@]
551 UINT WINAPI
DragQueryFileA(
559 DROPFILES
*lpDropFileStruct
= GlobalLock(hDrop
);
561 TRACE("(%p, %x, %p, %u)\n", hDrop
,lFile
,lpszFile
,lLength
);
563 if(!lpDropFileStruct
) goto end
;
565 lpDrop
= (LPSTR
) lpDropFileStruct
+ lpDropFileStruct
->pFiles
;
567 if(lpDropFileStruct
->fWide
) {
568 LPWSTR lpszFileW
= NULL
;
571 lpszFileW
= HeapAlloc(GetProcessHeap(), 0, lLength
*sizeof(WCHAR
));
572 if(lpszFileW
== NULL
) {
576 i
= DragQueryFileW(hDrop
, lFile
, lpszFileW
, lLength
);
579 WideCharToMultiByte(CP_ACP
, 0, lpszFileW
, -1, lpszFile
, lLength
, 0, NULL
);
580 HeapFree(GetProcessHeap(), 0, lpszFileW
);
587 while (*lpDrop
++); /* skip filename */
590 i
= (lFile
== 0xFFFFFFFF) ? i
: 0;
596 if (!lpszFile
) goto end
; /* needed buffer size */
597 lstrcpynA (lpszFile
, lpDrop
, lLength
);
603 /*************************************************************************
604 * DragQueryFileW [SHELL32.@]
606 UINT WINAPI
DragQueryFileW(
614 DROPFILES
*lpDropFileStruct
= GlobalLock(hDrop
);
616 TRACE("(%p, %x, %p, %u)\n", hDrop
,lFile
,lpszwFile
,lLength
);
618 if(!lpDropFileStruct
) goto end
;
620 lpwDrop
= (LPWSTR
) ((LPSTR
)lpDropFileStruct
+ lpDropFileStruct
->pFiles
);
622 if(lpDropFileStruct
->fWide
== FALSE
) {
623 LPSTR lpszFileA
= NULL
;
626 lpszFileA
= HeapAlloc(GetProcessHeap(), 0, lLength
);
627 if(lpszFileA
== NULL
) {
631 i
= DragQueryFileA(hDrop
, lFile
, lpszFileA
, lLength
);
634 MultiByteToWideChar(CP_ACP
, 0, lpszFileA
, -1, lpszwFile
, lLength
);
635 HeapFree(GetProcessHeap(), 0, lpszFileA
);
643 while (*lpwDrop
++); /* skip filename */
646 i
= (lFile
== 0xFFFFFFFF) ? i
: 0;
651 i
= strlenW(lpwDrop
);
652 if ( !lpszwFile
) goto end
; /* needed buffer size */
653 lstrcpynW (lpszwFile
, lpwDrop
, lLength
);