   Could someone with msvc tell me if this code works?
  I'm making some assumptions about the way classes are put together by the compiler. The following code works with MinGW. Could someone try it and tell me if it works with MSVC++?It should output something similar to the following:pointer=0x960570executing the right way:derived::func1 invoked. this=0x960570, arg=deadbeefderived::func2 invoked. this=0x960570, arg=deadbeefreplacing the vtable entry for func2 with a hack with replacement_function.executing the right way:derived::func1 invoked. this=0x960570, arg=deadbeefreplacement_function invoked. _this=0x960570, arg=deadbeefexecuting with a hack:derived::func1 invoked. this=0x960570, arg=deadbeefreplacement_function invoked. _this=0x960570, arg=deadbeef#include <iostream>// the abstract base classclass base {  public:   virtual void func1(unsigned int arg) = 0;   virtual void func2(unsigned int arg) = 0; };// a class derived from base class, which has the functions we want to execute.class derived: public base {  public:virtual void func1(unsigned int arg)    {std::cout << "derived::func1 invoked. this=" << this << ", arg=" << arg << std::endl;}   virtual void func2(unsigned int arg)    {std::cout << "derived::func2 invoked. this=" << this << ", arg=" << arg << std::endl;} }; // This is a guess as to how the object will be layed out by the compiler.typedef void (*func)(base *, unsigned int);struct vtable {func func1, func2;};struct fake_base {vtable *vtbl;};// We should be able to cast a base* to a fake_base* and get to the virtual functions with it.// We're later going to inject a pointer to this into the object's vtable.void replacement_function(base *_this, unsigned int arg) {  std::cout << "replacement_function invoked. _this=" << _this << ", arg=" << arg << std::endl; }int main() {  // Get an object.  base *ptr = new derived;    // An alias we will use for the vtable hacking.  fake_base *ptr2 = (fake_base *)ptr;    // Hex is good.  std::cout << std::hex;  // Where is the pointer? This should end up being the this pointer printed in every function call.  std::cout << "pointer=" << ptr << std::endl;    std::cout << std::endl << "executing the right way: " << std::endl;  // Execute the functions the way god intended.  ptr->func1(0xdeadbeef);  ptr->func2(0xdeadbeef);    std::cout << std::endl << "replacing the vtable entry for func2 with a hack with replacement_function." << std::endl;    // This is a perfect example of something you should never do.  ptr2->vtbl->func2 = &replacement_function;    std::cout << std::endl << "executing the right way: " << std::endl;    // Again, executing the functions the right way. ptr->func2 should execute  // replacement_function instead of derived::func2 like it is supposed to.  ptr->func1(0xdeadbeef);  ptr->func2(0xdeadbeef);    std::cout << std::endl << "executing using a hack: " << std::endl;  // Here we execute the functions manually. The results should be the  // same as they were for executing the right way.  (*(ptr2->vtbl->func1))(ptr, 0xdeadbeef);  (*(ptr2->vtbl->func2))(ptr, 0xdeadbeef);    // Delete the pointer and we're done.  delete ptr;    // Wait for some input before quitting.  std::cin.get(); }[edited by - smart_idiot on August 4, 2003 8:31:43 PM]
  it compiles but doesn''t run under version 6.0.   My HomepageSome shoot to kill, others shoot to mame. I say clear the chamber and let the lord decide. - Reno 911 ;  Nuts. ;  You weren''t actually planning to use that in any projects,were you? o_0 ;  I am trying to figure out how MSVC builds the classes so I can write a wrapper that will allow me (and possibly others) to use MinGW to write plugins for GDArena, at least until Khawk make it compatible with other compilers.Now that I know they are layed out differently I have an explanation for why MinGW compiled DLLs kill it.So, anyone know how MSVC lays everything out so I can write my wrapper? 