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);
}
}
}
}
...全文
457 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/a4b39357ea24 双系统引导修复机制是为那些在个人计算机上部署了两个操作系统(例如Windows与Linux、Windows的不同版本组合等)的用户群体量身定制的辅助程序。在常规操作过程中,由于某些因素的影响,比如系统更新、驱动程序安装或意外操作,可能会导致双系统的启动选择界面出现故障,无法正常选择需要启动的操作系统。在此情形下,需要一款操作极为简便的双系统启动菜单自动修正工具来处理这一挑战。 标题中所指的“双系统引导修复机制”指的是这一工具的核心作用,它专门致力于修复引导区域或MBR(主引导记录),而这两个部分是计算机在启动时用来加载操作系统的核心组件。引导修复过程通常包括对BIOS或UEFI设置进行校准,以及修复或重建BCD(启动配置数据)存储,这在Windows操作系统环境中显得尤为重要。 描述中提及的“操作简便修复”表明该工具在设计上非常注重易用性,无需用户拥有专业的计算机技术背景。用户只需遵循基础的步骤指引,即可自动完成启动菜单的修正任务,显著简化了故障诊断的流程。 标签“双系统启动菜单自动修复工具”进一步突出了该软件的主要职能,即解决因各种原因引发的双系统启动菜单失效状况。此类工具通常会对已安装的操作系统进行扫描,识别出所有可用的启动选项,并重新构建准确的引导菜单,使用户能够在启动阶段自由选择要执行的系统。 关于压缩文件内的“bootitng”文件,很可能是一个用于实施引导修复流程的程序组件。BootIt Next Generation(BING)是一个广为人知的引导管理解决方案,其部分模块可能以“bootitng”命名,负责处理启动项的管理与修复工作。该文件在执行修复过程中具有核...
内容概要:本文围绕2026年“华为杯”全国研究生数学建模竞赛B题——氢燃料电池低温冷启动建模与控制策略研究,提供系统性的解题思路、MATLAB与Python代码实现及论文撰写支持。内容涵盖一维单电池瞬态自冷启动模型构建、电堆自冷启动策略优化、辅助冷启动建模以及动态加热控制策略的设计与求解,深入分析多物理场耦合关系与模型基础,针对关键难点提出有效的求解路径。资源持续更新,包含详细的运行结果展示与参考文献,旨在为参赛者提供全面的技术指导与实战支持。; 适合人群:参加“华为杯”全国研究生数学建模竞赛,具备一定数学建模能力、编程基础(MATLAB/Python)和控制理论知识的高校学生或科研人员;特别适合对新能源系统建模、低温环境下能量管理与优化控制策略感兴趣的研究者。; 使用场景及目标:①用于高效备战2026年“华为杯”数学建模竞赛B题,快速掌握氢燃料电池在低温条件下的启动过程建模方法与求解技巧;②深入理解燃料电池系统在极端环境中的动态行为特性,开展冷启动过程中的热管理、能量分配与控制策略优化研究;③借助提供的完整代码框架与论文写作模板,完成高质量的仿真验证、结果分析与学术成果输出。; 阅读建议:建议结合文中提供的目录结构,按照问题分解的逻辑顺序逐步学习,重点关注模型构建的理论依据与代码实现的细节逻辑,动手运行并调试程序以加深对算法和控制策略的理解;同时参照配套的论文写作要点,将技术实现过程与学术规范表达有机结合,全面提升竞赛作品的技术深度与呈现质量。

111,128

社区成员

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

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

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