C#通过Win32 API获取外部程序中SysTreeView32和ListView32的值,结果为空,这是为什么

syoukinsan 2017-03-01 08:30:36
小弟我用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);
}
}
}
}
...全文
373 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
syoukinsan 2017-03-01
  • 打赏
  • 举报
回复
引用 3 楼 xuggzu 的回复:
感觉楼主还是没明白我的意思。 调试分布进行,比如:openprocess返回是否正常(需要提权的,否则之后操作会失败),如果不正常,后续操作全部无用啊,此时就要在这里找原因,……,后续调试一样,如此一步步调……
谢大神指点,我也是刚用Win32 API开发,代码也是从网上找来,没有能理解意思。 接下来我好好调试和理解每一句代码的意思。
xuggzu 2017-03-01
  • 打赏
  • 举报
回复
感觉楼主还是没明白我的意思。 调试分布进行,比如:openprocess返回是否正常(需要提权的,否则之后操作会失败),如果不正常,后续操作全部无用啊,此时就要在这里找原因,……,后续调试一样,如此一步步调……
syoukinsan 2017-03-01
  • 打赏
  • 举报
回复
引用 1 楼 xuggzu 的回复:
……这么长代码,拿到这估计没几个会好好看,希望楼主自己先做调试,遇到问题分析问题,不要一股脑扔出一堆代码来求解。



我做了调试了,获取到的值是一大堆\0,我实现是想不到什么方法了。所以把完整的代码贴出来了。
大神如果能解决的话,我给大神另外发红包。
xuggzu 2017-03-01
  • 打赏
  • 举报
回复
……这么长代码,拿到这估计没几个会好好看,希望楼主自己先做调试,遇到问题分析问题,不要一股脑扔出一堆代码来求解。

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧