When I’m at a breakpoint debugging some C++ code, sometimes I drill down into members of variables to inspect values.
The way C++ implements inheritance, there is a virtual function pointer table, often called a vtable. The Visual Studio debugger allows me to inspect the members of this table. The vtable shows as a member called “__vfptr” which looks to me like something to do with VFP (“Visual FoxPro”).<g>
+ __vfptr 0x02763470 const ATL::CComObject<class CShellNSEShellView>::`vftable’ *
A COM object reference is a pointer to the object. The first N 4-byte integers are pointers to the vTables of that object’s interfaces (COM objects can inherit from multiple interfaces). Because every COM interface inherits from the IUnknown interface, the first 3 entries in each vTable are the 3 members of IUnknown: QueryInterface, AddRef and Release. The rest of the vTable contains pointers to the methods of that particular interface.
When you build a Fox COM object (which can be used by other clients such as Excel or IIS), Fox generates a COM object representation for your object (or implemented interface). That means it constructs a vTable and it generates an executable machine language stub entry point for your code. A Fox object can only implement one interface (the interface for your object) which inherits from IDispatch which inherits from IUnknown. Thus there is only one vTable for that object. The generated ASM allows the fox object to be called either by early (vTable) or late (IDispatch) binding (Dual Interface)
Some of the things this generated code does:
- Records the invoke mode (early or late binding) which you can query via SYS(2334)
- Enters a critical section for multiple threaded DLLs.
- Records the method particulars, such as fox object reference and method id
- Invokes a routine that directs the call to your code.
The code generation occurs at object instantiation time. It reads the Type Library of the Fox object to get the particulars of how the Fox object vTable should look.
I started writing the COM server stuff back a decade ago for VFP5.
For VFP7, I added the ability to Implement a COM interface, which also required the ability to generate a COM vTable. I just reused my code to generate the ASM stub from the COM server stuff, so these share the same code.