Announcement

Collapse
No announcement yet.

Self-Parameter entfernen

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Self-Parameter entfernen

    <p>Ich bin gerade ein einer Komponenten-Implementation für einen
    Multimedia-Timer, der gleich einzusetzen ist, wie die TTimer-
    Komponente von Delphi.</p>
    <p>Dabei stiess ich auf das Problem, dass die Win-MM-API Funktion
    timeSetEvent() eine Globale Funktion erwartet. Wie ich in diesem
    Forum schon erfahren habe, gibt es aber die Möglichkeit, den
    (unsichtbaren) self-Parameter bei Objekt-Methoden zu entfernen,
    dazu sollte ich die Funktion</p>
    <font face="" style="font-family: monospace;"><pre> <b>function</b> MakeObjectInstance(Method: TWndMethod): Pointer;</pre></font>
    <p>verwenden.</p>
    <p>Leider gelingt es mir nicht, da der Parameter TWndMethod</p>
    <font face="" style="font-family: monospace;"><pre> TWndMethod = <b>procedure</b>(var Message: TMessage) of object;</pre></font>
    <p>so definiert ist, und folglich andere Parameter hat als die
    Procedure CallBackTimerProc.</p>
    <p>Bis zum Scheitern sieht mein Code so aus:</p>
    <font face="" style="font-family: monospace;"><pre>
    <font face="" color="Gray">01</font> TMMTimer = <b>class</b>(TComponent)
    <font face="" color="Gray">02</font> <i>{ ... }</i>
    <font face="" color="Gray">03</font> TimerHandle : MMRESULT;
    <font face="" color="Gray">04</font> TimerCallBack : TFNTimeCallBack;
    <font face="" color="Gray">05</font> <b>procedure</b> CallBackTimerProc(uTimerID, uMessage: UINT; dwUser, dw1, dw2: DWORD) <b>stdcall</b>;
    <font face="" color="Gray">06</font> <i>{ ... }</i>
    <font face="" color="Gray">07</font> <b>end</b>;
    <font face="" color="Gray">08</font>
    <font face="" color="Gray">09</font> <b>procedure</b> TMMTimer.InitTimer;
    <font face="" color="Gray">10</font> <b>begin</b>
    <font face="" color="Gray">11</font> <i>{ ... }</i>
    <font face="" color="Gray">12</font> TimerCallBack := MakeObjectInstance(CallBackTimerProc); <i>// funktioniert so nicht</i>
    <font face="" color="Gray">13</font> TimerHandle := timeSetEvent(FInterval, FResolution, TimeCallBack, 0, TIME_PERIODIC);
    <font face="" color="Gray">14</font> <i>{ ... }</i>
    <font face="" color="Gray">15</font> <b>end</b>;
    </pre></font>

    <p>Wenn CallBackTimerProc eine globale Prozedur ist, funktioniert es so:</p>
    <font face="" style="font-family: monospace;"><pre><font face="" color="Gray">12</font> TimerCallBack := CallBackTimerProc;</pre></font>

    <p>Eine globale Procedure kann ich ja nicht einsetzen, ich muss ja für jede
    Instanz des TMMTimer-Objekts eine Call-Back Funktion haben.</p>

    <p>Ist der Ansatz mit MakeObjectInstance falsch oder liegt das Problem woanders?</p>

  • #2
    Hi

    <pre>

    type
    TMMTimer = class
    private
    protected
    procedure DoTimer;
    public
    constructor Create();
    end;

    procedure TimerProc(uTimerID, uMessage: UINT; dwUser, dw1, dw2: DWORD); stdcall;
    begin
    TMMTimer(dwUser).DoTimer;
    end;

    constructor TMMTimer.Create();
    begin

    TimerHandle := timeSetEvent(FInterval, FResolution, TimerProc, Integer(Self), TIME_PERIODIC);
    end;

    </pre>

    Gruß Hage

    Comment


    • #3
      <p>Super, so funktioniert es. Danke für die Lösung.</p>
      <p>Gruss Andy</p&gt

      Comment

      Working...
      X