Back to original topic...

Couple of days ago when testing how D3DX9 loading of .X meshes with embedded hierachy working in FreePascal I've stumbled the same problem as Ultra: details of FreePascal implementation of VMT are different to C++ / TurboPascal / Delphi ones. In Delphi/C++ first virtual method is allocated in VMT with Offset == 0, but in FreePascal it has positive offset of 80 bytes. :twisted:

I found a solution to this problem, althrow not very elegant, but at least working. In mine case I've needed to export Pascal class to C++ code. First I've thought about patching class so Self will be pointing to "correct" method, but in this case Pascal code will loose ability to access class data members. Finally I've decided to use interfaces feature of object pascal (not least due to both mine C++ headers and Ultra ones pretend to be using some kind of COM incompatible interfaces ).

So for Ultra case solution will be:
[pascal][background=#FFFFFF][normal=#000000][number=#0000FF][string=#0000FF][comment=#248F24][reserved=#000000]
// MyClass.pas
type
IMyClass = interface
public
procedure DoSomething; virtual; cdecl; abstract;
end;

// pasdll.dpr
procedure InitDll(const MyClass: IMyClass); cdecl;
begin
// Before using MyClass patch interface to skip IUnknown fields - not needed in our case
// WARNING: you should NOT assign MyClass to other interfaces, as it's not REAL COM INTERFACE!!!
Dec(PInteger(MyClass)^, $C);

MyClass.DoSomething;

// After using - restore interface VMT
Inc(PInteger(MyClass)^, $C);
end;

exports
InitDll name 'InitDll';
[/pascal]

In case you need to pass Pascal "interface" to C++ (as I needed) you should perform reverse operation: Inc Self pointer before passing to C++ code, and Dec it after call. There are some other implementation details in this case - I'll later post excerpts from mine code to demonstrate complete solution.