Solved

Add an item to a listbox in another running process

Posted on 2006-11-04
5
173 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

738 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