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);
Windows.EnumWindows(EnumProcInst, 0);
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 + 1);
try
SendMessage(hWnd, WM_GETTEXT, len, Integer(Buffer));
Writeln(Buffer);
finally
FreeMemory(Buffer);
end;
end;
end;
Result := True;
end;
destructor TEnumWindows.Destroy;
begin
FreeProcInstance(EnumProcInst);
inherited;
end;