| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- using System;
- using System.Collections.Generic;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Windows.Forms;
- namespace ErpServer
- {
- public class WindowsAPIHelper
- {
- protected const uint PROCESS_VM_OPERATION = 8u;
- protected const uint PROCESS_VM_READ = 16u;
- protected const uint PROCESS_VM_WRITE = 32u;
- protected const uint MEM_COMMIT = 4096u;
- protected const uint MEM_RELEASE = 32768u;
- protected const uint MEM_RESERVE = 8192u;
- protected const uint PAGE_READWRITE = 4u;
- public int GetProcessId(int hwnd)
- {
- int result = 0;
- WindowsAPIHelper.GetWindowThreadProcessId(hwnd, out result);
- return result;
- }
- public int InjectProcess(int processId)
- {
- return WindowsAPIHelper.OpenProcess(56u, false, processId);
- }
- [DllImport("kernel32.dll")]
- protected static extern int VirtualAllocEx(int hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);
- [DllImport("kernel32.dll")]
- protected static extern bool VirtualFreeEx(int hProcess, int lpAddress, uint dwSize, uint dwFreeType);
- [DllImport("kernel32.dll")]
- protected static extern bool ReadProcessMemory(int hProcess, int lpBaseAddress, IntPtr lpBuffer, int nSize, ref uint vNumberOfBytesRead);
- [DllImport("kernel32.dll")]
- protected static extern bool WriteProcessMemory(int hProcess, int lpBaseAddress, IntPtr lpBuffer, int nSize, ref uint vNumberOfBytesRead);
- [DllImport("user32.dll")]
- public static extern int FindWindow(string strClassName, string strWindowName);
- [DllImport("user32.dll")]
- public static extern int FindWindowEx(int hwndParent, int hwndChildAfter, string className, string windowName);
- [DllImport("user32.DLL")]
- protected static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);
- [DllImport("User32.dll", EntryPoint = "SendMessage")]
- protected static extern int SendMessage2(IntPtr hWnd, int Msg, int wParam, string lParam);
- [DllImport("user32.dll")]
- protected static extern int GetWindowThreadProcessId(int hwnd, out int processId);
- [DllImport("kernel32.dll")]
- protected static extern int OpenProcess(uint dwDesiredAccess, bool bInheritHandle, int processId);
- [DllImport("kernel32.dll")]
- protected static extern bool CloseHandle(int handle);
- }
- public class ListViewAPIHelper : WindowsAPIHelper
- {
- private struct LVITEM
- {
- public int mask;
- public int iItem;
- public int iSubItem;
- #pragma warning disable CS0649 // 从未对字段“ListViewAPIHelper.LVITEM.state”赋值,字段将一直保持其默认值 0
- public int state;
- #pragma warning restore CS0649 // 从未对字段“ListViewAPIHelper.LVITEM.state”赋值,字段将一直保持其默认值 0
- #pragma warning disable CS0649 // 从未对字段“ListViewAPIHelper.LVITEM.stateMask”赋值,字段将一直保持其默认值 0
- public int stateMask;
- #pragma warning restore CS0649 // 从未对字段“ListViewAPIHelper.LVITEM.stateMask”赋值,字段将一直保持其默认值 0
- public IntPtr pszText;
- public int cchTextMax;
- }
- [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
- protected class HDITEM
- {
- public uint mask;
- public int cxy;
- public IntPtr pszText;
- public IntPtr hbm;
- public int cchTextMax;
- public int fmt;
- public int lParam;
- public int iImage;
- public int iOrder;
- }
- protected const uint LVM_FIRST = 4096u;
- protected const uint LVM_GETHEADER = 4127u;
- protected const uint LVM_GETITEMCOUNT = 4100u;
- protected const uint LVM_GETITEMTEXTA = 4141u;
- protected const uint LVM_GETITEMTEXTW = 4211u;
- protected const uint HDM_FIRST = 4608u;
- protected const uint HDM_GETITEMCOUNT = 4608u;
- protected const uint HDM_GETITEMW = 4619u;
- protected const uint HDM_GETITEMA = 4611u;
- protected int LVIF_TEXT = 1;
- protected int HDI_TEXT = 2;
- public int GetHeaderHwnd(int hwndListView)
- {
- return WindowsAPIHelper.SendMessage(hwndListView, 4127u, 0, 0);
- }
- public int GetRowCount(int hwndListView)
- {
- return WindowsAPIHelper.SendMessage(hwndListView, 4100u, 0, 0);
- }
- public int GetColumnCount(int hwndHeader)
- {
- return WindowsAPIHelper.SendMessage(hwndHeader, 4608u, 0, 0);
- }
- public List<string> GetColumnsHeaderText(int processHandle, int headerhwnd, int colCount)
- {
- List<string> list = new List<string>();
- uint num = 256u;
- int num2 = WindowsAPIHelper.VirtualAllocEx(processHandle, IntPtr.Zero, (uint)Marshal.SizeOf(typeof(ListViewAPIHelper.HDITEM)), 12288u, 4u);
- int num3 = WindowsAPIHelper.VirtualAllocEx(processHandle, IntPtr.Zero, num, 12288u, 4u);
- for (int i = 0; i < colCount; i++)
- {
- byte[] array = new byte[num];
- ListViewAPIHelper.HDITEM hDITEM = new ListViewAPIHelper.HDITEM();
- hDITEM.mask = (uint)this.HDI_TEXT;
- hDITEM.fmt = 0;
- hDITEM.cchTextMax = (int)num;
- hDITEM.pszText = (IntPtr)num3;
- IntPtr intPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(hDITEM));
- Marshal.StructureToPtr(hDITEM, intPtr, false);
- uint count = 0u;
- bool flag = WindowsAPIHelper.WriteProcessMemory(processHandle, num2, intPtr, Marshal.SizeOf(typeof(ListViewAPIHelper.HDITEM)), ref count);
- WindowsAPIHelper.SendMessage(headerhwnd, 4611u, i, num2);
- WindowsAPIHelper.ReadProcessMemory(processHandle, num3, Marshal.UnsafeAddrOfPinnedArrayElement(array, 0), (int)num, ref count);
- string @string = Encoding.Default.GetString(array, 0, (int)count);
- string text = "";
- string text2 = @string;
- for (int j = 0; j < text2.Length; j++)
- {
- char c = text2[j];
- if (c == '\0')
- {
- break;
- }
- text += c;
- }
- list.Add(text);
- }
- WindowsAPIHelper.VirtualFreeEx(processHandle, num2, 0u, 32768u);
- WindowsAPIHelper.VirtualFreeEx(processHandle, num3, 0u, 32768u);
- return list;
- }
- public string[,] GetItemCellsText(int processHandle, int hwndListView, int rows, int cols)
- {
- string[,] array = new string[rows, cols];
- uint num = 256u;
- int num2 = WindowsAPIHelper.VirtualAllocEx(processHandle, IntPtr.Zero, (uint)Marshal.SizeOf(typeof(ListViewAPIHelper.HDITEM)), 12288u, 4u);
- int num3 = WindowsAPIHelper.VirtualAllocEx(processHandle, IntPtr.Zero, num, 12288u, 4u);
- for (int i = 0; i < rows; i++)
- {
- for (int j = 0; j < cols; j++)
- {
- byte[] array2 = new byte[num];
- ListViewAPIHelper.LVITEM lVITEM = default(ListViewAPIHelper.LVITEM);
- lVITEM.mask = this.LVIF_TEXT;
- lVITEM.iItem = i;
- lVITEM.iSubItem = j;
- lVITEM.cchTextMax = (int)num;
- lVITEM.pszText = (IntPtr)num3;
- IntPtr intPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(lVITEM));
- Marshal.StructureToPtr(lVITEM, intPtr, false);
- uint count = 0u;
- WindowsAPIHelper.WriteProcessMemory(processHandle, num2, intPtr, Marshal.SizeOf(typeof(ListViewAPIHelper.LVITEM)), ref count);
- WindowsAPIHelper.SendMessage(hwndListView, 4141u, i, num2);
- if (j == 1)
- {
- WindowsAPIHelper.SendMessage2((IntPtr)hwndListView, 0xC, i, "LIU");
- }
- WindowsAPIHelper.ReadProcessMemory(processHandle, num3, Marshal.UnsafeAddrOfPinnedArrayElement(array2, 0), array2.Length, ref count);
- string @string = Encoding.Default.GetString(array2, 0, (int)count);
- array[i, j] = @string;
- }
- }
- WindowsAPIHelper.VirtualFreeEx(processHandle, num2, 0u, 32768u);
- WindowsAPIHelper.VirtualFreeEx(processHandle, num3, 0u, 32768u);
- return array;
- }
- public static void DoCatch(int hwnd, ListView LV)
- {
- LV.Columns.Clear();
- LV.Items.Clear();
- ListViewAPIHelper listViewAPIHelper = new ListViewAPIHelper();
- int headerHwnd = listViewAPIHelper.GetHeaderHwnd(hwnd);
- int rowCount = listViewAPIHelper.GetRowCount(hwnd);
- int columnCount = listViewAPIHelper.GetColumnCount(headerHwnd);
- int processId = listViewAPIHelper.GetProcessId(hwnd);
- int processHandle = listViewAPIHelper.InjectProcess(processId);
- List<string> columnsHeaderText = listViewAPIHelper.GetColumnsHeaderText(processHandle, headerHwnd, columnCount);
- for (int i = 0; i < columnsHeaderText.Count; i++)
- {
- string text = i.ToString();
- if (!string.IsNullOrEmpty(columnsHeaderText[i]))
- {
- text = columnsHeaderText[i];
- }
- LV.Columns.Add(text);
- }
- string[,] itemCellsText = listViewAPIHelper.GetItemCellsText(processHandle, hwnd, rowCount, columnCount);
- string[] array = new string[columnCount];
- for (int i = 0; i < rowCount; i++)
- {
- for (int j = 0; j < columnCount; j++)
- {
- array[j] = itemCellsText[i, j];
- }
- ListViewItem value = new ListViewItem(array);
- LV.Items.Add(value);
- }
- for (int i = 0; i < columnsHeaderText.Count; i++)
- {
- ColumnHeader columnHeader = LV.Columns[i];
- columnHeader.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
- }
- }
- }
- }
|