I recently came across this problem myself for the up-teenth time in one of my own projects. So for several days now I have been researching and tinkering with an alternative approach that even the VCL itself uses internally - proxy functions.
Specifically, the VCL has a MakeObjectInstance() function (and accompanied FreeObjectInstance() function) which allows a class method of a particular signature (TWndMethod) to be used as a Win32 API window procedure callback. In other words, any class method with the following signature:
Code: Select all
void __fastcall (__closure*)(TMethod&)
Code: Select all
LRESULT WINAPI (*)(HWND, UINT, WPARAM, LPARAM)
What I have been working on this past week is a new set of proxy code that takes MakeObjectInstance() to a whole new level. Specifically, I've taken Borland's basic proxy stub code and abstracted it out so it can be used with any kind of class method of any signature. The main proxying is controlled by a user-defined function for each proxy that is generated.
So far, I have made several passthrough proxies that support the most common calling conventions - cdecl, stdcall, and Borland's fastcall. These require the source and target signatures to match other than the use of __closure, for example:
Code: Select all
char* __cdecl (__closure *)(char*, char*, ...)
Code: Select all
char* __cdecl (*)(char*, char*, ...)
Code: Select all
bool __stdcall (__closure *)(double)
Code: Select all
bool __stdcall (*)(double)
Code: Select all
void __fastcall (__closure *)(int)
Code: Select all
void __fastcall (*)(int)
Code: Select all
void __fastcall (__closure *)(char*, char*, bool&)
Code: Select all
bool __cdecl (*)(char*, char*)