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 static const WCHAR sShell32
[12] = {'S','H','E','L','L','3','2','.','D','L','L','\0'};
28 /**************************************************************************
29 * Default ClassFactory types
31 typedef HRESULT (CALLBACK
*LPFNCREATEINSTANCE
)(IUnknown
* pUnkOuter
, REFIID riid
, LPVOID
* ppvObject
);
32 HRESULT
IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI
, PLONG pcRefDll
, const IID
*riidInst
, IClassFactory
**theFactory
);
34 /* FIXME: this should be SHLWAPI.24 since we can't yet import by ordinal */
36 DWORD WINAPI
__SHGUIDToStringW (REFGUID guid
, LPWSTR str
)
38 WCHAR sFormat
[52] = {'{','%','0','8','l','x','-','%','0','4',
39 'x','-','%','0','4','x','-','%','0','2',
40 'x','%','0','2','x','-','%','0','2','x',
41 '%','0','2','x','%','0','2','x','%','0',
42 '2','x','%','0','2','x','%','0','2','x',
45 return swprintf ( str
, sFormat
,
46 guid
.Data1
, guid
.Data2
, guid
.Data3
,
47 guid
.Data4
[0], guid
.Data4
[1], guid
.Data4
[2], guid
.Data4
[3],
48 guid
.Data4
[4], guid
.Data4
[5], guid
.Data4
[6], guid
.Data4
[7] );
52 /*************************************************************************
53 * SHCoCreateInstance [SHELL32.102]
55 * Equivalent to CoCreateInstance. Under Windows 9x this function could sometimes
56 * use the shell32 built-in "mini-COM" without the need to load ole32.dll - see
57 * SHLoadOLE for details.
59 * Under wine if a "LoadWithoutCOM" value is present or the object resides in
60 * shell32.dll the function will load the object manually without the help of ole32
66 * CoCreateInstace, SHLoadOLE
68 HRESULT WINAPI
SHCoCreateInstance(
77 const CLSID
* myclsid
= clsid
;
78 WCHAR sKeyName
[MAX_PATH
];
79 const WCHAR sCLSID
[7] = {'C','L','S','I','D','\\','\0'};
81 const WCHAR sInProcServer32
[16] ={'\\','I','n','p','r','o','c','S','e','r','v','e','r','3','2','\0'};
82 const WCHAR sLoadWithoutCOM
[15] ={'L','o','a','d','W','i','t','h','o','u','t','C','O','M','\0'};
83 WCHAR sDllPath
[MAX_PATH
];
86 BOOLEAN bLoadFromShell32
= FALSE
;
87 BOOLEAN bLoadWithoutCOM
= FALSE
;
88 CComPtr
<IClassFactory
> pcf
;
90 if(!ppv
) return E_POINTER
;
93 /* if the clsid is a string, convert it */
96 if (!aclsid
) return REGDB_E_CLASSNOTREG
;
97 CLSIDFromString((LPOLESTR
)aclsid
, &iid
);
101 TRACE("(%p,%s,unk:%p,%s,%p)\n",
102 aclsid
, shdebugstr_guid(myclsid
), pUnkOuter
, shdebugstr_guid(&refiid
), ppv
);
104 /* we look up the dll path in the registry */
105 __SHGUIDToStringW(*myclsid
, sClassID
);
106 wcscpy(sKeyName
, sCLSID
);
107 wcscat(sKeyName
, sClassID
);
108 wcscat(sKeyName
, sInProcServer32
);
110 if (ERROR_SUCCESS
== RegOpenKeyExW(HKEY_CLASSES_ROOT
, sKeyName
, 0, KEY_READ
, &hKey
)) {
111 dwSize
= sizeof(sDllPath
);
112 SHQueryValueExW(hKey
, NULL
, 0,0, sDllPath
, &dwSize
);
114 /* if a special registry key is set, we load a shell extension without help of OLE32 */
115 bLoadWithoutCOM
= (ERROR_SUCCESS
== SHQueryValueExW(hKey
, sLoadWithoutCOM
, 0, 0, 0, 0));
117 /* if the com object is inside shell32, omit use of ole32 */
118 bLoadFromShell32
= (0==lstrcmpiW( PathFindFileNameW(sDllPath
), sShell32
));
122 /* since we can't find it in the registry we try internally */
123 bLoadFromShell32
= TRUE
;
126 TRACE("WithoutCom=%u FromShell=%u\n", bLoadWithoutCOM
, bLoadFromShell32
);
128 /* now we create an instance */
129 if (bLoadFromShell32
) {
130 if (! SUCCEEDED(DllGetClassObject(*myclsid
, IID_PPV_ARG(IClassFactory
, &pcf
)))) {
131 ERR("LoadFromShell failed for CLSID=%s\n", shdebugstr_guid(myclsid
));
133 } else if (bLoadWithoutCOM
) {
135 /* load an external dll without ole32 */
137 typedef HRESULT (CALLBACK
*DllGetClassObjectFunc
)(REFCLSID clsid
, REFIID iid
, LPVOID
*ppv
);
138 DllGetClassObjectFunc DllGetClassObject
;
140 if ((hLibrary
= LoadLibraryExW(sDllPath
, 0, LOAD_WITH_ALTERED_SEARCH_PATH
)) == 0) {
141 ERR("couldn't load InprocServer32 dll %s\n", debugstr_w(sDllPath
));
142 hres
= E_ACCESSDENIED
;
144 } else if (!(DllGetClassObject
= (DllGetClassObjectFunc
)GetProcAddress(hLibrary
, "DllGetClassObject"))) {
145 ERR("couldn't find function DllGetClassObject in %s\n", debugstr_w(sDllPath
));
146 FreeLibrary( hLibrary
);
147 hres
= E_ACCESSDENIED
;
149 } else if (! SUCCEEDED(hres
= DllGetClassObject(*myclsid
, IID_PPV_ARG(IClassFactory
, &pcf
)))) {
150 TRACE("GetClassObject failed 0x%08x\n", hres
);
156 /* load an external dll in the usual way */
157 hres
= CoCreateInstance(*myclsid
, pUnkOuter
, CLSCTX_INPROC_SERVER
, refiid
, ppv
);
161 /* here we should have a ClassFactory */
162 if (!pcf
) return E_ACCESSDENIED
;
164 hres
= pcf
->CreateInstance(pUnkOuter
, refiid
, ppv
);
168 ERR("failed (0x%08x) to create CLSID:%s IID:%s\n",
169 hres
, shdebugstr_guid(myclsid
), shdebugstr_guid(&refiid
));
170 ERR("class not found in registry\n");
173 TRACE("-- instance: %p\n",*ppv
);
177 /*************************************************************************
178 * SHCLSIDFromString [SHELL32.147]
180 * Under Windows 9x this was an ANSI version of CLSIDFromString. It also allowed
181 * to avoid dependency on ole32.dll (see SHLoadOLE for details).
183 * Under Windows NT/2000/XP this is equivalent to CLSIDFromString
186 * exported by ordinal
189 * CLSIDFromString, SHLoadOLE
191 DWORD WINAPI
SHCLSIDFromStringA (LPCSTR clsid
, CLSID
*id
)
194 TRACE("(%p(%s) %p)\n", clsid
, clsid
, id
);
195 if (!MultiByteToWideChar( CP_ACP
, 0, clsid
, -1, buffer
, sizeof(buffer
)/sizeof(WCHAR
) ))
196 return CO_E_CLASSSTRING
;
197 return CLSIDFromString( buffer
, id
);
200 DWORD WINAPI
SHCLSIDFromStringW (LPCWSTR clsid
, CLSID
*id
)
202 TRACE("(%p(%s) %p)\n", clsid
, debugstr_w(clsid
), id
);
203 return CLSIDFromString((LPWSTR
)clsid
, id
);
206 EXTERN_C DWORD WINAPI
SHCLSIDFromStringAW (LPCVOID clsid
, CLSID
*id
)
208 if (SHELL_OsIsUnicode())
209 return SHCLSIDFromStringW ((LPCWSTR
)clsid
, id
);
210 return SHCLSIDFromStringA ((LPCSTR
)clsid
, id
);
213 /*************************************************************************
214 * SHGetMalloc [SHELL32.@]
216 * Equivalent to CoGetMalloc(MEMCTX_TASK, ...). Under Windows 9x this function
217 * could use the shell32 built-in "mini-COM" without the need to load ole32.dll -
218 * see SHLoadOLE for details.
221 * lpmal [O] Destination for IMalloc interface.
224 * Success: S_OK. lpmal contains the shells IMalloc interface.
225 * Failure. An HRESULT error code.
228 * CoGetMalloc, SHLoadOLE
230 HRESULT WINAPI
SHGetMalloc(LPMALLOC
*lpmal
)
232 TRACE("(%p)\n", lpmal
);
233 return CoGetMalloc(MEMCTX_TASK
, lpmal
);
236 /*************************************************************************
237 * SHAlloc [SHELL32.196]
239 * Equivalent to CoTaskMemAlloc. Under Windows 9x this function could use
240 * the shell32 built-in "mini-COM" without the need to load ole32.dll -
241 * see SHLoadOLE for details.
244 * exported by ordinal
247 * CoTaskMemAlloc, SHLoadOLE
249 LPVOID WINAPI
SHAlloc(SIZE_T len
)
253 ret
= CoTaskMemAlloc(len
);
254 TRACE("%u bytes at %p\n",len
, ret
);
258 /*************************************************************************
259 * SHFree [SHELL32.195]
261 * Equivalent to CoTaskMemFree. Under Windows 9x this function could use
262 * the shell32 built-in "mini-COM" without the need to load ole32.dll -
263 * see SHLoadOLE for details.
266 * exported by ordinal
269 * CoTaskMemFree, SHLoadOLE
271 void WINAPI
SHFree(LPVOID pv
)
277 /*************************************************************************
278 * SHGetDesktopFolder [SHELL32.@]
280 HRESULT WINAPI
SHGetDesktopFolder(IShellFolder
**psf
)
285 if(!psf
) return E_INVALIDARG
;
287 hres
= CDesktopFolder::_CreatorClass::CreateInstance(NULL
, IID_PPV_ARG(IShellFolder
, psf
));
289 TRACE("-- %p->(%p)\n",psf
, *psf
);
292 /**************************************************************************
293 * Default ClassFactory Implementation
295 * SHCreateDefClassObject
298 * Helper function for dlls without their own classfactory.
299 * A generic classfactory is returned.
300 * When the CreateInstance of the cf is called the callback is executed.
304 public CComObjectRootEx
<CComMultiThreadModelNoCS
>,
309 LPFNCREATEINSTANCE lpfnCI
;
311 LONG
*pcRefDll
; /* pointer to refcounter in external dll (ugrrr...) */
314 HRESULT
Initialize(LPFNCREATEINSTANCE lpfnCI
, PLONG pcRefDll
, const IID
*riidInstx
);
317 virtual HRESULT WINAPI
CreateInstance(IUnknown
* pUnkOuter
, REFIID riid
, LPVOID
*ppvObject
);
318 virtual HRESULT WINAPI
LockServer(BOOL fLock
);
320 BEGIN_COM_MAP(IDefClFImpl
)
321 COM_INTERFACE_ENTRY_IID(IID_IClassFactory
, IClassFactory
)
325 IDefClFImpl::IDefClFImpl()
333 HRESULT
IDefClFImpl::Initialize(LPFNCREATEINSTANCE lpfnCIx
, PLONG pcRefDllx
, const IID
*riidInstx
)
336 riidInst
= riidInstx
;
337 pcRefDll
= pcRefDllx
;
340 InterlockedIncrement(pcRefDll
);
342 TRACE("(%p)%s\n", this, shdebugstr_guid(riidInst
));
346 /******************************************************************************
347 * IDefClF_fnCreateInstance
349 HRESULT WINAPI
IDefClFImpl::CreateInstance(IUnknown
* pUnkOuter
, REFIID riid
, LPVOID
*ppvObject
)
351 TRACE("%p->(%p,%s,%p)\n", this, pUnkOuter
, shdebugstr_guid(&riid
), ppvObject
);
355 if (riidInst
== NULL
|| IsEqualCLSID(riid
, *riidInst
) || IsEqualCLSID(riid
, IID_IUnknown
))
357 return lpfnCI(pUnkOuter
, riid
, ppvObject
);
360 ERR("unknown IID requested %s\n", shdebugstr_guid(&riid
));
361 return E_NOINTERFACE
;
364 /******************************************************************************
365 * IDefClF_fnLockServer
367 HRESULT WINAPI
IDefClFImpl::LockServer(BOOL fLock
)
369 TRACE("%p->(0x%x), not implemented\n", this, fLock
);
373 /**************************************************************************
374 * IDefClF_fnConstructor
377 HRESULT
IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI
, PLONG pcRefDll
, const IID
*riidInst
, IClassFactory
**theFactory
)
379 return ShellObjectCreatorInit
<IDefClFImpl
>(lpfnCI
, pcRefDll
, riidInst
, IID_IClassFactory
, theFactory
);
382 /******************************************************************************
383 * SHCreateDefClassObject [SHELL32.70]
385 HRESULT WINAPI
SHCreateDefClassObject(
388 LPFNCREATEINSTANCE lpfnCI
, /* [in] create instance callback entry */
389 LPDWORD pcRefDll
, /* [in/out] ref count of the dll */
390 REFIID riidInst
) /* [in] optional interface to the instance */
395 TRACE("%s %p %p %p %s\n", shdebugstr_guid(&riid
), ppv
, lpfnCI
, pcRefDll
, shdebugstr_guid(&riidInst
));
397 if (!IsEqualCLSID(riid
, IID_IClassFactory
))
398 return E_NOINTERFACE
;
399 hResult
= IDefClF_fnConstructor(lpfnCI
, (PLONG
)pcRefDll
, &riidInst
, &pcf
);
406 /*************************************************************************
407 * DragAcceptFiles [SHELL32.@]
409 void WINAPI
DragAcceptFiles(HWND hWnd
, BOOL b
)
413 if( !IsWindow(hWnd
) ) return;
414 exstyle
= GetWindowLongPtrA(hWnd
,GWL_EXSTYLE
);
416 exstyle
|= WS_EX_ACCEPTFILES
;
418 exstyle
&= ~WS_EX_ACCEPTFILES
;
419 SetWindowLongPtrA(hWnd
,GWL_EXSTYLE
,exstyle
);
422 /*************************************************************************
423 * DragFinish [SHELL32.@]
425 void WINAPI
DragFinish(HDROP h
)
428 GlobalFree((HGLOBAL
)h
);
431 /*************************************************************************
432 * DragQueryPoint [SHELL32.@]
434 BOOL WINAPI
DragQueryPoint(HDROP hDrop
, POINT
*p
)
436 DROPFILES
*lpDropFileStruct
;
441 lpDropFileStruct
= (DROPFILES
*) GlobalLock(hDrop
);
443 *p
= lpDropFileStruct
->pt
;
444 bRet
= lpDropFileStruct
->fNC
;
450 /*************************************************************************
451 * DragQueryFileA [SHELL32.@]
452 * DragQueryFile [SHELL32.@]
454 UINT WINAPI
DragQueryFileA(
462 DROPFILES
*lpDropFileStruct
= (DROPFILES
*) GlobalLock(hDrop
);
464 TRACE("(%p, %x, %p, %u)\n", hDrop
,lFile
,lpszFile
,lLength
);
466 if(!lpDropFileStruct
) goto end
;
468 lpDrop
= (LPSTR
) lpDropFileStruct
+ lpDropFileStruct
->pFiles
;
470 if(lpDropFileStruct
->fWide
) {
471 LPWSTR lpszFileW
= NULL
;
474 lpszFileW
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, lLength
*sizeof(WCHAR
));
475 if(lpszFileW
== NULL
) {
479 i
= DragQueryFileW(hDrop
, lFile
, lpszFileW
, lLength
);
482 WideCharToMultiByte(CP_ACP
, 0, lpszFileW
, -1, lpszFile
, lLength
, 0, NULL
);
483 HeapFree(GetProcessHeap(), 0, lpszFileW
);
490 while (*lpDrop
++); /* skip filename */
493 i
= (lFile
== 0xFFFFFFFF) ? i
: 0;
499 if (!lpszFile
) goto end
; /* needed buffer size */
500 lstrcpynA (lpszFile
, lpDrop
, lLength
);
506 /*************************************************************************
507 * DragQueryFileW [SHELL32.@]
509 UINT WINAPI
DragQueryFileW(
517 DROPFILES
*lpDropFileStruct
= (DROPFILES
*) GlobalLock(hDrop
);
519 TRACE("(%p, %x, %p, %u)\n", hDrop
,lFile
,lpszwFile
,lLength
);
521 if(!lpDropFileStruct
) goto end
;
523 lpwDrop
= (LPWSTR
) ((LPSTR
)lpDropFileStruct
+ lpDropFileStruct
->pFiles
);
525 if(lpDropFileStruct
->fWide
== FALSE
) {
526 LPSTR lpszFileA
= NULL
;
529 lpszFileA
= (LPSTR
)HeapAlloc(GetProcessHeap(), 0, lLength
);
530 if(lpszFileA
== NULL
) {
534 i
= DragQueryFileA(hDrop
, lFile
, lpszFileA
, lLength
);
537 MultiByteToWideChar(CP_ACP
, 0, lpszFileA
, -1, lpszwFile
, lLength
);
538 HeapFree(GetProcessHeap(), 0, lpszFileA
);
546 while (*lpwDrop
++); /* skip filename */
549 i
= (lFile
== 0xFFFFFFFF) ? i
: 0;
555 if ( !lpszwFile
) goto end
; /* needed buffer size */
556 lstrcpynW (lpszwFile
, lpwDrop
, lLength
);
562 /*************************************************************************
563 * SHPropStgCreate [SHELL32.685]
565 EXTERN_C HRESULT WINAPI
SHPropStgCreate(IPropertySetStorage
*psstg
, REFFMTID fmtid
,
566 const CLSID
*pclsid
, DWORD grfFlags
, DWORD grfMode
,
567 DWORD dwDisposition
, IPropertyStorage
**ppstg
, UINT
*puCodePage
)
573 TRACE("%p %s %s %x %x %x %p %p\n", psstg
, debugstr_guid(&fmtid
), debugstr_guid(pclsid
),
574 grfFlags
, grfMode
, dwDisposition
, ppstg
, puCodePage
);
576 hres
= psstg
->Open(fmtid
, grfMode
, ppstg
);
578 switch (dwDisposition
)
584 hres
= psstg
->Delete(fmtid
);
593 hres
= psstg
->Create(fmtid
, pclsid
, grfFlags
, grfMode
, ppstg
);
601 prop
.ulKind
= PRSPEC_PROPID
;
602 prop
.propid
= PID_CODEPAGE
;
603 hres
= (*ppstg
)->ReadMultiple(1, &prop
, &ret
);
604 if (FAILED(hres
) || ret
.vt
!=VT_I2
)
607 *puCodePage
= ret
.iVal
;
614 /*************************************************************************
615 * SHPropStgReadMultiple [SHELL32.688]
617 EXTERN_C HRESULT WINAPI
SHPropStgReadMultiple(IPropertyStorage
*pps
, UINT uCodePage
,
618 ULONG cpspec
, const PROPSPEC
*rgpspec
, PROPVARIANT
*rgvar
)
623 FIXME("%p %u %u %p %p\n", pps
, uCodePage
, cpspec
, rgpspec
, rgvar
);
625 memset(rgvar
, 0, cpspec
*sizeof(PROPVARIANT
));
626 hres
= pps
->ReadMultiple(cpspec
, rgpspec
, rgvar
);
635 prop
.ulKind
= PRSPEC_PROPID
;
636 prop
.propid
= PID_CODEPAGE
;
637 hres
= pps
->ReadMultiple(1, &prop
, &ret
);
638 if(FAILED(hres
) || ret
.vt
!=VT_I2
)
641 uCodePage
= ret
.iVal
;
644 hres
= pps
->Stat(&stat
);
648 /* TODO: do something with codepage and stat */
652 /*************************************************************************
653 * SHPropStgWriteMultiple [SHELL32.689]
655 EXTERN_C HRESULT WINAPI
SHPropStgWriteMultiple(IPropertyStorage
*pps
, UINT
*uCodePage
,
656 ULONG cpspec
, const PROPSPEC
*rgpspec
, PROPVARIANT
*rgvar
, PROPID propidNameFirst
)
662 FIXME("%p %p %u %p %p %d\n", pps
, uCodePage
, cpspec
, rgpspec
, rgvar
, propidNameFirst
);
664 hres
= pps
->Stat(&stat
);
668 if (uCodePage
&& *uCodePage
)
669 codepage
= *uCodePage
;
675 prop
.ulKind
= PRSPEC_PROPID
;
676 prop
.propid
= PID_CODEPAGE
;
677 hres
= pps
->ReadMultiple(1, &prop
, &ret
);
680 if (ret
.vt
!=VT_I2
|| !ret
.iVal
)
685 *uCodePage
= codepage
;
688 /* TODO: do something with codepage and stat */
690 hres
= pps
->WriteMultiple(cpspec
, rgpspec
, rgvar
, propidNameFirst
);
694 /*************************************************************************
695 * SHCreateQueryCancelAutoPlayMoniker [SHELL32.@]
697 HRESULT WINAPI
SHCreateQueryCancelAutoPlayMoniker(IMoniker
**moniker
)
699 TRACE("%p\n", moniker
);
701 if (!moniker
) return E_INVALIDARG
;
702 return CreateClassMoniker(CLSID_QueryCancelAutoPlay
, moniker
);