Getting ListView items in C# from SysListView32 using SendMessage

D

Dave Rich

Hi

I am trying to access the listview items from a SysListView32 control
in C#. I am using LVM.FINDITEM and LVFINDINFO through SendMessage to
try to get the text from each column in the item (it is a multi-column
list view).

I am using the PARTIAL flag to try to find an item starting with a
certain letter, that i am setting on the LVFINDINFO.psz. I am always
being returned -1 (or 0) as the index and am not quite sure what i am
doing wrong - i hate Win32! Could someone be able to outline the steps
i need to take, given the handle to the syslistview32 control, to get
an item from a multi-column listview and find out the text in each
column for that item?

Many thanks

Dave
 
M

Mattias Sjögren

Dave,

Is the listview in your application or in a different process? Most of
these messages will only work in the same process since the message
parameters don't marshal correctly cross process boundaries.



Mattias
 
D

David Rich

Hi
Thanks for your reply. The listview is in another process. I can use
SendMessage to get information from a RichTextBox and a ComboBox. I was
very much hoping i could do the same with the listview. To be honest, i
am fairly new to Win32 so it is probably the basics i have got wrong.

Would it be possible to list the steps i would need to take to make this
work. Currently, my calling code is:

internal unsafe int ListView_FindItem(IntPtr lv, int iStart,
ref LVFINDINFO lvfi)
{
LVFINDINFO_INTERNAL lvfii = new LVFINDINFO_INTERNAL();

lvfii.flags = lvfi.flags;
lvfii.lParam = lvfi.lParam;
lvfii.vkDirection = lvfi.vkDirection;
lvfii.pt = new POINT();
lvfii.pt.x = lvfi.pt.X;
lvfii.pt.y = lvfi.pt.Y;
if(lvfi.psz != null)
{
fixed(char* ptr = lvfi.psz.ToCharArray())
{
lvfii.psz = ptr;
return SendMessage(lv, (int)LVM.FINDITEM, (uint)iStart,
new IntPtr(&lvfii).ToInt32());
}
}
return SendMessage(lv, (int)LVM.FINDITEM, (uint)iStart,
new IntPtr(&lvfii).ToInt32());
}

public string FindInfo( IntPtr listView, string find )
{
LVFINDINFO lvfi = new LVFINDINFO();
lvfi.flags = LVFI.PARTIAL;
lvfi.psz = "A";
int returned = ListView_FindItem(listView, -1, ref lvfi);
return lvfi.psz;
}

It compiles and runs without error (apart from the lack of output), so
am assuming all my definitions are ok.

Am i correct in assuming lvfi.psz should contain the text an the return
value of SendMessage contain the index!?

Many thanks for your help

Dave
 
M

Mattias Sjögren

David,
I can use
SendMessage to get information from a RichTextBox and a ComboBox.

Because Windows knows how to marshal those messages.

Would it be possible to list the steps i would need to take to make this
work.

It's far from trivial, and probably not something you want to do in
managed code. One way is to inject code to run in the foreign process
(CreateRemoteThread for example), and make that pass the data back via
some shared memory.



Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top