Click here to Skip to main content
15,886,673 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

below is my working C# code for getting item text from syslistview32 control of another application.

C#
IntPtr SysListView32 = Value[1];
            int count = SendMessage(SysListView32, LVM_GETITEMCOUNT,0,"0");
           
            LVITEM lvi = new LVITEM();
            lvi.mask = LVIF_TEXT; 
            lvi.cchTextMax = 512; 
            lvi.iItem = 1;            // the zero-based index of the ListView item 
            lvi.iSubItem = 0;
            lvi.pszText = Marshal.AllocHGlobal(512);
            IntPtr ptrLvi = Marshal.AllocHGlobal(Marshal.SizeOf(lvi)); 
            Marshal.StructureToPtr(lvi, ptrLvi, false);
            SendMessage(SysListView32, LVM_GETITEM, IntPtr.Zero, ptrLvi);  // Extract the text of the specified item 
            string itemText = Marshal.PtrToStringAuto(lvi.pszText); 

[StructLayoutAttribute(LayoutKind.Sequential)]
private struct LVITEM {
    public uint mask; 
    public int iItem; 
    public int iSubItem; 
    public uint state; 
    public uint stateMask; 
    public IntPtr pszText; 
    public int cchTextMax; 
    public int iImage; 
    public IntPtr lParam; 
} 


I have correct handle for SysListview32 control cause I am getting correct items count as "4" & Target SysListView32 application is shown as below

http://www.freeimagehosting.net/t/f07ab.jpg

But target aplication having this syslistview32 control always hanged when I tried to sendmessage for getting item with
C#
lvi.iItem = 1; 
lvi.iSubItem = 0;
SendMessage(SysListView32, LVM_GETITEM, IntPtr.Zero, ptrLvi);  

I think it has to with something memmory allocation problem.
can anybody please tell me where did I make mistake in above code?

Note : Both visual studio & Target SysteListView32 control application running as administrator in my Win 7 pc.
Posted
Updated 1-Dec-11 5:38am
v5
Comments
Sergey Alexandrovich Kryukov 13-Apr-13 0:29am    
Please stop posting non-answers as "solution". It can give you abuse reports which eventually may lead to cancellation of your CodeProject membership.
Comment on any posts, reply to available comments, or use "Improve question" (above).
Also, keep in mind that members only get notifications on the post sent in reply to there posts.
—SA

1 solution

Hi

To access memory (and the control contents) in another application you need to use win32 API functions to prepare a memory buffer that is accessible for the other process. If you pass a pointer from your own process, the memory area pointed to by this pointer cannot be accessed from the target process.

I do not remember the API names, possibly virtualalloc is one of them.

BR,
Jozsef
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900