LVM_GETITEMCOUNT后得到了行数,但读取内容,显示为空啊,再怎么弄啊?

laserhz 2008-04-08 04:31:45
按照老师zswang 的指导,在C#里读取个C的老程序内容,LVM_GETITEMCOUNT后得到了行数,但读取内容,显示为空啊。
请老师继续指导啊!谢谢啊!
代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace ReadProcessMemoryWenhua
{
public partial class Form1 : Form
{
public const uint LVM_FIRST = 0x1000;
public const uint LVM_GETITEMCOUNT = LVM_FIRST + 4;
public const uint LVM_GETITEMW = LVM_FIRST + 75;

[DllImport("user32.DLL")]
public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
[DllImport("user32.DLL")]
public static extern IntPtr FindWindow(string lpszClass, string lpszWindow);
[DllImport("user32.DLL")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent,
IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll")]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd,
out uint dwProcessId);

public const uint PROCESS_VM_OPERATION = 0x0008;
public const uint PROCESS_VM_READ = 0x0010;
public const uint PROCESS_VM_WRITE = 0x0020;

[DllImport("kernel32.dll")]
public static extern IntPtr OpenProcess(uint dwDesiredAccess,
bool bInheritHandle, uint dwProcessId);
public const uint MEM_COMMIT = 0x1000;
public const uint MEM_RELEASE = 0x8000;

public const uint MEM_RESERVE = 0x2000;
public const uint PAGE_READWRITE = 4;

[DllImport("kernel32.dll")]
public static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress,
uint dwSize, uint flAllocationType, uint flProtect);

[DllImport("kernel32.dll")]
public static extern bool VirtualFreeEx(IntPtr hProcess, IntPtr lpAddress,
uint dwSize, uint dwFreeType);

[DllImport("kernel32.dll")]
public static extern bool CloseHandle(IntPtr handle);

[DllImport("kernel32.dll")]
public static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,
IntPtr lpBuffer, int nSize, ref uint vNumberOfBytesRead);

[DllImport("kernel32.dll")]
public static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,
IntPtr lpBuffer, int nSize, ref uint vNumberOfBytesRead);

public struct LVITEM
{
public int mask;
public int iItem;
public int iSubItem;
public int state;
public int stateMask;
public IntPtr pszText; // string
public int cchTextMax;
public int iImage;
public IntPtr lParam;
public int iIndent;
public int iGroupId;
public int cColumns;
public IntPtr puColumns;
}
public int LVIF_TEXT = 0x0001;

public int ListView_GetItemCount(IntPtr AHandle)
{
return SendMessage(AHandle, LVM_GETITEMCOUNT, 0, 0);
}

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

// IntPtr vHandle = FindWindow("#32770", "Windows 任务管理器");
IntPtr vHandle = FindWindow("Afx:400000:8:10011:0:70c01", null);

MessageBox.Show("vHandle = " + Convert.ToString(vHandle));


//增加中间窗口
vHandle = FindWindowEx(vHandle, IntPtr.Zero, "MDIClient", null);
MessageBox.Show("MDIClient =" + Convert.ToString(vHandle));

vHandle = FindWindowEx(vHandle, IntPtr.Zero, "Afx:400000:b:10011:6:1d8b021b", null);
MessageBox.Show("Afx:400000:b:10011:6:1d8b021b =" + Convert.ToString(vHandle));


// vHandle = FindWindowEx(vHandle, IntPtr.Zero, "#32770", null);
vHandle = FindWindowEx(vHandle, IntPtr.Zero, "AfxFrameOrView42", null);

MessageBox.Show("AfxFrameOrView42 vHandle =" + Convert.ToString(vHandle));


// vHandle = FindWindowEx(vHandle, IntPtr.Zero, "SysListView32", null);
vHandle = FindWindowEx(vHandle, IntPtr.Zero, "SysListView32", null);
Int32 vHandleList = 0x00010F68;
MessageBox.Show("SysListView32= " + Convert.ToString(vHandle));

if (vHandle == IntPtr.Zero) return;
// int vItemCount = ListView_GetItemCount(vHandle);
int vItemCount = ListView_GetItemCount((IntPtr ) vHandleList);

MessageBox.Show("vItemcount = " + Convert.ToString(vItemCount));

uint vProcessId;
GetWindowThreadProcessId(vHandle, out vProcessId);
MessageBox.Show("vProcessId = " + Convert.ToString(vProcessId));

IntPtr vProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ |
PROCESS_VM_WRITE, false, vProcessId);
MessageBox.Show("vProcess = " + Convert.ToString(vProcess));

IntPtr vPointer = VirtualAllocEx(vProcess, IntPtr.Zero, 4096,
MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
try
{
for (int i = 0; i < vItemCount; i++)
{
byte[] vBuffer = new byte[256];
LVITEM[] vItem = new LVITEM[1];
vItem[0].mask = LVIF_TEXT;
vItem[0].iItem = i;
vItem[0].iSubItem = 0;
vItem[0].cchTextMax = vBuffer.Length;
vItem[0].pszText = (IntPtr)((int)vPointer + Marshal.SizeOf(typeof(LVITEM)));
uint vNumberOfBytesRead = 0;

WriteProcessMemory(vProcess, vPointer,
Marshal.UnsafeAddrOfPinnedArrayElement(vItem, 0),
Marshal.SizeOf(typeof(LVITEM)), ref vNumberOfBytesRead);
SendMessage(vHandle, LVM_GETITEMW, i, vPointer.ToInt32());
ReadProcessMemory(vProcess,
(IntPtr)((int)vPointer + Marshal.SizeOf(typeof(LVITEM))),
Marshal.UnsafeAddrOfPinnedArrayElement(vBuffer, 0),
vBuffer.Length, ref vNumberOfBytesRead);

string vText = Marshal.PtrToStringUni(
Marshal.UnsafeAddrOfPinnedArrayElement(vBuffer, 0));
//Console.WriteLine(vText);
MessageBox.Show(vText);
}
}
finally
{
VirtualFreeEx(vProcess, vPointer, 0, MEM_RELEASE);
CloseHandle(vProcess);
}
}
}
}



...全文
629 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
knowledge_Is_Life 2008-04-30
  • 打赏
  • 举报
回复
不会,帮顶
曲滨_銘龘鶽 2008-04-14
  • 打赏
  • 举报
回复
没有实际环境没法测试;

13,190

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 分析与设计
社区管理员
  • 分析与设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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