#include <iostream>
#include <windows.h>
#include <commctrl.h>
HWND hListview;
int headerhwnd; //listview控件的列头句柄
int rows,cols; //listview控件中的行列数
DWORD ProcessID = NULL;
DWORD ThreadID = NULL;
hListview =...;
//listview的列头句柄
headerhwnd = ::SendMessage(hListview, LVM_GETHEADER, 0, 0);
//总行数:进程的数量
rows = ::SendMessage(hListview,LVM_GETITEMCOUNT,0,0);
//列表列数
cols = ::SendMessage(hListview, HDM_GETITEMCOUNT,0,0);
ThreadID = GetWindowThreadProcessId(hListview,&ProcessID);
//打开并插入进程
hProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE|PROCESS_QUERY_INFORMATION, FALSE,ProcessID);
//申请代码的内存区
pointer = (LVITEM*)VirtualAllocEx(hProcess,NULL, sizeof(LVITEM),MEM_COMMIT, PAGE_READWRITE);
for (int i = 0; i < rows;i++ )
{
m_ProcessList.InsertItem(i,_T(""));
for (int j = 0; j < cols;j++ )
{
LVITEM vItem;
vItem.mask = LVIF_TEXT; //说明pszText是有效的
vItem.iItem = i; //行号
vItem.iSubItem = j; //列号
vItem.cchTextMax = 512; //所能存储的最大的文本为256字节
LPWSTR pItem = NULL;
//申请内存空间
pItem = (LPWSTR)VirtualAllocEx(hProcess, NULL, 512, MEM_COMMIT, PAGE_READWRITE);
vItem.pszText = pItem;
WriteProcessMemory( hProcess , pointer , &vItem , sizeof(LVITEM),NULL);
::SendMessage(hListview, LVM_GETITEMW,(WPARAM)i,(LPARAM)pointer);
char ItemBuf[512];
memset(ItemBuf,0,512);
ReadProcessMemory(hProcess, pItem, ItemBuf, 512, NULL);
CString str;
str.Format(_T("%s"),ItemBuf);
m_ProcessList.SetItemText(i,j,str);
//释放内存空间
VirtualFreeEx(hProcess, pItem, 0, MEM_RELEASE);
}
}
//释放内存空间
VirtualFreeEx(hProcess, pointer, 0, MEM_RELEASE);//在其它进程中释放申请的虚拟内存空间,MEM_RELEASE方式很彻底,完全回收
CloseHandle(hProcess);//关闭打开的进程对象
}
m_ProcessList.InsertItem(i,_T("")) 不知如何获取m_ProcessList
m_ProcessList.SetItemText(i,j,str) 不知如何获取m_ProcessList
VC跨进程获取ListView控件中的内容
于 2024-10-04 21:55:01 首次发布