Solved

Add an item to a listbox in another running process

Posted on 2006-11-04
5
170 Views
Last Modified: 2010-04-05
I have a running process (not my own) and I want to add an item to a listbox.

I have the handle of the listbox control, can somebody give me some code to add an item to it?

thanks
0
Comment
Question by:zattz
  • 3
5 Comments
 
LVL 28

Accepted Solution

by:
TName earned 500 total points
ID: 17873837
Hi, do you mean something like this? It will insert a string (as last item) into a listbox:

procedure TMainForm.Button1Click(Sender: TObject);
var
hnd:HWND;
pc:PChar;
Count:Integer;
begin
 pc:='TestString';
 hnd:=FindWindow(nil,'TestForm');
 if hnd<>0 then begin
   hnd := FindWindowEx(hnd, 0, 'TListBox', nil);
   if hnd<>0 then begin
      Count:=SendMessage(hnd, LB_GETCOUNT,0,0);
      SendMessage(hnd, LB_INSERTSTRING,Count-1,Integer(pc));
   end;
 end;
end;
0
 
LVL 28

Expert Comment

by:TName
ID: 17873852
Oops, sorry, if it's supposed to be the last item, it should be:

SendMessage(hnd, LB_INSERTSTRING,Count,Integer(pc));
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 17873921
Yep... one of the few exceptions where you can send(message) a string to a remote process without first allocating memory in the remote process. You can also just pass (-1) as the index if you want to append to the list (no need to get count)

Russell
0
 
LVL 28

Expert Comment

by:TName
ID: 17874632
>You can also just pass (-1) as the index if you want to append to the list (no need to get count)

Thanks, I wasn't aware of that :)

And I just see there's also LB_ADDSTRING.
So zattz, in fact if you already have the LB handle, all you really need is a line like:
SendMessage(hnd, LB_INSERTSTRING,-1,Integer(PChar('TestString')));

or
SendMessage(hnd, LB_ADDSTRING,0,Integer(PChar('TestString')));

0
 

Author Comment

by:zattz
ID: 17876188
Thanks Guys:)

Worked perfectly.

Except that I made a mistake and actually wanted to send to a combobox so I changed LB_ADDSTRING to CB_ADDSTRING

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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 …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

864 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now