Solved

Add an item to a listbox in another running process

Posted on 2006-11-04
5
169 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

744 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

8 Experts available now in Live!

Get 1:1 Help Now