Read Items from a ListView in another Process

I have a windows application that controls another windows application.  I need to read the items in a SysListView32 control in the app being controlled.  I have the handle to the control and can call the ListView_SetItemCount and ListView_EnsureVisible macros OK with the correct results.  I can not get ListView_GetItemText to work though.  I think it may be because of pointers across different processes but I noticed LB_GETTEXT works with a ListBox control?  Is there a way to read from a ListView in a different process?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

3 ways possible:

1)If B is written by you, send WM_COPYDATA message with the item text to B. When B receives this message, it must get LV item itself.

2)Install hook (SetWindowHookEx, for example WH_GETMESSAGE) on the LV thread (use GetWindowThreadProcessId). You must get an item from the hook proc (which is in the hook dll). It'll work because hook proc is in the address space of B.

3) No documented way for 95/98.
For NT:
Alloc memory in the B address space using pMem=VirtualAllocEx()
Place LV_ITEM inside pMem using WriteProcessMemory.
Then call ListView_InsertItem(lv, pMem). Then use ReadProcessMemory to get result.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Sorry, I copied this text from my  previous answer to the same question. That question was about inserting an item into a list view. So may be my answer above is not clear enough.

If another app is written by you, it's better to use WM_COPY message.

If another app is not yours, then you have to use the one of the Windows hooks to get access to the address space of another process (you have to place the hook into DLL).

If your program will work on NT (or Win2000) only, then VirtualAllocEx is the best solution.

Choose one of the ways above and I'll give you additional details if necessary.
joeslowAuthor Commented:
I did not write the app I am trying to control, nor do I have the source code to modify it...
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

joeslowAuthor Commented:
Also, my app will only be running on NT.  I need a solution where I don't need to modify the app I am trying to control (B).

Thank you,
Ok, you know the window handle of the listview in B (hLV).

1) Obtain process id of B by GetWindowThreadProcessId(hLV,&pid)

2) Get the process handle of B by OpenProcess(PROCESS_VM_WRITE| PROCESS_VM_OPERATION | PROCESS_VM_READ)

3) Allocate the memory in the address space of B by VirtualAllocEx. Allocated block must be large enough to hold LVITEM structure and the buffer for the item text. Like this:

 struct S {
    LVITEM it;
    char buf[120];
 } s;

 S* ps=VirtualAllocEx(... sizeof(s)...)

4) Fill allocated memory by WriteProcessMemory:;
     LPBYTE(ps)+(LPBYTE(s.buf)-LPBYTE(&s))  );

Now copy s struct to ps by WriteProcessMemory.

5) Call ListView_GetItem(hLV,ps);

6) Read contents of ps->buf by ReadProcessMemory()

7) Use VirtualFreeEx to free memory and CloseHandle to close process handle.
joeslowAuthor Commented:
Thanks for the detailed explanation, I would have never figured this out in time.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.