/// <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> /// Writes a structure of a given type to a memory address allocated by this function, and subsequently use it. /// </summary> /// <typeparam name="T">The type of the structure to read.</typeparam> /// <param name="toWrite">The structure to write to memory.</param> /// <param name="useMemory">Method using the pointer in memory where the written object is located.</param> internal void WriteToAddress <T>(T toWrite, Action <IntPtr> useMemory) where T : struct { using (var memory = new ControlMemory(Window, Marshal.SizeOf(typeof(T)))) { memory.Write(toWrite); useMemory(memory.Address); } }