ListViewAPIHelper.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. using System.Text;
  5. using System.Windows.Forms;
  6. namespace ErpServer
  7. {
  8. public class WindowsAPIHelper
  9. {
  10. protected const uint PROCESS_VM_OPERATION = 8u;
  11. protected const uint PROCESS_VM_READ = 16u;
  12. protected const uint PROCESS_VM_WRITE = 32u;
  13. protected const uint MEM_COMMIT = 4096u;
  14. protected const uint MEM_RELEASE = 32768u;
  15. protected const uint MEM_RESERVE = 8192u;
  16. protected const uint PAGE_READWRITE = 4u;
  17. public int GetProcessId(int hwnd)
  18. {
  19. int result = 0;
  20. WindowsAPIHelper.GetWindowThreadProcessId(hwnd, out result);
  21. return result;
  22. }
  23. public int InjectProcess(int processId)
  24. {
  25. return WindowsAPIHelper.OpenProcess(56u, false, processId);
  26. }
  27. [DllImport("kernel32.dll")]
  28. protected static extern int VirtualAllocEx(int hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);
  29. [DllImport("kernel32.dll")]
  30. protected static extern bool VirtualFreeEx(int hProcess, int lpAddress, uint dwSize, uint dwFreeType);
  31. [DllImport("kernel32.dll")]
  32. protected static extern bool ReadProcessMemory(int hProcess, int lpBaseAddress, IntPtr lpBuffer, int nSize, ref uint vNumberOfBytesRead);
  33. [DllImport("kernel32.dll")]
  34. protected static extern bool WriteProcessMemory(int hProcess, int lpBaseAddress, IntPtr lpBuffer, int nSize, ref uint vNumberOfBytesRead);
  35. [DllImport("user32.dll")]
  36. public static extern int FindWindow(string strClassName, string strWindowName);
  37. [DllImport("user32.dll")]
  38. public static extern int FindWindowEx(int hwndParent, int hwndChildAfter, string className, string windowName);
  39. [DllImport("user32.DLL")]
  40. protected static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);
  41. [DllImport("User32.dll", EntryPoint = "SendMessage")]
  42. protected static extern int SendMessage2(IntPtr hWnd, int Msg, int wParam, string lParam);
  43. [DllImport("user32.dll")]
  44. protected static extern int GetWindowThreadProcessId(int hwnd, out int processId);
  45. [DllImport("kernel32.dll")]
  46. protected static extern int OpenProcess(uint dwDesiredAccess, bool bInheritHandle, int processId);
  47. [DllImport("kernel32.dll")]
  48. protected static extern bool CloseHandle(int handle);
  49. }
  50. public class ListViewAPIHelper : WindowsAPIHelper
  51. {
  52. private struct LVITEM
  53. {
  54. public int mask;
  55. public int iItem;
  56. public int iSubItem;
  57. #pragma warning disable CS0649 // 从未对字段“ListViewAPIHelper.LVITEM.state”赋值,字段将一直保持其默认值 0
  58. public int state;
  59. #pragma warning restore CS0649 // 从未对字段“ListViewAPIHelper.LVITEM.state”赋值,字段将一直保持其默认值 0
  60. #pragma warning disable CS0649 // 从未对字段“ListViewAPIHelper.LVITEM.stateMask”赋值,字段将一直保持其默认值 0
  61. public int stateMask;
  62. #pragma warning restore CS0649 // 从未对字段“ListViewAPIHelper.LVITEM.stateMask”赋值,字段将一直保持其默认值 0
  63. public IntPtr pszText;
  64. public int cchTextMax;
  65. }
  66. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
  67. protected class HDITEM
  68. {
  69. public uint mask;
  70. public int cxy;
  71. public IntPtr pszText;
  72. public IntPtr hbm;
  73. public int cchTextMax;
  74. public int fmt;
  75. public int lParam;
  76. public int iImage;
  77. public int iOrder;
  78. }
  79. protected const uint LVM_FIRST = 4096u;
  80. protected const uint LVM_GETHEADER = 4127u;
  81. protected const uint LVM_GETITEMCOUNT = 4100u;
  82. protected const uint LVM_GETITEMTEXTA = 4141u;
  83. protected const uint LVM_GETITEMTEXTW = 4211u;
  84. protected const uint HDM_FIRST = 4608u;
  85. protected const uint HDM_GETITEMCOUNT = 4608u;
  86. protected const uint HDM_GETITEMW = 4619u;
  87. protected const uint HDM_GETITEMA = 4611u;
  88. protected int LVIF_TEXT = 1;
  89. protected int HDI_TEXT = 2;
  90. public int GetHeaderHwnd(int hwndListView)
  91. {
  92. return WindowsAPIHelper.SendMessage(hwndListView, 4127u, 0, 0);
  93. }
  94. public int GetRowCount(int hwndListView)
  95. {
  96. return WindowsAPIHelper.SendMessage(hwndListView, 4100u, 0, 0);
  97. }
  98. public int GetColumnCount(int hwndHeader)
  99. {
  100. return WindowsAPIHelper.SendMessage(hwndHeader, 4608u, 0, 0);
  101. }
  102. public List<string> GetColumnsHeaderText(int processHandle, int headerhwnd, int colCount)
  103. {
  104. List<string> list = new List<string>();
  105. uint num = 256u;
  106. int num2 = WindowsAPIHelper.VirtualAllocEx(processHandle, IntPtr.Zero, (uint)Marshal.SizeOf(typeof(ListViewAPIHelper.HDITEM)), 12288u, 4u);
  107. int num3 = WindowsAPIHelper.VirtualAllocEx(processHandle, IntPtr.Zero, num, 12288u, 4u);
  108. for (int i = 0; i < colCount; i++)
  109. {
  110. byte[] array = new byte[num];
  111. ListViewAPIHelper.HDITEM hDITEM = new ListViewAPIHelper.HDITEM();
  112. hDITEM.mask = (uint)this.HDI_TEXT;
  113. hDITEM.fmt = 0;
  114. hDITEM.cchTextMax = (int)num;
  115. hDITEM.pszText = (IntPtr)num3;
  116. IntPtr intPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(hDITEM));
  117. Marshal.StructureToPtr(hDITEM, intPtr, false);
  118. uint count = 0u;
  119. bool flag = WindowsAPIHelper.WriteProcessMemory(processHandle, num2, intPtr, Marshal.SizeOf(typeof(ListViewAPIHelper.HDITEM)), ref count);
  120. WindowsAPIHelper.SendMessage(headerhwnd, 4611u, i, num2);
  121. WindowsAPIHelper.ReadProcessMemory(processHandle, num3, Marshal.UnsafeAddrOfPinnedArrayElement(array, 0), (int)num, ref count);
  122. string @string = Encoding.Default.GetString(array, 0, (int)count);
  123. string text = "";
  124. string text2 = @string;
  125. for (int j = 0; j < text2.Length; j++)
  126. {
  127. char c = text2[j];
  128. if (c == '\0')
  129. {
  130. break;
  131. }
  132. text += c;
  133. }
  134. list.Add(text);
  135. }
  136. WindowsAPIHelper.VirtualFreeEx(processHandle, num2, 0u, 32768u);
  137. WindowsAPIHelper.VirtualFreeEx(processHandle, num3, 0u, 32768u);
  138. return list;
  139. }
  140. public string[,] GetItemCellsText(int processHandle, int hwndListView, int rows, int cols)
  141. {
  142. string[,] array = new string[rows, cols];
  143. uint num = 256u;
  144. int num2 = WindowsAPIHelper.VirtualAllocEx(processHandle, IntPtr.Zero, (uint)Marshal.SizeOf(typeof(ListViewAPIHelper.HDITEM)), 12288u, 4u);
  145. int num3 = WindowsAPIHelper.VirtualAllocEx(processHandle, IntPtr.Zero, num, 12288u, 4u);
  146. for (int i = 0; i < rows; i++)
  147. {
  148. for (int j = 0; j < cols; j++)
  149. {
  150. byte[] array2 = new byte[num];
  151. ListViewAPIHelper.LVITEM lVITEM = default(ListViewAPIHelper.LVITEM);
  152. lVITEM.mask = this.LVIF_TEXT;
  153. lVITEM.iItem = i;
  154. lVITEM.iSubItem = j;
  155. lVITEM.cchTextMax = (int)num;
  156. lVITEM.pszText = (IntPtr)num3;
  157. IntPtr intPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(lVITEM));
  158. Marshal.StructureToPtr(lVITEM, intPtr, false);
  159. uint count = 0u;
  160. WindowsAPIHelper.WriteProcessMemory(processHandle, num2, intPtr, Marshal.SizeOf(typeof(ListViewAPIHelper.LVITEM)), ref count);
  161. WindowsAPIHelper.SendMessage(hwndListView, 4141u, i, num2);
  162. if (j == 1)
  163. {
  164. WindowsAPIHelper.SendMessage2((IntPtr)hwndListView, 0xC, i, "LIU");
  165. }
  166. WindowsAPIHelper.ReadProcessMemory(processHandle, num3, Marshal.UnsafeAddrOfPinnedArrayElement(array2, 0), array2.Length, ref count);
  167. string @string = Encoding.Default.GetString(array2, 0, (int)count);
  168. array[i, j] = @string;
  169. }
  170. }
  171. WindowsAPIHelper.VirtualFreeEx(processHandle, num2, 0u, 32768u);
  172. WindowsAPIHelper.VirtualFreeEx(processHandle, num3, 0u, 32768u);
  173. return array;
  174. }
  175. public static void DoCatch(int hwnd, ListView LV)
  176. {
  177. LV.Columns.Clear();
  178. LV.Items.Clear();
  179. ListViewAPIHelper listViewAPIHelper = new ListViewAPIHelper();
  180. int headerHwnd = listViewAPIHelper.GetHeaderHwnd(hwnd);
  181. int rowCount = listViewAPIHelper.GetRowCount(hwnd);
  182. int columnCount = listViewAPIHelper.GetColumnCount(headerHwnd);
  183. int processId = listViewAPIHelper.GetProcessId(hwnd);
  184. int processHandle = listViewAPIHelper.InjectProcess(processId);
  185. List<string> columnsHeaderText = listViewAPIHelper.GetColumnsHeaderText(processHandle, headerHwnd, columnCount);
  186. for (int i = 0; i < columnsHeaderText.Count; i++)
  187. {
  188. string text = i.ToString();
  189. if (!string.IsNullOrEmpty(columnsHeaderText[i]))
  190. {
  191. text = columnsHeaderText[i];
  192. }
  193. LV.Columns.Add(text);
  194. }
  195. string[,] itemCellsText = listViewAPIHelper.GetItemCellsText(processHandle, hwnd, rowCount, columnCount);
  196. string[] array = new string[columnCount];
  197. for (int i = 0; i < rowCount; i++)
  198. {
  199. for (int j = 0; j < columnCount; j++)
  200. {
  201. array[j] = itemCellsText[i, j];
  202. }
  203. ListViewItem value = new ListViewItem(array);
  204. LV.Items.Add(value);
  205. }
  206. for (int i = 0; i < columnsHeaderText.Count; i++)
  207. {
  208. ColumnHeader columnHeader = LV.Columns[i];
  209. columnHeader.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
  210. }
  211. }
  212. }
  213. }