AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Callback mit externer C-dll

Ein Thema von oXmoX · begonnen am 23. Jun 2005 · letzter Beitrag vom 24. Jun 2005
 
NicoDE
(Gast)

n/a Beiträge
 
#10

Re: Callback mit externer C-dll

  Alt 24. Jun 2005, 13:11
Apropos Service, hier ist eine Version die mit 'pascal', 'stdcall' und 'safecall' (auch unter WinXP SP2) funktioniert:
(falls es mal jemand brauchen sollte)
Delphi-Quellcode:
////////////////////////////////////////////////////////////////////////////////
//
// MakeStdcallCallback (thunk to use stdcall method as static callback)
// (works too for pascal and safecall, but NOT for cdecl and register!)

function MakeStdcallCallback(const Method: TMethod): Pointer;
{$IFDEF WIN32}
type
  PCallbackCode = ^TCallbackCode;
  TCallbackCode = packed record
    Ops1: array [0..2] of Longword;
    Val1: Pointer;
    Ops2: array [0..1] of Longword;
    Val2: Pointer;
  end;
{$ENDIF}
begin
{$IFDEF WIN32}
  Result := VirtualAlloc(nil, $100, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
  if Assigned(Result) then
    try
      with PCallbackCode(Result)^ do
      begin
        // ; do not change registers!
        // push eax ; stack space (Self)
        // push eax ; backup accumulator
        // mov eax, [esp+8] ; get return address
        // mov [esp+4], eax ; save at lower addr
        // mov eax, [x].TMethod.Data ; get x.Self pointer
        // mov [esp+8], eax ; save it into space
        // pop eax ; get eax from stack
        // nop ; think about it *g*
        // jmp dword ptr [x].TMethod.Code ; now jump to method
        // ; no need to cleanup stack
        Ops1[0] := $448B5050;
        Ops1[1] := $44890824;
        Ops1[2] := $058B0424;
        Val1 := Addr(Method.Data);
        Ops2[0] := $08244489;
        Ops2[1] := $25FF9058;
        Val2 := Addr(Method.Code);
      end;
    except
      VirtualFree(Result, 0, MEM_RELEASE);
      Result := nil;
    end;
{$ELSE}
  Result := nil;
{$ENDIF}
end;

procedure FreeCallback(Callback: Pointer);
begin
{$IFDEF WIN32}
  if Assigned(Callback) then
    VirtualFree(Callback, 0, MEM_RELEASE);
{$ENDIF}
end;
Fehlt nur noch 'register', aber dazu bin ich heute zu faul.
  Mit Zitat antworten Zitat
 


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 12:17 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz