钩的IDispatch v表在C ++中
题
我试图修改IDispatch接口已经存在于系统中的行为。 要做到这一点我的计划是在运行时期间挂接到对象v表和修改中的指针,使其指向定制的钩方法来代替。
如果我能得到这个工作,我可以添加新的方法和属性已经存在的对象。好的。
首先我试图钩住到V表的IUnknown(IDispatch的继承从中),并且工作得很好。但是想在IDispatch的变化entires根本不起作用。什么也没有发生在所有的代码工作正如它不挂钩。
下面的代码,这是非常简单,所以它不应该有任何问题,了解
#include <iostream>
#include <windows.h>
#include <Objbase.h>
#pragma comment (lib,"Ole32.lib")
using namespace std;
HRESULT __stdcall typecount(IDispatch *self,UINT*u)
{
cout << "hook" << endl;
*u=1;
return S_OK;
}
int main()
{
CoInitialize(NULL);
// Get clsid from name
CLSID clsid;
CLSIDFromProgID(L"shell.application",&clsid);
// Create instance
IDispatch *obj=NULL;
CoCreateInstance(clsid,NULL,CLSCTX_INPROC_SERVER,__uuidof(IDispatch),(void**)&obj);
// Get vtable and offset in vtable for idispatch
void* iunknown_vtable= (void*)*((unsigned int*)obj);
// There are three entries in IUnknown, therefore add 12 to go to IDispatch
void* idispatch_vtable = (void*)(((unsigned int)iunknown_vtable)+12);
// Get pointer of first emtry in IDispatch vtable (GetTypeInfoCount)
unsigned int* v1 = (unsigned int*)iunknown_vtable;
// Change memory permissions so address can be overwritten
DWORD old;
VirtualProtect(v1,4,PAGE_EXECUTE_READWRITE,&old);
// Override v-table pointer
*v1 = (unsigned int) typecount;
// Try calling GetTypeInfo count, should now be hooked. But isn't works as usual
UINT num=0;
obj->GetTypeInfoCount(&num);
/*
HRESULT hresult;
OLECHAR FAR* szMember = (OLECHAR*)L"MinimizeAll";
DISPID dispid;
DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
hresult = obj->GetIDsOfNames(IID_NULL, &szMember, 1,
LOCALE_SYSTEM_DEFAULT, &dispid) ;
hresult = obj->Invoke(dispid,IID_NULL,LOCALE_SYSTEM_DEFAULT,DISPATCH_METHOD,&dispparamsNoArgs, NULL, NULL, NULL);
*/
}
提前感谢!
解决方案
// Get pointer of first emtry in IDispatch vtable (GetTypeInfoCount)
unsigned int* v1 = (unsigned int*)iunknown_vtable;
此acturely钩住QueryInterface
的IUnknown
。
以下调用将路由到您的typecount
//obj->GetTypeInfoCount(&num);
LPVOID dummy;
obj->QueryInterface(IID_NULL, &dummy);
其他提示
我想你应该重新编码这是32位和64位之间的便携式
原件:
// There are three entries in IUnknown, therefore add 12 to go to IDispatch
void* idispatch_vtable = (void*)(((unsigned int)iunknown_vtable)+12);
便携式:
// There are three entries in IUnknown, therefore add 3 pointers to go to IDispatch
void* idispatch_vtable = (void*)(((DWORD_PTR)iunknown_vtable) + (sizeof(void *) * 3));
不隶属于 StackOverflow