/// <summary> /// Allocate a chunk in another process and unmarshal a struct /// there. /// </summary> public static ProcessMemoryChunk AllocStruct(Process process, object structure) { int size = Marshal.SizeOf(structure); ProcessMemoryChunk result = Alloc(process, size); result.WriteStructure(0, structure); return(result); }
public void CheckItem(int itemIndex) { var lvi = new WindowsApi.LVITEM { iItem = itemIndex, iSubItem = 0, stateMask = 8192, //TODO: Replace these with the proper constants state = 8192, }; using (var structBuffer = ProcessMemoryChunk.AllocStruct(process, lvi)) { if (WindowsApi.SendMessage(handle, WindowsApi.LVM_SETITEMSTATE, (IntPtr)itemIndex, structBuffer.Location) == IntPtr.Zero) { throw new Exception("Couldn't set list item state"); } } }
/// <remarks> /// Taken from https://github.com/kvakulo/Switcheroo/blob/master/ManagedWinapi/SystemListView.cs#L75 /// </remarks> public IEnumerable <string> GetListItems() { var listViewItemCount = (int)WindowsApi.SendMessage(handle, WindowsApi.LVM_GETITEMCOUNT, IntPtr.Zero, IntPtr.Zero); for (var i = 0; i < listViewItemCount; ++i) { var lvi = new WindowsApi.LVITEM { cchTextMax = 512, iItem = i, iSubItem = 0, stateMask = 0xffffffff, mask = WindowsApi.LVIF_STATE | WindowsApi.LVIF_TEXT, }; using (var textBuffer = ProcessMemoryChunk.Alloc(process, lvi.cchTextMax * 2 /* Assuming 2-byte character size just in case */)) { lvi.pszText = textBuffer.Location; using (var structBuffer = ProcessMemoryChunk.AllocStruct(process, lvi)) { if (WindowsApi.SendMessage(handle, WindowsApi.LVM_GETITEM, IntPtr.Zero, structBuffer.Location) == IntPtr.Zero) { throw new Exception("Couldn't get list item"); } lvi = (WindowsApi.LVITEM)structBuffer.ReadToStructure(0, typeof(WindowsApi.LVITEM)); } //TODO: Check if pszText pointer location changed var titleBytes = textBuffer.Read(); var title = Encoding.Default.GetString(titleBytes); if (title.IndexOf('\0') != -1) { title = title.Substring(0, title.IndexOf('\0')); } yield return(title); } } }