📄 shellole.c
字号:
/*
* handling of SHELL32.DLL OLE-Objects
*
* Copyright 1997 Marcus Meissner
* Copyright 1998 Juergen Schmied <juergen.schmied@metronet.de>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "config.h"
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#define COBJMACROS
#include "windef.h"
#include "winbase.h"
#include "shellapi.h"
#include "wingdi.h"
#include "winuser.h"
#include "shlobj.h"
#include "shlguid.h"
#include "winreg.h"
#include "winerror.h"
#include "undocshell.h"
#include "wine/unicode.h"
#include "shell32_main.h"
#include "wine/debug.h"
#include "shlwapi.h"
#include "debughlp.h"
WINE_DEFAULT_DEBUG_CHANNEL(shell);
extern HRESULT WINAPI IFSFolder_Constructor(IUnknown * pUnkOuter, REFIID riid, LPVOID * ppv);
const WCHAR sShell32[12] = {'S','H','E','L','L','3','2','.','D','L','L','\0'};
const WCHAR sOLE32[10] = {'O','L','E','3','2','.','D','L','L','\0'};
HINSTANCE hShellOle32 = 0;
/**************************************************************************
* Default ClassFactory types
*/
typedef HRESULT (CALLBACK *LPFNCREATEINSTANCE)(IUnknown* pUnkOuter, REFIID riid, LPVOID* ppvObject);
IClassFactory * IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI, PLONG pcRefDll, REFIID riidInst);
/* this table contains all CLSID's of shell32 objects */
struct {
REFIID riid;
LPFNCREATEINSTANCE lpfnCI;
} InterfaceTable[] = {
{&CLSID_ShellFSFolder, &IFSFolder_Constructor},
{&CLSID_MyComputer, &ISF_MyComputer_Constructor},
{&CLSID_ShellDesktop, &ISF_Desktop_Constructor},
{&CLSID_ShellLink, &IShellLink_Constructor},
{&CLSID_DragDropHelper, &IDropTargetHelper_Constructor},
{&CLSID_ControlPanel, &IControlPanel_Constructor},
{&CLSID_AutoComplete, &IAutoComplete_Constructor},
{NULL,NULL}
};
/*************************************************************************
* SHCoCreateInstance [SHELL32.102]
*
* NOTES
* exported by ordinal
*/
/* FIXME: this should be SHLWAPI.24 since we can't yet import by ordinal */
DWORD WINAPI __SHGUIDToStringW (REFGUID guid, LPWSTR str)
{
WCHAR sFormat[52] = {'{','%','0','8','l','x','-','%','0','4',
'x','-','%','0','4','x','-','%','0','2',
'x','%','0','2','x','-','%','0','2','x',
'%','0','2','x','%','0','2','x','%','0',
'2','x','%','0','2','x','%','0','2','x',
'}','\0'};
return wsprintfW ( str, sFormat,
guid->Data1, guid->Data2, guid->Data3,
guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7] );
}
/************************************************************************/
HRESULT WINAPI SHCoCreateInstance(
LPCWSTR aclsid,
const CLSID *clsid,
LPUNKNOWN pUnkOuter,
REFIID refiid,
LPVOID *ppv)
{
DWORD hres;
IID iid;
CLSID * myclsid = (CLSID*)clsid;
WCHAR sKeyName[MAX_PATH];
const WCHAR sCLSID[7] = {'C','L','S','I','D','\\','\0'};
WCHAR sClassID[60];
const WCHAR sInProcServer32[16] ={'\\','I','n','p','r','o','c','S','e','r','v','e','r','3','2','\0'};
const WCHAR sLoadWithoutCOM[15] ={'L','o','a','d','W','i','t','h','o','u','t','C','O','M','\0'};
WCHAR sDllPath[MAX_PATH];
HKEY hKey;
DWORD dwSize;
BOOLEAN bLoadFromShell32 = FALSE;
BOOLEAN bLoadWithoutCOM = FALSE;
IClassFactory * pcf = NULL;
if(!ppv) return E_POINTER;
*ppv=NULL;
/* if the clsid is a string, convert it */
if (!clsid)
{
if (!aclsid) return REGDB_E_CLASSNOTREG;
SHCLSIDFromStringW(aclsid, &iid);
myclsid = &iid;
}
TRACE("(%p,%s,unk:%p,%s,%p)\n",
aclsid,shdebugstr_guid(myclsid),pUnkOuter,shdebugstr_guid(refiid),ppv);
/* we look up the dll path in the registry */
__SHGUIDToStringW(myclsid, sClassID);
lstrcpyW(sKeyName, sCLSID);
lstrcatW(sKeyName, sClassID);
lstrcatW(sKeyName, sInProcServer32);
if (ERROR_SUCCESS == RegOpenKeyExW(HKEY_CLASSES_ROOT, sKeyName, 0, KEY_READ, &hKey)) {
dwSize = sizeof(sDllPath);
SHQueryValueExW(hKey, NULL, 0,0, sDllPath, &dwSize );
/* if a special registry key is set, we load a shell extension without help of OLE32 */
bLoadWithoutCOM = (ERROR_SUCCESS == SHQueryValueExW(hKey, sLoadWithoutCOM, 0, 0, 0, 0));
/* if the com object is inside shell32, omit use of ole32 */
bLoadFromShell32 = (0==lstrcmpiW( PathFindFileNameW(sDllPath), sShell32));
RegCloseKey (hKey);
} else {
/* since we can't find it in the registry we try internally */
bLoadFromShell32 = TRUE;
}
TRACE("WithoutCom=%u FromShell=%u\n", bLoadWithoutCOM, bLoadFromShell32);
/* now we create an instance */
if (bLoadFromShell32) {
if (! SUCCEEDED(DllGetClassObject(myclsid, &IID_IClassFactory,(LPVOID*)&pcf))) {
ERR("LoadFromShell failed for CLSID=%s\n", shdebugstr_guid(myclsid));
}
} else if (bLoadWithoutCOM) {
/* load an external dll without ole32 */
HANDLE hLibrary;
typedef HRESULT (CALLBACK *DllGetClassObjectFunc)(REFCLSID clsid, REFIID iid, LPVOID *ppv);
DllGetClassObjectFunc DllGetClassObject;
if ((hLibrary = LoadLibraryExW(sDllPath, 0, LOAD_WITH_ALTERED_SEARCH_PATH)) == 0) {
ERR("couldn't load InprocServer32 dll %s\n", debugstr_w(sDllPath));
hres = E_ACCESSDENIED;
goto end;
} else if (!(DllGetClassObject = (DllGetClassObjectFunc)GetProcAddress(hLibrary, "DllGetClassObject"))) {
ERR("couldn't find function DllGetClassObject in %s\n", debugstr_w(sDllPath));
FreeLibrary( hLibrary );
hres = E_ACCESSDENIED;
goto end;
} else if (! SUCCEEDED(hres = DllGetClassObject(myclsid, &IID_IClassFactory, (LPVOID*)&pcf))) {
TRACE("GetClassObject failed 0x%08lx\n", hres);
goto end;
}
} else {
/* load an external dll in the usual way */
hres = CoCreateInstance(myclsid, pUnkOuter, CLSCTX_INPROC_SERVER, refiid, ppv);
goto end;
}
/* here we should have a ClassFactory */
if (!pcf) return E_ACCESSDENIED;
hres = IClassFactory_CreateInstance(pcf, pUnkOuter, refiid, ppv);
IClassFactory_Release(pcf);
end:
if(hres!=S_OK)
{
ERR("failed (0x%08lx) to create CLSID:%s IID:%s\n",
hres, shdebugstr_guid(myclsid), shdebugstr_guid(refiid));
ERR("class not found in registry\n");
}
TRACE("-- instance: %p\n",*ppv);
return hres;
}
/*************************************************************************
* DllGetClassObject [SHELL32.@]
*/
HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
{
HRESULT hres = E_OUTOFMEMORY;
IClassFactory * pcf = NULL;
int i;
TRACE("CLSID:%s,IID:%s\n",shdebugstr_guid(rclsid),shdebugstr_guid(iid));
if (!ppv) return E_INVALIDARG;
*ppv = NULL;
/* search our internal interface table */
for(i=0;InterfaceTable[i].riid;i++) {
if(IsEqualIID(InterfaceTable[i].riid, rclsid)) {
TRACE("index[%u]\n", i);
pcf = IDefClF_fnConstructor(InterfaceTable[i].lpfnCI, NULL, NULL);
}
}
if (!pcf) {
FIXME("failed for CLSID=%s\n", shdebugstr_guid(rclsid));
return CLASS_E_CLASSNOTAVAILABLE;
}
hres = IClassFactory_QueryInterface(pcf, iid, ppv);
IClassFactory_Release(pcf);
TRACE("-- pointer to class factory: %p\n",*ppv);
return hres;
}
/*************************************************************************
* SHCLSIDFromString [SHELL32.147]
*
* NOTES
* exported by ordinal
*/
DWORD WINAPI SHCLSIDFromStringA (LPCSTR clsid, CLSID *id)
{
WCHAR buffer[40];
TRACE("(%p(%s) %p)\n", clsid, clsid, id);
if (!MultiByteToWideChar( CP_ACP, 0, clsid, -1, buffer, sizeof(buffer)/sizeof(WCHAR) ))
return CO_E_CLASSSTRING;
return CLSIDFromString( buffer, id );
}
DWORD WINAPI SHCLSIDFromStringW (LPCWSTR clsid, CLSID *id)
{
TRACE("(%p(%s) %p)\n", clsid, debugstr_w(clsid), id);
return CLSIDFromString((LPWSTR)clsid, id);
}
DWORD WINAPI SHCLSIDFromStringAW (LPVOID clsid, CLSID *id)
{
if (SHELL_OsIsUnicode())
return SHCLSIDFromStringW (clsid, id);
return SHCLSIDFromStringA (clsid, id);
}
/*************************************************************************
* Shell Memory Allocator
*/
/* set the vtable later */
static const IMallocVtbl VT_Shell_IMalloc32;
/* this is the static object instance */
typedef struct {
const IMallocVtbl *lpVtbl;
DWORD dummy;
} _ShellMalloc;
static _ShellMalloc Shell_Malloc = { &VT_Shell_IMalloc32,1};
/* this is the global allocator of shell32 */
static IMalloc * ShellTaskAllocator = NULL;
/******************************************************************************
* IShellMalloc_QueryInterface [VTABLE]
*/
static HRESULT WINAPI IShellMalloc_fnQueryInterface(LPMALLOC iface, REFIID refiid, LPVOID *obj)
{
TRACE("(%s,%p)\n",shdebugstr_guid(refiid),obj);
if (IsEqualIID(refiid, &IID_IUnknown) || IsEqualIID(refiid, &IID_IMalloc)) {
*obj = (LPMALLOC) &Shell_Malloc;
return S_OK;
}
return E_NOINTERFACE;
}
/******************************************************************************
* IShellMalloc_AddRefRelease [VTABLE]
*/
static ULONG WINAPI IShellMalloc_fnAddRefRelease(LPMALLOC iface)
{
return 1;
}
/******************************************************************************
* IShellMalloc_Alloc [VTABLE]
*/
static LPVOID WINAPI IShellMalloc_fnAlloc(LPMALLOC iface, DWORD cb)
{
LPVOID addr;
addr = (LPVOID) LocalAlloc(LMEM_ZEROINIT, cb);
TRACE("(%p,%ld);\n",addr,cb);
return addr;
}
/******************************************************************************
* IShellMalloc_Realloc [VTABLE]
*/
static LPVOID WINAPI IShellMalloc_fnRealloc(LPMALLOC iface, LPVOID pv, DWORD cb)
{
LPVOID addr;
if (pv) {
if (cb) {
addr = (LPVOID) LocalReAlloc((HANDLE) pv, cb, LMEM_ZEROINIT | LMEM_MOVEABLE);
} else {
LocalFree((HANDLE) pv);
addr = NULL;
}
} else {
if (cb) {
addr = (LPVOID) LocalAlloc(LMEM_ZEROINIT, cb);
} else {
addr = NULL;
}
}
TRACE("(%p->%p,%ld)\n",pv,addr,cb);
return addr;
}
/******************************************************************************
* IShellMalloc_Free [VTABLE]
*/
static VOID WINAPI IShellMalloc_fnFree(LPMALLOC iface, LPVOID pv)
{
TRACE("(%p)\n",pv);
LocalFree((HANDLE) pv);
}
/******************************************************************************
* IShellMalloc_GetSize [VTABLE]
*/
static DWORD WINAPI IShellMalloc_fnGetSize(LPMALLOC iface, LPVOID pv)
{
DWORD cb = (DWORD) LocalSize((HANDLE)pv);
TRACE("(%p,%ld)\n", pv, cb);
return cb;
}
/******************************************************************************
* IShellMalloc_DidAlloc [VTABLE]
*/
static INT WINAPI IShellMalloc_fnDidAlloc(LPMALLOC iface, LPVOID pv)
{
TRACE("(%p)\n",pv);
return -1;
}
/******************************************************************************
* IShellMalloc_HeapMinimize [VTABLE]
*/
static VOID WINAPI IShellMalloc_fnHeapMinimize(LPMALLOC iface)
{
TRACE("()\n");
}
static const IMallocVtbl VT_Shell_IMalloc32 =
{
IShellMalloc_fnQueryInterface,
IShellMalloc_fnAddRefRelease,
IShellMalloc_fnAddRefRelease,
IShellMalloc_fnAlloc,
IShellMalloc_fnRealloc,
IShellMalloc_fnFree,
IShellMalloc_fnGetSize,
IShellMalloc_fnDidAlloc,
IShellMalloc_fnHeapMinimize
};
/*************************************************************************
* SHGetMalloc [SHELL32.@]
*
* Return the shell IMalloc interface.
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -