gpt4 book ai didi

c# - 如何使用 ReadProcessMemory 获取托盘按钮文本

转载 作者:太空宇宙 更新时间:2023-11-03 11:13:33 25 4
gpt4 key购买 nike

我想用 C# 获取 windows7(64 位)上的所有托盘图标,但是当我使用 windows api“ReadProcessMemory”时,无法识别托盘按钮文本。下面的代码

        IntPtr pid = IntPtr.Zero;
IntPtr ipHandle = IntPtr.Zero;
IntPtr lTextAdr = IntPtr.Zero;

IntPtr ipTray = TrayToolbarWindow32();

WinApiHelper.GetWindowThreadProcessId(ipTray, ref pid);
if (pid.Equals(0))
return iconList;

IntPtr hProcess = WinApiHelper.OpenProcess(WinApiHelper.PROCESS_ALL_ACCESS | WinApiHelper.PROCESS_VM_OPERATION | WinApiHelper.PROCESS_VM_READ | WinApiHelper.PROCESS_VM_WRITE, IntPtr.Zero, pid);

IntPtr lAddress = WinApiHelper.VirtualAllocEx(hProcess, 0, 4096, WinApiHelper.MEM_COMMIT, WinApiHelper.PAGE_READWRITE);


int lButton = WinApiHelper.SendMessage(ipTray, WinApiHelper.TB_BUTTONCOUNT, 0, 0);

for (int i = 0; i < lButton; i++)
{

WinApiHelper.SendMessage(ipTray, WinApiHelper.TB_GETBUTTON, i, lAddress);


WinApiHelper.ReadProcessMemory(hProcess, (IntPtr)(lAddress.ToInt32() + 16), ref lTextAdr, 4, 0);

if (!lTextAdr.Equals(-1))
{
byte[] buff = new byte[ 1024 ];

WinApiHelper.ReadProcessMemory(hProcess, lTextAdr, buff, 1024, 0);
string title = System.Text.ASCIIEncoding.Unicode.GetString(buff);

和api声明

    [DllImport("kernel32", EntryPoint = "ReadProcessMemory")]
public static extern int ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, ref IntPtr lpBuffer, int nSize, int lpNumberOfBytesWritten);

[DllImport("kernel32.dll", EntryPoint = "ReadProcessMemory")]
public static extern bool ReadProcessMemoryEx(IntPtr hProcess, IntPtr lpBaseAddress, [In, Out] byte[] buffer, IntPtr size, out IntPtr lpNumberOfBytesRead);

[DllImport("kernel32", EntryPoint = "ReadProcessMemory")]
public static extern int ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [In, Out] byte[] lpBuffer, int nSize, int lpNumberOfBytesWritten);

[DllImport("kernel32.dll")]
public static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, IntPtr lpBuffer, UIntPtr nSize, IntPtr lpNumberOfBytesRead);

问题就在这里

string title = System.Text.ASCIIEncoding.Unicode.GetString(buff);

转换后无法识别字符串“title”,可能是这样

ǎ\0\0\0\0Д\0\0à\0\0ƿ\r\0\0\0\0\0\0\0\0\0\0D:\\Tools\\ESET Smart Security\\egui.exe\0\0\0\0\0\0\0\0\0\0\0\0\0\

不知道为什么,求助

最佳答案

您可能需要考虑一下您在做什么。 ReadProcessMemory 是为需要SeDebugPrivilege 的调试器设计的调试函数,所以我希望你正在编写一个调试器。忽略我在考虑以非调试能力使用这些函数时产生的不适,你正在泄漏你分配的缓冲区并要求你的应用程序以管理员身份运行。

如果此应用程序仅用于您自己的目的而不是其他任何人,请查看相关问题 Systray Access ,因为似乎有一个单独的 TB_GETBUTTONTEXT 消息。我想您实际上是在接收按钮数据,而不是复制内存中的文本,这会导致问题。

关于c# - 如何使用 ReadProcessMemory 获取托盘按钮文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13323454/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com