Add an item to a listbox in another running process

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
zattzAsked:
Who is Participating?
 
TNameConnect With a Mentor Commented:
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
 
TNameCommented:
Oops, sorry, if it's supposed to be the last item, it should be:

SendMessage(hnd, LB_INSERTSTRING,Count,Integer(pc));
0
 
Russell LibbySoftware Engineer, Advisory Commented:
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
 
TNameCommented:
>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
 
zattzAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.