Solved

Retrieving strings from a ListBox in another app

Posted on 2002-05-12
4
590 Views
Last Modified: 2008-03-10
Hi

I am trying to get the handle of a ListBox in another application and then read the strings into my own ListBox.  The following code does not work:

function EnumProc ( H:THandle; Par:Word):Boolean; StdCall;
var ca:Array[1..250] of char; N: Integer;
Begin
     Result := True;
     GetClassName(H,@Ca,SizeOf(ca));
     If Copy(ca,1,7) = 'ListBox' then
     begin
          MyClass := ca;
          Result := False;
     end;
End;

procedure TForm1.GetSearchWindow;
var HDlg : THandle; Count,N,TextLen: LongInt; ItemText: String;
    Strbuf: Array[0..255] of Char;
begin
     ListBox1.Items.Clear;
     HDlg := FindWindow(PChar(0),StrPCopy(Strbuf,'Search Results'));
     If hDlg > 0 then
     begin
          enumChildWindows (hdlg,@EnumProc,0);
          hDlg := FindWindow(StrPCopy(Strbuf,MyClass),PChar(0));
          Count := SendMessage(hDlg,LB_GETCOUNT,0,0);
          For N := 0 to Count-1 do
          begin
               TextLen := SendMessage(hDlg,LB_GETTEXTLEN,N,0);
               TextLen := TextLen+1;
               SetLength(ItemText,TextLen);
               SendMessage(hDlg,LB_GETTEXT,N,longint(@ItemText[1]));
               ListBox1.Items.Add(ItemText);
          end;
     end;
end;

The 'Search Results'  window is found, but  hDlg returns 0 on the next FindWindow trying to find the ListBox.  The variable MyClass returns 'ListBox' from the EnumProc function.

What am I doing wrong?

John
0
Comment
Question by:jdthedj
  • 2
4 Comments
 
LVL 33

Accepted Solution

by:
Slick812 earned 50 total points
ID: 7005287
hello jdthedj , if there is only ONE List box, then the code below should work, If the list box Items could be longer than 4096 charaters, then increase CharBuffer


procedure TForm1.sbut_FindListBClick(Sender: TObject);
var
hApp, hList: THandle;
Count, i: Integer;
CharBuffer: array[0..4096] of Char;
begin
ListBox1.Items.Clear;
hApp := FindWindow(nil,'Search Results);
if hApp <> 0 then
  begin
  hList := FindWindowEx(hApp,0,'LISTBOX',nil);
  Count := SendMessage(hList,LB_GETCOUNT,0,0);
  for i := 0 to Count-1 do
    begin;
    SendMessage(hList, LB_GETTEXT, i, Integer(@CharBuffer));
    ListBox2.Items.Add(CharBuffer);
    end;
  end;
end;
0
 
LVL 17

Expert Comment

by:geobul
ID: 7005304
Hi John,

>hDlg := FindWindow(StrPCopy(Strbuf,MyClass),PChar(0));

You can't get a child window handle using FindWindow function.

Add a new global var MyHandle: THandle; similar to MyClass.
Then in EnumProc write:

...
    If Copy(ca,1,7) = 'ListBox' then
    begin
         MyClass := ca;
         MyHandle := H;
         Result := False;
    end;
...

In GetSearchWindow write:

...
enumChildWindows (hdlg,@EnumProc,0);
if MyHandle > 0 then begin
  Count := SendMessage(hDlg,LB_GETCOUNT,0,0);
...

Regards, Geo
0
 
LVL 33

Expert Comment

by:Slick812
ID: 7006825
if you really want to use the EnumChildWindows function then


procedure TForm1.sbut_FindListBClick(Sender: TObject);
var
Count, i, hApp, hList: Integer;
CharBuffer: array[0..1024] of Char;

function EnumChildren(hChild: Integer; Pnt2Int:PInteger): Boolean; StdCall;
var
Buffer: array[0..10] of Char;
begin
{in this function the parameter variable Pnt2Int can not be declared as a var type
so if you want to get a value BACK to the EnumChildWindows( ) function, you will
need to use the Pointer type PInteger and Dereference it
(Pnt2Int^ := hChild) to change it's value outside of this fuction}
Result := True;
GetClassName(hChild,Buffer,9);
if CompareText(Buffer, 'LISTBOX')= 0 then
  begin
  Result := False;
  Pnt2Int^ := hChild;
  end else Pnt2Int^ := 0;
end;

begin
ListBox2.Items.Clear;
hApp := FindWindow(nil,'Menus and List Boxes');
if hApp <> 0 then
  begin
  hList := 0;
  {a more conventonal (API) way to get a value from an API callback
  func is to send the address of the variable (hList) and read it's
  value after the call}
  EnumChildWindows(hApp,@EnumChildren,Integer(@hList));
  if hList <> 0 then
    begin
    Count := SendMessage(hList,LB_GETCOUNT,0,0);
    for i := 0 to Count-1 do
      begin;
      SendMessage(hList, LB_GETTEXT, i, Integer(@CharBuffer));
      ListBox2.Items.Add(CharBuffer);
      end;
    end else
      ShowMessage('The ListBox does not exist');
  end else
  ShowMessage('The window does not exist');
end;
0
 
LVL 3

Author Comment

by:jdthedj
ID: 7007047
Thanks for the replies

Slick812:  Your first suggestion works, so I am accepting that as the answer.  Thanks!

Geo:  Thanks for your input too!

John
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question