Solved

Add an item to a listbox in another running process

Posted on 2006-11-04
5
174 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses
Course of the Month6 days, 17 hours left to enroll

622 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