Search code examples
c++builder

How do you print out the value of a System::Classes::TWndMethod variable?


If I have a variable that is of type System::Classes::TWndMethod, how can I print out the value with any of the *printf type functions using %p?

I tried casting to (void*) but the compiler failed with an internal error.

Basically, I have the WndProc of three TEdit fields that I'm overriding. They can all be handled the same, so I wanted to see if they all use the same WndProc (for the callback to original routine), which I presume they would, but would like to check before actually doing it.


Solution

  • You can't print out a TWndMethod as-is using %p, because it is not a single pointer, it is actually a struct which holds 2 pointers.

    If you want to print them out, cast the TWndMethod to System::TMethod first:

    The TMethod type stores the Code and Data pointers for a method. This type can be used in a type cast of a method pointer to access the code and data parts of the method pointer.

    Then you can print out its Code and Data fields as needed, where:

    • the Code field points at the class method itself

    • the Data field points at the object which is passed to the method's hidden this parameter.

    For example:

    TWndMethod wm = ...;
    TMethod &m = reinterpret_cast<TMethod&>(wm);
    printf("%p %p\n", m.Code, m.Data);