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
24 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
26 extern HRESULT WINAPI
IFSFolder_Constructor(IUnknown
* pUnkOuter
, REFIID riid
, LPVOID
* ppv
);
28 static const WCHAR sShell32
[12] = {'S','H','E','L','L','3','2','.','D','L','L','\0'};
30 /**************************************************************************
31 * Default ClassFactory types
33 typedef HRESULT (CALLBACK
*LPFNCREATEINSTANCE
)(IUnknown
* pUnkOuter
, REFIID riid
, LPVOID
* ppvObject
);
34 HRESULT
IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI
, PLONG pcRefDll
, const IID
*riidInst
, IClassFactory
**theFactory
);
36 /* FIXME: this should be SHLWAPI.24 since we can't yet import by ordinal */
38 DWORD WINAPI
__SHGUIDToStringW (REFGUID guid
, LPWSTR str
)
40 WCHAR sFormat
[52] = {'{','%','0','8','l','x','-','%','0','4',
41 'x','-','%','0','4','x','-','%','0','2',
42 'x','%','0','2','x','-','%','0','2','x',
43 '%','0','2','x','%','0','2','x','%','0',
44 '2','x','%','0','2','x','%','0','2','x',
47 return swprintf ( str
, sFormat
,
48 guid
.Data1
, guid
.Data2
, guid
.Data3
,
49 guid
.Data4
[0], guid
.Data4
[1], guid
.Data4
[2], guid
.Data4
[3],
50 guid
.Data4
[4], guid
.Data4
[5], guid
.Data4
[6], guid
.Data4
[7] );
54 /*************************************************************************
55 * SHCoCreateInstance [SHELL32.102]
57 * Equivalent to CoCreateInstance. Under Windows 9x this function could sometimes
58 * use the shell32 built-in "mini-COM" without the need to load ole32.dll - see
59 * SHLoadOLE for details.
61 * Under wine if a "LoadWithoutCOM" value is present or the object resides in
62 * shell32.dll the function will load the object manually without the help of ole32
68 * CoCreateInstace, SHLoadOLE
70 HRESULT WINAPI
SHCoCreateInstance(
79 const CLSID
* myclsid
= clsid
;
80 WCHAR sKeyName
[MAX_PATH
];
81 const WCHAR sCLSID
[7] = {'C','L','S','I','D','\\','\0'};
83 const WCHAR sInProcServer32
[16] ={'\\','I','n','p','r','o','c','S','e','r','v','e','r','3','2','\0'};
84 const WCHAR sLoadWithoutCOM
[15] ={'L','o','a','d','W','i','t','h','o','u','t','C','O','M','\0'};
85 WCHAR sDllPath
[MAX_PATH
];
88 BOOLEAN bLoadFromShell32
= FALSE
;
89 BOOLEAN bLoadWithoutCOM
= FALSE
;
90 CComPtr
<IClassFactory
> pcf
;
92 if(!ppv
) return E_POINTER
;
95 /* if the clsid is a string, convert it */
98 if (!aclsid
) return REGDB_E_CLASSNOTREG
;
99 CLSIDFromString((LPOLESTR
)aclsid
, &iid
);
103 TRACE("(%p,%s,unk:%p,%s,%p)\n",
104 aclsid
, shdebugstr_guid(myclsid
), pUnkOuter
, shdebugstr_guid(&refiid
), ppv
);
106 /* we look up the dll path in the registry */
107 __SHGUIDToStringW(*myclsid
, sClassID
);
108 wcscpy(sKeyName
, sCLSID
);
109 wcscat(sKeyName
, sClassID
);
110 wcscat(sKeyName
, sInProcServer32
);
112 if (ERROR_SUCCESS
== RegOpenKeyExW(HKEY_CLASSES_ROOT
, sKeyName
, 0, KEY_READ
, &hKey
)) {
113 dwSize
= sizeof(sDllPath
);
114 SHQueryValueExW(hKey
, NULL
, 0,0, sDllPath
, &dwSize
);
116 /* if a special registry key is set, we load a shell extension without help of OLE32 */
117 bLoadWithoutCOM
= (ERROR_SUCCESS
== SHQueryValueExW(hKey
, sLoadWithoutCOM
, 0, 0, 0, 0));
119 /* if the com object is inside shell32, omit use of ole32 */
120 bLoadFromShell32
= (0==lstrcmpiW( PathFindFileNameW(sDllPath
), sShell32
));
124 /* since we can't find it in the registry we try internally */
125 bLoadFromShell32
= TRUE
;
128 TRACE("WithoutCom=%u FromShell=%u\n", bLoadWithoutCOM
, bLoadFromShell32
);
130 /* now we create an instance */
131 if (bLoadFromShell32
) {
132 if (! SUCCEEDED(DllGetClassObject(*myclsid
, IID_IClassFactory
, (LPVOID
*)&pcf
))) {
133 ERR("LoadFromShell failed for CLSID=%s\n", shdebugstr_guid(myclsid
));
135 } else if (bLoadWithoutCOM
) {
137 /* load an external dll without ole32 */
139 typedef HRESULT (CALLBACK
*DllGetClassObjectFunc
)(REFCLSID clsid
, REFIID iid
, LPVOID
*ppv
);
140 DllGetClassObjectFunc DllGetClassObject
;
142 if ((hLibrary
= LoadLibraryExW(sDllPath
, 0, LOAD_WITH_ALTERED_SEARCH_PATH
)) == 0) {
143 ERR("couldn't load InprocServer32 dll %s\n", debugstr_w(sDllPath
));
144 hres
= E_ACCESSDENIED
;
146 } else if (!(DllGetClassObject
= (DllGetClassObjectFunc
)GetProcAddress(hLibrary
, "DllGetClassObject"))) {
147 ERR("couldn't find function DllGetClassObject in %s\n", debugstr_w(sDllPath
));
148 FreeLibrary( hLibrary
);
149 hres
= E_ACCESSDENIED
;
151 } else if (! SUCCEEDED(hres
= DllGetClassObject(*myclsid
, IID_IClassFactory
, (LPVOID
*)&pcf
))) {
152 TRACE("GetClassObject failed 0x%08x\n", hres
);
158 /* load an external dll in the usual way */
159 hres
= CoCreateInstance(*myclsid
, pUnkOuter
, CLSCTX_INPROC_SERVER
, refiid
, ppv
);
163 /* here we should have a ClassFactory */
164 if (!pcf
) return E_ACCESSDENIED
;
166 hres
= pcf
->CreateInstance(pUnkOuter
, refiid
, ppv
);
170 ERR("failed (0x%08x) to create CLSID:%s IID:%s\n",
171 hres
, shdebugstr_guid(myclsid
), shdebugstr_guid(&refiid
));
172 ERR("class not found in registry\n");
175 TRACE("-- instance: %p\n",*ppv
);
179 /*************************************************************************
180 * SHCLSIDFromString [SHELL32.147]
182 * Under Windows 9x this was an ANSI version of CLSIDFromString. It also allowed
183 * to avoid dependency on ole32.dll (see SHLoadOLE for details).
185 * Under Windows NT/2000/XP this is equivalent to CLSIDFromString
188 * exported by ordinal
191 * CLSIDFromString, SHLoadOLE
193 DWORD WINAPI
SHCLSIDFromStringA (LPCSTR clsid
, CLSID
*id
)
196 TRACE("(%p(%s) %p)\n", clsid
, clsid
, id
);
197 if (!MultiByteToWideChar( CP_ACP
, 0, clsid
, -1, buffer
, sizeof(buffer
)/sizeof(WCHAR
) ))
198 return CO_E_CLASSSTRING
;
199 return CLSIDFromString( buffer
, id
);
202 DWORD WINAPI
SHCLSIDFromStringW (LPCWSTR clsid
, CLSID
*id
)
204 TRACE("(%p(%s) %p)\n", clsid
, debugstr_w(clsid
), id
);
205 return CLSIDFromString((LPWSTR
)clsid
, id
);
208 EXTERN_C DWORD WINAPI
SHCLSIDFromStringAW (LPCVOID clsid
, CLSID
*id
)
210 if (SHELL_OsIsUnicode())
211 return SHCLSIDFromStringW ((LPCWSTR
)clsid
, id
);
212 return SHCLSIDFromStringA ((LPCSTR
)clsid
, id
);
215 /*************************************************************************
216 * SHGetMalloc [SHELL32.@]
218 * Equivalent to CoGetMalloc(MEMCTX_TASK, ...). Under Windows 9x this function
219 * could use the shell32 built-in "mini-COM" without the need to load ole32.dll -
220 * see SHLoadOLE for details.
223 * lpmal [O] Destination for IMalloc interface.
226 * Success: S_OK. lpmal contains the shells IMalloc interface.
227 * Failure. An HRESULT error code.
230 * CoGetMalloc, SHLoadOLE
232 HRESULT WINAPI
SHGetMalloc(LPMALLOC
*lpmal
)
234 TRACE("(%p)\n", lpmal
);
235 return CoGetMalloc(MEMCTX_TASK
, lpmal
);
238 /*************************************************************************
239 * SHAlloc [SHELL32.196]
241 * Equivalent to CoTaskMemAlloc. Under Windows 9x this function could use
242 * the shell32 built-in "mini-COM" without the need to load ole32.dll -
243 * see SHLoadOLE for details.
246 * exported by ordinal
249 * CoTaskMemAlloc, SHLoadOLE
251 LPVOID WINAPI
SHAlloc(DWORD len
)
255 ret
= CoTaskMemAlloc(len
);
256 TRACE("%u bytes at %p\n",len
, ret
);
260 /*************************************************************************
261 * SHFree [SHELL32.195]
263 * Equivalent to CoTaskMemFree. Under Windows 9x this function could use
264 * the shell32 built-in "mini-COM" without the need to load ole32.dll -
265 * see SHLoadOLE for details.
268 * exported by ordinal
271 * CoTaskMemFree, SHLoadOLE
273 void WINAPI
SHFree(LPVOID pv
)
279 /*************************************************************************
280 * SHGetDesktopFolder [SHELL32.@]
282 HRESULT WINAPI
SHGetDesktopFolder(IShellFolder
**psf
)
287 if(!psf
) return E_INVALIDARG
;
289 hres
= CDesktopFolder::_CreatorClass::CreateInstance(NULL
, IID_IShellFolder
, (void**)psf
);
291 TRACE("-- %p->(%p)\n",psf
, *psf
);
294 /**************************************************************************
295 * Default ClassFactory Implementation
297 * SHCreateDefClassObject
300 * Helper function for dlls without their own classfactory.
301 * A generic classfactory is returned.
302 * When the CreateInstance of the cf is called the callback is executed.
306 public CComObjectRootEx
<CComMultiThreadModelNoCS
>,
311 LPFNCREATEINSTANCE lpfnCI
;
313 LONG
*pcRefDll
; /* pointer to refcounter in external dll (ugrrr...) */
316 HRESULT
Initialize(LPFNCREATEINSTANCE lpfnCI
, PLONG pcRefDll
, const IID
*riidInstx
);
319 virtual HRESULT WINAPI
CreateInstance(LPUNKNOWN pUnkOuter
, REFIID riid
, LPVOID
*ppvObject
);
320 virtual HRESULT WINAPI
LockServer(BOOL fLock
);
322 BEGIN_COM_MAP(IDefClFImpl
)
323 COM_INTERFACE_ENTRY_IID(IID_IClassFactory
, IClassFactory
)
327 IDefClFImpl::IDefClFImpl()
335 HRESULT
IDefClFImpl::Initialize(LPFNCREATEINSTANCE lpfnCIx
, PLONG pcRefDllx
, const IID
*riidInstx
)
338 riidInst
= riidInstx
;
339 pcRefDll
= pcRefDllx
;
342 InterlockedIncrement(pcRefDll
);
344 TRACE("(%p)%s\n", this, shdebugstr_guid(riidInst
));
348 /******************************************************************************
349 * IDefClF_fnCreateInstance
351 HRESULT WINAPI
IDefClFImpl::CreateInstance(LPUNKNOWN pUnkOuter
, REFIID riid
, LPVOID
*ppvObject
)
353 TRACE("%p->(%p,%s,%p)\n", this, pUnkOuter
, shdebugstr_guid(&riid
), ppvObject
);
357 if (riidInst
== NULL
|| IsEqualCLSID(riid
, *riidInst
) || IsEqualCLSID(riid
, IID_IUnknown
))
359 return lpfnCI(pUnkOuter
, riid
, ppvObject
);
362 ERR("unknown IID requested %s\n", shdebugstr_guid(&riid
));
363 return E_NOINTERFACE
;
366 /******************************************************************************
367 * IDefClF_fnLockServer
369 HRESULT WINAPI
IDefClFImpl::LockServer(BOOL fLock
)
371 TRACE("%p->(0x%x), not implemented\n", this, fLock
);
375 /**************************************************************************
376 * IDefClF_fnConstructor
379 HRESULT
IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI
, PLONG pcRefDll
, const IID
*riidInst
, IClassFactory
**theFactory
)
381 CComObject
<IDefClFImpl
> *theClassObject
;
382 CComPtr
<IClassFactory
> result
;
385 if (theFactory
== NULL
)
388 ATLTRY (theClassObject
= new CComObject
<IDefClFImpl
>);
389 if (theClassObject
== NULL
)
390 return E_OUTOFMEMORY
;
391 hResult
= theClassObject
->QueryInterface (IID_IClassFactory
, (void **)&result
);
392 if (FAILED (hResult
))
394 delete theClassObject
;
397 hResult
= theClassObject
->Initialize (lpfnCI
, pcRefDll
, riidInst
);
398 if (FAILED (hResult
))
400 *theFactory
= result
.Detach ();
404 /******************************************************************************
405 * SHCreateDefClassObject [SHELL32.70]
407 HRESULT WINAPI
SHCreateDefClassObject(
410 LPFNCREATEINSTANCE lpfnCI
, /* [in] create instance callback entry */
411 LPDWORD pcRefDll
, /* [in/out] ref count of the dll */
412 REFIID riidInst
) /* [in] optional interface to the instance */
417 TRACE("%s %p %p %p %s\n", shdebugstr_guid(&riid
), ppv
, lpfnCI
, pcRefDll
, shdebugstr_guid(&riidInst
));
419 if (!IsEqualCLSID(riid
, IID_IClassFactory
))
420 return E_NOINTERFACE
;
421 hResult
= IDefClF_fnConstructor(lpfnCI
, (PLONG
)pcRefDll
, &riidInst
, &pcf
);
428 /*************************************************************************
429 * DragAcceptFiles [SHELL32.@]
431 void WINAPI
DragAcceptFiles(HWND hWnd
, BOOL b
)
435 if( !IsWindow(hWnd
) ) return;
436 exstyle
= GetWindowLongPtrA(hWnd
,GWL_EXSTYLE
);
438 exstyle
|= WS_EX_ACCEPTFILES
;
440 exstyle
&= ~WS_EX_ACCEPTFILES
;
441 SetWindowLongPtrA(hWnd
,GWL_EXSTYLE
,exstyle
);
444 /*************************************************************************
445 * DragFinish [SHELL32.@]
447 void WINAPI
DragFinish(HDROP h
)
450 GlobalFree((HGLOBAL
)h
);
453 /*************************************************************************
454 * DragQueryPoint [SHELL32.@]
456 BOOL WINAPI
DragQueryPoint(HDROP hDrop
, POINT
*p
)
458 DROPFILES
*lpDropFileStruct
;
463 lpDropFileStruct
= (DROPFILES
*) GlobalLock(hDrop
);
465 *p
= lpDropFileStruct
->pt
;
466 bRet
= lpDropFileStruct
->fNC
;
472 /*************************************************************************
473 * DragQueryFileA [SHELL32.@]
474 * DragQueryFile [SHELL32.@]
476 UINT WINAPI
DragQueryFileA(
484 DROPFILES
*lpDropFileStruct
= (DROPFILES
*) GlobalLock(hDrop
);
486 TRACE("(%p, %x, %p, %u)\n", hDrop
,lFile
,lpszFile
,lLength
);
488 if(!lpDropFileStruct
) goto end
;
490 lpDrop
= (LPSTR
) lpDropFileStruct
+ lpDropFileStruct
->pFiles
;
492 if(lpDropFileStruct
->fWide
) {
493 LPWSTR lpszFileW
= NULL
;
496 lpszFileW
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, lLength
*sizeof(WCHAR
));
497 if(lpszFileW
== NULL
) {
501 i
= DragQueryFileW(hDrop
, lFile
, lpszFileW
, lLength
);
504 WideCharToMultiByte(CP_ACP
, 0, lpszFileW
, -1, lpszFile
, lLength
, 0, NULL
);
505 HeapFree(GetProcessHeap(), 0, lpszFileW
);
512 while (*lpDrop
++); /* skip filename */
515 i
= (lFile
== 0xFFFFFFFF) ? i
: 0;
521 if (!lpszFile
) goto end
; /* needed buffer size */
522 lstrcpynA (lpszFile
, lpDrop
, lLength
);
528 /*************************************************************************
529 * DragQueryFileW [SHELL32.@]
531 UINT WINAPI
DragQueryFileW(
539 DROPFILES
*lpDropFileStruct
= (DROPFILES
*) GlobalLock(hDrop
);
541 TRACE("(%p, %x, %p, %u)\n", hDrop
,lFile
,lpszwFile
,lLength
);
543 if(!lpDropFileStruct
) goto end
;
545 lpwDrop
= (LPWSTR
) ((LPSTR
)lpDropFileStruct
+ lpDropFileStruct
->pFiles
);
547 if(lpDropFileStruct
->fWide
== FALSE
) {
548 LPSTR lpszFileA
= NULL
;
551 lpszFileA
= (LPSTR
)HeapAlloc(GetProcessHeap(), 0, lLength
);
552 if(lpszFileA
== NULL
) {
556 i
= DragQueryFileA(hDrop
, lFile
, lpszFileA
, lLength
);
559 MultiByteToWideChar(CP_ACP
, 0, lpszFileA
, -1, lpszwFile
, lLength
);
560 HeapFree(GetProcessHeap(), 0, lpszFileA
);
568 while (*lpwDrop
++); /* skip filename */
571 i
= (lFile
== 0xFFFFFFFF) ? i
: 0;
577 if ( !lpszwFile
) goto end
; /* needed buffer size */
578 lstrcpynW (lpszwFile
, lpwDrop
, lLength
);
584 /*************************************************************************
585 * SHPropStgCreate [SHELL32.685]
587 EXTERN_C HRESULT WINAPI
SHPropStgCreate(IPropertySetStorage
*psstg
, REFFMTID fmtid
,
588 const CLSID
*pclsid
, DWORD grfFlags
, DWORD grfMode
,
589 DWORD dwDisposition
, IPropertyStorage
**ppstg
, UINT
*puCodePage
)
595 TRACE("%p %s %s %x %x %x %p %p\n", psstg
, debugstr_guid(&fmtid
), debugstr_guid(pclsid
),
596 grfFlags
, grfMode
, dwDisposition
, ppstg
, puCodePage
);
598 hres
= psstg
->Open(fmtid
, grfMode
, ppstg
);
600 switch (dwDisposition
)
605 reinterpret_cast<IPropertyStorage
*>(*ppstg
)->Release();
606 hres
= psstg
->Delete(fmtid
);
615 hres
= psstg
->Create(fmtid
, pclsid
, grfFlags
, grfMode
, ppstg
);
623 prop
.ulKind
= PRSPEC_PROPID
;
624 prop
.propid
= PID_CODEPAGE
;
625 hres
= reinterpret_cast<IPropertyStorage
*>(*ppstg
)->ReadMultiple(1, &prop
, &ret
);
626 if (FAILED(hres
) || ret
.vt
!=VT_I2
)
629 *puCodePage
= ret
.iVal
;
636 /*************************************************************************
637 * SHPropStgReadMultiple [SHELL32.688]
639 EXTERN_C HRESULT WINAPI
SHPropStgReadMultiple(IPropertyStorage
*pps
, UINT uCodePage
,
640 ULONG cpspec
, const PROPSPEC
*rgpspec
, PROPVARIANT
*rgvar
)
645 FIXME("%p %u %u %p %p\n", pps
, uCodePage
, cpspec
, rgpspec
, rgvar
);
647 memset(rgvar
, 0, cpspec
*sizeof(PROPVARIANT
));
648 hres
= pps
->ReadMultiple(cpspec
, rgpspec
, rgvar
);
657 prop
.ulKind
= PRSPEC_PROPID
;
658 prop
.propid
= PID_CODEPAGE
;
659 hres
= pps
->ReadMultiple(1, &prop
, &ret
);
660 if(FAILED(hres
) || ret
.vt
!=VT_I2
)
663 uCodePage
= ret
.iVal
;
666 hres
= pps
->Stat(&stat
);
670 /* TODO: do something with codepage and stat */
674 /*************************************************************************
675 * SHPropStgWriteMultiple [SHELL32.689]
677 EXTERN_C HRESULT WINAPI
SHPropStgWriteMultiple(IPropertyStorage
*pps
, UINT
*uCodePage
,
678 ULONG cpspec
, const PROPSPEC
*rgpspec
, PROPVARIANT
*rgvar
, PROPID propidNameFirst
)
684 FIXME("%p %p %u %p %p %d\n", pps
, uCodePage
, cpspec
, rgpspec
, rgvar
, propidNameFirst
);
686 hres
= pps
->Stat(&stat
);
690 if (uCodePage
&& *uCodePage
)
691 codepage
= *uCodePage
;
697 prop
.ulKind
= PRSPEC_PROPID
;
698 prop
.propid
= PID_CODEPAGE
;
699 hres
= pps
->ReadMultiple(1, &prop
, &ret
);
702 if (ret
.vt
!=VT_I2
|| !ret
.iVal
)
707 *uCodePage
= codepage
;
710 /* TODO: do something with codepage and stat */
712 hres
= pps
->WriteMultiple(cpspec
, rgpspec
, rgvar
, propidNameFirst
);