Mit diesem Konsolenprogramm bekommt man ein Callback aller offenen Windows-Fenster. Der Unterschied bei diesem Beispiel im Gegensatz zu anderen liegt darin, dass der Callback ultraschnell ist.
program CallBackMethod;
{$APPTYPE CONSOLE}
uses
Windows, Messages;
type
TEnumWindows = class(TObject)
private
FEnumProcInst: Pointer;
function MakeProcInstance(M: TMethod): Pointer;
procedure FreeProcInstance(ProcInstance: Pointer);
function EnumWindows(hWnd: THandle; lp: LPARAM): Boolean; stdcall;
public
destructor Destroy; override;
procedure ListWindows;
end;
{ TEnumWindows }
function TEnumWindows.MakeProcInstance(M: TMethod): Pointer;
begin
// allocate memory
GetMem(Result, 15);
asm
// MOV ECX,
MOV BYTE PTR [EAX], $B9
MOV ECX, M.Data
MOV DWORD PTR [EAX+$1], ECX
// POP EDX
MOV BYTE PTR [EAX+$5], $5A
// PUSH ECX
MOV BYTE PTR [EAX+$6], $51
// PUSH EDX
MOV BYTE PTR [EAX+$7], $52
// MOV ECX,
MOV BYTE PTR [EAX+$8], $B9
MOV ECX, M.Code
MOV DWORD PTR [EAX+$9], ECX
// JMP ECX
MOV BYTE PTR [EAX+$D], $FF
MOV BYTE PTR [EAX+$E], $E1
end;
end;
procedure TEnumWindows.FreeProcInstance(ProcInstance: Pointer);
begin
// free memory
FreeMem(ProcInstance, 15);
end;
function TEnumWindows.EnumWindows(hWnd: THandle; lp: LPARAM): Boolean; stdcall;
var
Buffer: PChar;
len: Integer;
begin
if IsWindow(hWnd) and IsWindowVisible(hWnd) then
begin
len := SendMessage(hWnd, WM_GETTEXTLENGTH, 0, 0);
if len > 0 then
begin
Buffer := GetMemory(len);
try
SendMessage(hWnd, WM_GETTEXT, len + 1, Integer(Buffer));
Writeln(Buffer);
finally
FreeMemory(Buffer);
end;
end;
end;
Result := True;
end;
destructor TEnumWindows.Destroy;
begin
FreeProcInstance(FEnumProcInst);
inherited;
end;
var
EnumWnds: TEnumWindows;
procedure TEnumWindows.ListWindows;
var
Method: TMethod;
begin
Method.Code := @TEnumWindows.EnumWindows;
Method.Data := Self;
FEnumProcInst := MakeProcInstance(Method);
Windows.EnumWindows(FEnumProcInst, 0);
end;
begin
EnumWnds := TEnumWindows.Create;
try
EnumWnds.ListWindows;
finally
EnumWnds.Free;
end;
Readln;
end.
Keine Kommentare:
Kommentar veröffentlichen