Einzelnen Beitrag anzeigen

Muetze1
(Gast)

n/a Beiträge
 
#10

Re: EnumWindowsProc -> EAccessError, OutOfResources

  Alt 29. Jul 2006, 17:46
Ok, ich habe mich geirrt in 2 Dingen:

1. Die EnumWindowProc darf doch lokal sein!
2. EnumWindows() ist bei D5 u.a. noch so definiert, dass die EnumWindowProc als Pointer definiert wurde und nicht als Procedure Prototype.

Hier ein funktionierender Code:
Delphi-Quellcode:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    procedure FormShow(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

type
  TFensterInfo = record
    Handle: HWND;
    Caption: String;
    Icon: HIcon;
  end;
  PFensterInfoList = ^TFensterInfoList;
  TFensterInfoList = Array Of TFensterInfo;

Var
  WindowList: TFensterInfoList;

Function EnumWindowsProc(Wnd: HWND; lp: LPARAM): LongBool; stdcall;
Const
  coBufferSize = 2048;
Var
  lList: PFensterInfoList;
  lCaption: String;
  lCapLen: Integer;
Begin
  Result := True;
  lList := PFensterInfoList(lp);

  If IsWindowVisible(Wnd) and
     ((GetWindowLong(Wnd, GWL_HWNDPARENT)=0) or
      (HWND(GetWindowLong(Wnd, GWL_HWNDPARENT))=GetDesktopWindow)) and
     ((GetWindowLong(Wnd, GWL_EXSTYLE) and WS_EX_TOOLWINDOW)=0) then
  begin
    SetLength(lCaption, coBufferSize);
    lCapLen := SendMessage(Wnd, WM_GETTEXT, coBufferSize, Integer(PChar(lCaption)));
    SetLength(lCaption, lCapLen);

    SetLength(lList^, Length(lList^)+1);
    lList^[High(lList^)].Caption := lCaption;
    lList^[High(lList^)].Handle := Wnd;
  end;
end;

procedure Tasks();
Begin
  SetLength(WindowList, 0);

  EnumWindows(@EnumWindowsProc, Integer(@WindowList));
End;

procedure TForm1.FormShow(Sender: TObject);
Var
  i: Integer;
begin
  Tasks;

  For i := Low(WindowList) To High(WindowList) Do
    ListBox1.Items.Add(WindowList[i].Caption);
end;

end.
  Mit Zitat antworten Zitat