/// <summary> /// Retrieves the text of a list-view item. /// </summary> /// <param name="index">Index of the desired item of which to retrieve the text.</param> public string GetItemText(int index) { string text = ""; using (var memory = new ControlMemory(Window, Marshal.SizeOf(typeof(User32.ListViewItem)))) { // TODO: Any way of knowing the maximum length of item text here? // Trial and error might be needed, trying a bigger buffer when the whole string is not retrieved: // http://forums.codeguru.com/showthread.php?351972-Getting-listView-item-text-length; var stringBuffer = new ControlMemory(Window, Constants.MaximumPathLength * 2); var itemData = new User32.ListViewItem { TextMax = Constants.MaximumPathLength, Text = stringBuffer.Address }; memory.Write(itemData); int length = (int)SendMessage(Message.GetItemText, new IntPtr(index), memory.Address); itemData = memory.Read <User32.ListViewItem>(); if (length > 0) { byte[] textBuffer = stringBuffer.Read <byte>(length * 2); text = Encoding.Unicode.GetString(textBuffer); } stringBuffer.Dispose(); } return(text); }
/// <summary> /// Read a structure of a given type from a memory address allocated by this function, but filled by the caller. /// </summary> /// <typeparam name="T">The type of the structure to read.</typeparam> /// <param name="fillMemory">Fills up the memory at the provided address, from which the structure will be read.</param> /// <returns>The read structure from the memory address.</returns> internal T ReadFromAddress <T>(Action <IntPtr> fillMemory) where T : struct { using (var memory = new ControlMemory(Window, Marshal.SizeOf(typeof(T)))) { fillMemory(memory.Address); return(memory.Read <T>()); } }