Gancio IDispatch v-tavolo in C ++
Domanda
Sto cercando di modificare il comportamento di un'interfaccia IDispatch già presente nel sistema. Per fare questo il mio piano era quello di collegare in oggetti v-tavolo durante il runtime e modificare i puntatori in modo che punti a un metodo personalizzato invece gancio.
Se riesco a far funzionare tutto questo posso aggiungere nuovi metodi e proprietà agli oggetti già esistenti. Nizza.
cercai aggancio nel v-tavolo per IUnknown (da cui IDispatch eredita da) e che funzionava bene. Tuttavia cercando di cambiare interi a IDispatch non funziona affatto. Nulla accade a tutti, il codice funziona proprio come ha fatto senza il gancio.
Ecco il codice, è molto semplice, quindi non dovrebbe essere alcun problema per capire
#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);
*/
}
Grazie in anticipo!
Soluzione
// Get pointer of first emtry in IDispatch vtable (GetTypeInfoCount)
unsigned int* v1 = (unsigned int*)iunknown_vtable;
questo acturely agganciato il QueryInterface
di IUnknown
.
A seguito di invocare percorso volontà di tuo typecount
//obj->GetTypeInfoCount(&num);
LPVOID dummy;
obj->QueryInterface(IID_NULL, &dummy);
Altri suggerimenti
Credo che si dovrebbe ricodificare questo per essere portabile tra 32 bit e 64 bit
originale:
// There are three entries in IUnknown, therefore add 12 to go to IDispatch
void* idispatch_vtable = (void*)(((unsigned int)iunknown_vtable)+12);
Portatile:
// 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));