dahall / Vanara

A set of .NET libraries for Windows implementing PInvoke calls to many native Windows APIs with supporting wrappers.
MIT License
1.75k stars 190 forks source link

can't use lvitem struct in SendMessage for listview #410

Closed zhao0876 closed 1 year ago

zhao0876 commented 1 year ago

When I use the SendMessage command to send something similar to ListViewMessage. LVM_GETITEMTEXT instruction Based on the help, I attempted to specify an LVITEM object for IParam to receive the results However, SendMessage provides the following error prompt:

CS0453: Type 'ComCtl32. LVITEM' must be a non nullable value type in order to be used as parameter 'TLP' in a generic type or method User32. SendMessage(HWND, TMsg, nint, TLP)

Does it need to provide a pointer to an LVITEM object as a parameter? I saw in other searches that the memory pointed to by this pointer needs to be created within the target program, is this also the case in Vanara? Anyway, I hope to get a valid example of sending ListViewMessage.

What code is involved

      var item = new LVITEM(row);
      var result=  SendMessage(hwnd, ListViewMessage.LVM_GETITEMTEXT, (nint)row, item);
dahall commented 1 year ago

LVITEM is a class, so it has to be pinned in memory before passing to SendMessage.

var item = new LVITEM(row);
using var pitem = new PinnedObject(item);
var result = SendMessage(hwnd, ListViewMessage.LVM_GETITEMTEXT, (IntPtr)row, pitem);