1

How i can cast a WNDPROC to a TWndMethod

so far i try this but give me a invalid typecast error.

 TWndMethod(Pointer(GetWindowLong(FHandle, GWL_WNDPROC)));
Salvador
  • 16,132
  • 33
  • 143
  • 245
  • 1
    You are implementing ancient window subclassing technique in Delphi. Generally it is not as easy as Sertac Akyuz answer (and requires more details about what are you doing for a good answer - may be your don't need this kind of subclassing at all), but an answer to your current question is MakeObjectInstance function. – kludg Dec 03 '10 at 03:11
  • Back up. What makes you think you need to do such a thing at all? You're probably doing something wrong. – Rob Kennedy Dec 03 '10 at 03:57

2 Answers2

2

If you want to subclass a window handle (using SetWindowLong) to process window messages in TWndMethod function, you should not cast the value returned by GetWindowLong(FHandle, GWL_WNDPROC) to TWndMethod. You should use MakeObjectInstance function to obtain a value that can be passed to SetWindowLong instead. Read Sertac Akuyz answer for a general idea.

Note that the need to subclass a window handle is very rare in Delphi applications. Delphi provides several other ways to interfere into window message processing, they are more simple and safe.

kludg
  • 27,213
  • 5
  • 67
  • 118
  • Since the mentioned answer is no more, one can check the second code example in the [answer](http://stackoverflow.com/questions/2660799/why-doesnt-my-cursor-change-to-an-hourglass-in-my-finddialog-in-delphi/2661080#2661080) to this [question](http://stackoverflow.com/questions/2660799/why-doesnt-my-cursor-change-to-an-hourglass-in-my-finddialog-in-delphi) to see sub-classing at work for a good cause. – Sertac Akyuz Dec 05 '10 at 13:36
1

You've got two problems here. First, GetWindowLong doesn't give you the actual WndProc, but a handle to it.

Second, TWndMethod is defined as procedure(var Message: TMessage) of object; It's a method pointer, not a function pointer, so you can't cast a normal pointer to it. What exactly are you trying to do?

Mason Wheeler
  • 82,511
  • 50
  • 270
  • 477