How to convert a member function pointer to a normal function pointer

d d at nomail.com
Thu May 3 07:47:40 PDT 2007


> How to convert a member function pointer to a normal function pointer?
> Just like Object Pascal Language's Classes.MakeObjectInstance(xxx);

Okay. To understand what the OP wants, you need to understand how Delphi
works. Right, Delphi's Object Pascal differentiates between function
pointers and method pointers. So:

type
  TWndMethod = procedure(var Message: TMessage) of object; // a method
pointer

  TWndFunc = procedure(var Message: TMessage); //a function pointer

The OP also mentions "Classes.MakeObjectInstance(xxx);... " Well, in my
version of Delphi, this is in the Forms unit (a unit is like a module - i.e.
a source file or I guess block of code.) So Classes.Xxxx or Forms.Xxx is a
bit like a namespace (but not really) though in practice, it doesn't work a
lot like that in Delphi for Win32. Anyway, back to the story. So
MakeObjectInstance takes a TWndMethod, which is a non static Method pointer,
and then converts it to a pointer to a standard Win32 WinProc ala:

function StdWndProc(Window: HWND; Message, WParam: Longint; LParam:
Longint): Longint; stdcall;

All the MakeObjectInstance call really does is make a stub that can be
called that "magically" puts in the hidden "self" param. It only works for
the type TWndMethod. What this allows the programmer to do is create an
Class that has a method that gets all Windows messages sent to the handle
the user sets up when thety pass the pointer to the WinProc to the
CreateWindow API call. Nothing more spectacular really. Okay, it also fills
out he TMessage instance, but all that really contains if the Message,
LParam, WParam etc from the standard WinProc call. It is probably possible
to take the Delphi code and translate the technique to D - though legally
that would probably be against the VCL sourcecode license...

M





More information about the Digitalmars-d mailing list