EDIT:
I am attempting to encapsulate a 3rd party com object in .net classes and at the same time fix a bug with it. I was hoping to replace a method in the com object's vtable with a call to my own method which would provide a bug free implementation of the method.
I was trying to test this out on something very basic, but given the answers below i'm on completely the wrong track. By creating a reference to my com object I presume i've created a runtime callable wrapper, is there any way to get a pointer to the real underlying com object?
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();
}