program Project2;
{$APPTYPE CONSOLE}
uses
Windows;
type
TEnumWindows =
class(TObject)
private
EnumProcInst: Pointer;
function MakeProcInstance(M: TMethod): Pointer;
procedure FreeProcInstance(ProcInstance: Pointer);
function EnumWindows(hWnd: THandle; lp: LPARAM): Boolean;
stdcall;
public
constructor Create;
destructor Destroy;
override;
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;
constructor TEnumWindows.Create;
var
Method: TMethod;
begin
Method.Code := @TEnumWindows.EnumWindows;
Method.Data := Self;
EnumProcInst := MakeProcInstance(Method);
EnumWindows(Cardinal(EnumProcInst), 0);
end;
function TEnumWindows.EnumWindows(hWnd: THandle; lp: LPARAM): Boolean;
stdcall;
begin
Writeln(hWnd);
Result := True;
end;
destructor TEnumWindows.Destroy;
begin
FreeProcInstance(EnumProcInst);
inherited;
end;
var
EnumWnds: TEnumWindows;
begin
EnumWnds := TEnumWindows.Create;
EnumWnds.Free;
Readln;
end.