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);
}
}
}
}
...全文
444 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
  • 打赏
  • 举报
回复
……这么长代码,拿到这估计没几个会好好看,希望楼主自己先做调试,遇到问题分析问题,不要一股脑扔出一堆代码来求解。
代码转载自:https://pan.quark.cn/s/8ce4326d996e 对于在 CentOS 7 系统修改网卡配置文件后无法使设置生效的情况,经过实践验证,可以通过使用 nmcli 命令来进行调整。完成修改之后,需要重新启动虚拟机以使更改生效,这样操作流程即告完成。如果设置仍然无法生效,则表明虚拟机在启动过程获取的 IP 地址配置并非针对 eth0,此时可以对其它网卡的配置文件进行修改或将其移除。在 CentOS 7 系统,网络配置的管理机制与早期版本存在差异,主要体现为采用了 Network Manager 服务来负责网络接口的管理。在某些情形下,尽管修改了 `/etc/sysconfig/network-scripts` 目录下的 `ifcfg-eth0` 文件,但网络配置却未能即时生效。此类问题的发生通常源于 CentOS 7 采用了不同于以往的配置读取方法。接下来将具体阐述如何借助 nmcli 命令来处理这一挑战。 以 root 用户身份登录系统并打开终端界面。nmcli 是 Network Manager 提供的命令行界面工具,它支持在命令行环境下执行网络连接的建立、编辑、查询及管理任务。针对修改 eth0 网卡配置的需求,可以遵循以下步骤进行操作: 1. 导航至 `/etc/sysconfig/network-scripts` 目录: ``` cd /etc/sysconfig/network-scripts ``` 2. 检查该目录内是否存在 `ifcfg-eth0.bak` 文件,该备份文件可能是先前调整配置时遗留下来的,若存在可能造成冲突。若发现该文件,可以选择将其删除: ``` [root@localhost netw...
代码转载自:https://pan.quark.cn/s/46fd08fb879c 网管教程 从入门到精通软件篇 ★一。★详尽的xp修复控制台指令及其应用!!! 放入xp(2000)的光盘,安装时选择R,执行修复! Windows XP(涵盖 Windows 2000)的控制台指令是在系统遭遇某些意外状况时的一种极具效用的诊断、检测以及恢复系统功能的工具。笔者确实一直期望能够将这方面的指令进行归纳,此次由老范辛苦整理了这份极具价的秘籍。 Bootcfg bootcfg 命令用于启动配置与故障恢复(对大多数计算机而言,即 boot.ini 文件)。 带有特定参数的 bootcfg 命令仅在运用故障恢复控制台时方可使用。能够在命令行界面下运用带有不同参数的 bootcfg 命令。 用法: bootcfg /default 设定默认引导选项。 bootcfg /add 向引导清单增添 Windows 安装。 bootcfg /rebuild 重复整个 Windows 安装流程并让用户选择需添加的项目。 注意:运用 bootcfg /rebuild 之前,应先借助 bootcfg /copy 命令备份 boot.ini 文件。 bootcfg /scan 探查用于 Windows 安装的全部磁盘并展示结果。 注意:这些结果被静态存储,并用于当前会话。若在当前会话期间磁盘配置发生变动,为获取更新的探查结果,必须先重启计算机,然后再次探查磁盘。 bootcfg /list 列示引导清单已有的项目。 bootcfg /disableredirect 在启动引导程序禁用重定向。 bootcfg /redirect [ PortBaudRrate] |[ useBio...

111,131

社区成员

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

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

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