Accessing a com object's vtable in c#
Posted
by Martin Booth
on Stack Overflow
See other posts from Stack Overflow
or by Martin Booth
Published on 2010-04-22T09:11:43Z
Indexed on
2010/04/22
9:13 UTC
Read the original article
Hit count: 479
I'm attempting to access a com object's vtable in memory and getting nowhere.
From what I have read, the first 4 bytes in memory of a com object should be a pointer to the vtableptr, which in turn is a pointer to the vtable.
Although I'm not sure I expect to find my test method at slot 7 in this com object I have tried them all and nothing looks like the address of my function.
If anyone can make the necessary changes to this sample to get it to work (the aim is just to try and invoke the Test method on the Tester class by accessing the com object's vtable and locating the method in whichever row it turns up) I would be very grateful.
I know it is rare that anyone would need to do this, but please accept I do need to do something quite similar and have considered the alternatives
Thanks in advance
[ComVisible(true)]
public class Tester
{
public void Test()
{
MessageBox.Show("Here");
}
}
public delegate void TestDelegate();
private void main()
{
Tester t = new Tester();
GCHandle objectHandle = GCHandle.Alloc(t);
IntPtr objectPtr = GCHandle.ToIntPtr(objectHandle);
IntPtr vTable = (IntPtr)Marshal.ReadInt32(objectPtr);
IntPtr vTablePtr = (IntPtr)Marshal.ReadInt32(vTable);
IntPtr functionPtr = (IntPtr)Marshal.ReadInt32(vTablePtr, 7 * 4); // 7 may be incorrect but i have tried many different values
TestDelegate func = (TestDelegate)Marshal.GetDelegateForFunctionPointer(functionPtr, typeof(TestDelegate));
func();
objectHandle.Free();
}
© Stack Overflow or respective owner