Recent

Author Topic: [SOLVED] EnumWindows doesnt Finish  (Read 5730 times)

Cliff48

  • Jr. Member
  • **
  • Posts: 51
[SOLVED] EnumWindows doesnt Finish
« on: July 02, 2015, 02:07:23 am »
The Following code used to work, but now EnumWindows never finishes  /   returns.

Code: [Select]
function AllWindows(hW: THandle; List: Integer): Boolean; stdcall;
var
  l:      LongInt;
  found:  Boolean = false;
  wCaption:     String[255];
begin
  Result := True;
  l := SendMessage(hW, WM_GETTEXT, 254, LongInt(@wCaption[1]));
    if l > 0 then begin
      wCaption[0] := char(l);
      FormEnum.cb1.Items.add(wCaption);
    end;
  showMessage(IntToHex(hW, 8) + ' ' + IntToStr(l));
end;
   
 procedure TFormEnum.FormCreate(Sender: TObject);
begin
  ShowMessage('Enum Windows');
  enumWindows(@AllWindows, 0);
  showmessage('Pause');
end;       

EnumW calls the AllWindows Function many times, but after it is done, the system is left in a loop and ShowMessage('Pause') is never executed.

This is a Function I have in a working program and the above was created to isolate/demonstrate the problem I'm having.
One thing different, is that the working program is compiled with
{$mode Objfpc}{$H+}   but to compile the above I had to change to:  {$mode Delphi}{$H+}  which I dont understand why.

I assume I have done something silly, but I cant see it.  Any help would be appreciated.                         
« Last Edit: July 02, 2015, 02:29:41 am by Cliff48 »
Laz Ver 1.4.0  FPC 2.6.4 SVN 48774
Win 7 Ultimate SP1
Intel I7-2600 CPU @  3.40GHZ 8GB Ram 64bit OS

Cliff48

  • Jr. Member
  • **
  • Posts: 51
Re: [SOLVED] EnumWindows doesnt Finish
« Reply #1 on: July 02, 2015, 02:32:37 am »
I added

    if IsWindowVisible(hW) then 

before the SendMessage(hW, WM_GETTEXT .........

Obviously Windows hangs waiting for a response from a non-visible window.

Still curious as to why I needed to compile in Delphi mode tho.
Laz Ver 1.4.0  FPC 2.6.4 SVN 48774
Win 7 Ultimate SP1
Intel I7-2600 CPU @  3.40GHZ 8GB Ram 64bit OS

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: [SOLVED] EnumWindows doesnt Finish
« Reply #2 on: July 02, 2015, 02:37:33 am »
You need to replace mode ObjFPC by mode Delphi:
Code: [Select]
//{$mode objfpc}
{$mode delphi}{$H+}
« Last Edit: July 02, 2015, 02:40:07 am by typo »

Cliff48

  • Jr. Member
  • **
  • Posts: 51
Re: [SOLVED] EnumWindows doesnt Finish
« Reply #3 on: July 02, 2015, 03:29:37 am »
You need to replace mode ObjFPC by mode Delphi:
Code: [Select]
//{$mode objfpc}
{$mode delphi}{$H+}

Can someone explain why i had to change ... and why it compiled with objfpc previously?
Laz Ver 1.4.0  FPC 2.6.4 SVN 48774
Win 7 Ultimate SP1
Intel I7-2600 CPU @  3.40GHZ 8GB Ram 64bit OS

 

TinyPortal © 2005-2018