C#通过Win32 API获取外部程序中SysTreeView32和ListView32的值,结果为空,这是为什么
小弟我用C#.NET通过Win32 API获取外部程序中SysTreeView32和ListView32的值,结果为空,这是为什么?
这是我从网上找来的代码,运行结果获取到的值为空
请大神们帮忙指点迷津。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace GetListViewTreeViewValue
{
public class ListViewWindowsAPIHelper
{
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;
ListViewWindowsAPIHelper.GetWindowThreadProcessId(hwnd, out result);
return result;
}
public int InjectProcess(int processId)
{
return ListViewWindowsAPIHelper.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")]
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 : ListViewWindowsAPIHelper
{
private struct LVITEM
{
public int mask;
public int iItem;
public int iSubItem;
public int state;
public int stateMask;
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 ListViewWindowsAPIHelper.SendMessage(hwndListView, 4127u, 0, 0);
}
public int GetRowCount(int hwndListView)
{
return ListViewWindowsAPIHelper.SendMessage(hwndListView, 4100u, 0, 0);
}
public int GetColumnCount(int hwndHeader)
{
return ListViewWindowsAPIHelper.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 = ListViewWindowsAPIHelper.VirtualAllocEx(processHandle, IntPtr.Zero, (uint)Marshal.SizeOf(typeof(ListViewAPIHelper.HDITEM)), 12288u, 4u);
int num3 = ListViewWindowsAPIHelper.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 = ListViewWindowsAPIHelper.WriteProcessMemory(processHandle, num2, intPtr, Marshal.SizeOf(typeof(ListViewAPIHelper.HDITEM)), ref count);
ListViewWindowsAPIHelper.SendMessage(headerhwnd, 4611u, i, num2);
ListViewWindowsAPIHelper.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);
}
ListViewWindowsAPIHelper.VirtualFreeEx(processHandle, num2, 0u, 32768u);
ListViewWindowsAPIHelper.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 = ListViewWindowsAPIHelper.VirtualAllocEx(processHandle, IntPtr.Zero, (uint)Marshal.SizeOf(typeof(ListViewAPIHelper.HDITEM)), 12288u, 4u);
int num3 = ListViewWindowsAPIHelper.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;
ListViewWindowsAPIHelper.WriteProcessMemory(processHandle, num2, intPtr, Marshal.SizeOf(typeof(ListViewAPIHelper.LVITEM)), ref count);
ListViewWindowsAPIHelper.SendMessage(hwndListView, 4141u, i, num2);
ListViewWindowsAPIHelper.ReadProcessMemory(processHandle, num3, Marshal.UnsafeAddrOfPinnedArrayElement(array2, 0), array2.Length, ref count);
string @string = Encoding.Default.GetString(array2, 0, (int)count);
array[i, j] = @string;
}
}
ListViewWindowsAPIHelper.VirtualFreeEx(processHandle, num2, 0u, 32768u);
ListViewWindowsAPIHelper.VirtualFreeEx(processHandle, num3, 0u, 32768u);
return array;
}
public static void DoCatch(int hwnd, ref 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);
}
}
}
}