How to pass a method as callback to a Windows API call?

前端 未结 6 1852
萌比男神i
萌比男神i 2020-12-14 13:10

I\'d like to pass a method of a class as callback to a WinAPI function. Is this possible and if yes, how?

Example case for setting a timer:

TMyClass          


        
6条回答
  •  温柔的废话
    2020-12-14 13:38

    Madshi has a MethodToProcedure procedure. It's in the "madTools.pas" which is in the "madBasic" package. If you use it, you should change the calling convention for "TimerProc" to stdcall and DoIt procedure would become,

    TMyClass = class
    private
      Timer: UINT;
      SetTimerProc: Pointer;
    [...]
    
    procedure TMyClass.DoIt;
    begin
      SetTimerProc := MethodToProcedure(Self, @TMyClass.TimerProc);
      Timer := SetTimer(0, 0, 8, SetTimerProc);
    end;
    // After "KillTimer(0, Timer)" is called call:
    // VirtualFree(SetTimerProc, 0, MEM_RELEASE);
    


    I've never tried but I think one could also try to duplicate the code in the "classses.MakeObjectInstance" for passing other procedure types than TWndMethod.

提交回复
热议问题