2 hechuan2646 hechuan2646 于 2013.06.11 11:30 提问

关于MFC读取外部程序的ListControl内容
mfc

MFC程序读取其他应用程序的Listcontrol内容时,只能读取到目标软件Listcontrol的部分列内容。但是读取任务管理器时,是正常的,我也尝试过读取一些其他软件,都是正常的。代码是我在网上查的比较经典的一种方法。代码如下:

DWORD pid;
HANDLE process;
LVITEM lvi, *_lvi;
char firstItem[512], secondItem[512], thirdItem[512], fourthItem[512], fifthItem[512], sixthItem[512], seventhItem[512], eighthItem[512], ninthItem[512];
char *_firstItem, *_secondItem, *_thirdItem, *_fourthItem, *_fifthItem, *_sixthItem, *_seventhItem, *_eighthItem, *_ninthItem;

memset(firstItem, 0, 512);
memset(secondItem, 0, 512);
memset(thirdItem, 0, 512);
memset(fourthItem, 0, 512);
memset(fifthItem, 0, 512);
memset(sixthItem, 0, 512);
memset(seventhItem, 0, 512);
memset(eighthItem, 0, 512);
memset(ninthItem, 0, 512);

// 获取窗口PID、打开进程
GetWindowThreadProcessId(hWnd, &pid);
process = OpenProcess( PROCESS_ALL_ACCESS, TRUE, pid);

if(!process)
return FALSE;

// 申请内存空间
_lvi = (LVITEM*)VirtualAllocEx(process, NULL, sizeof(LVITEM), MEM_COMMIT, PAGE_EXECUTE_READWRITE);
_firstItem = (char*)VirtualAllocEx(process, NULL, 512 + 1, MEM_COMMIT,PAGE_READWRITE);

_secondItem = (char*)VirtualAllocEx(process, NULL, 512 + 1, MEM_COMMIT,PAGE_READWRITE);

_thirdItem = (char*)VirtualAllocEx(process, NULL, 512 + 1, MEM_COMMIT,PAGE_READWRITE);

_fourthItem = (char*)VirtualAllocEx(process, NULL, 512 + 1, MEM_COMMIT,PAGE_READWRITE);

_fifthItem = (char*)VirtualAllocEx(process, NULL, 512 + 1, MEM_COMMIT,PAGE_READWRITE);

_sixthItem = (char*)VirtualAllocEx(process, NULL, 512 + 1, MEM_COMMIT,PAGE_READWRITE);
_seventhItem = (char*)VirtualAllocEx(process, NULL, 512 + 1, MEM_COMMIT,PAGE_READWRITE);
_eighthItem = (char*)VirtualAllocEx(process, NULL, 512 + 1, MEM_COMMIT,PAGE_READWRITE);
_ninthItem = (char*)VirtualAllocEx(process, NULL, 512 + 1, MEM_COMMIT,PAGE_READWRITE);

lvi.cchTextMax = 512;
CString strTemp1;
for(i=0; i<count; i++)
{

// 获取第一列记录
lvi.iSubItem = 0;
lvi.pszText = _firstItem;
WriteProcessMemory(process, _lvi, &lvi, sizeof(LVITEM), NULL);
::SendMessage(hWnd, LVM_GETITEMTEXT, (WPARAM)i, (LPARAM)_lvi);

// 获取第二列记录
lvi.iSubItem = 1;
lvi.pszText = _secondItem;
WriteProcessMemory(process, _lvi, &lvi, sizeof(LVITEM), NULL);
::SendMessage(hWnd, LVM_GETITEMTEXT, (WPARAM)i, (LPARAM)_lvi);

// 获取第三列记录
lvi.iSubItem = 2;
lvi.pszText = _thirdItem;
WriteProcessMemory(process, _lvi, &lvi, sizeof(LVITEM), NULL);
::SendMessage(hWnd, LVM_GETITEMTEXT, (WPARAM)i, (LPARAM)_lvi);

// 获取第四列记录
lvi.iSubItem = 3;
lvi.pszText = _fourthItem;
WriteProcessMemory(process, _lvi, &lvi, sizeof(LVITEM), NULL);
::SendMessage(hWnd, LVM_GETITEMTEXT, (WPARAM)i, (LPARAM)_lvi);

// 获取第五列记录
lvi.iSubItem = 4;
lvi.pszText = _fifthItem;
WriteProcessMemory(process, _lvi, &lvi, sizeof(LVITEM), NULL);
::SendMessage(hWnd, LVM_GETITEMTEXT, (WPARAM)i, (LPARAM)_lvi);

// 获取第六列记录
lvi.iSubItem = 5;
lvi.pszText = _sixthItem;
WriteProcessMemory(process, _lvi, &lvi, sizeof(LVITEM), NULL);
::SendMessage(hWnd, LVM_GETITEMTEXT, (WPARAM)i, (LPARAM)_lvi);

// 获取第7列记录
lvi.iSubItem = 6;
lvi.pszText = _seventhItem;
WriteProcessMemory(process, _lvi, &lvi, sizeof(LVITEM), NULL);
::SendMessage(hWnd, LVM_GETITEMTEXT, (WPARAM)i, (LPARAM)_lvi);

// 获取第8列记录
lvi.iSubItem = 7;
lvi.pszText = _eighthItem;
WriteProcessMemory(process, _lvi, &lvi, sizeof(LVITEM), NULL);
::SendMessage(hWnd, LVM_GETITEMTEXT, (WPARAM)i, (LPARAM)_lvi);

// 获取第9列记录
lvi.iSubItem = 8;
lvi.pszText = _ninthItem;
WriteProcessMemory(process, _lvi, &lvi, sizeof(LVITEM), NULL);
::SendMessage(hWnd, LVM_GETITEMTEXT, (WPARAM)i, (LPARAM)_lvi);

// 读取进程内存

ReadProcessMemory(process, _firstItem, firstItem, 512, NULL);
ReadProcessMemory(process, _secondItem, secondItem, 512, NULL);
ReadProcessMemory(process, _thirdItem, thirdItem, 512, NULL);
ReadProcessMemory(process, _fourthItem, fourthItem, 512, NULL);
ReadProcessMemory(process, _fifthItem, fifthItem, 512, NULL);
ReadProcessMemory(process, _sixthItem, sixthItem, 512, NULL);
ReadProcessMemory(process, _seventhItem, seventhItem, 512, NULL);
ReadProcessMemory(process, _eighthItem, eighthItem, 512, NULL);
ReadProcessMemory(process, _ninthItem, ninthItem, 512, NULL);

    // 格式化字符串   

str1.Format("%s, %s, %s, %s, %s, %s, %s, %s, %s.",firstItem, secondItem, thirdItem, fourthItem, fifthItem, sixthItem, seventhItem, eighthItem, ninthItem);
AfxMessageBox(str1);

}

// 释放内存空间

VirtualFreeEx(process, _lvi, 0, MEM_RELEASE);

VirtualFreeEx(process, _firstItem, 0, MEM_RELEASE);

VirtualFreeEx(process, _secondItem, 0, MEM_RELEASE);

VirtualFreeEx(process, _thirdItem, 0, MEM_RELEASE);

VirtualFreeEx(process, _fourthItem, 0, MEM_RELEASE);

VirtualFreeEx(process, _fifthItem, 0, MEM_RELEASE);
VirtualFreeEx(process, _sixthItem, 0, MEM_RELEASE);
VirtualFreeEx(process, _seventhItem, 0, MEM_RELEASE);
VirtualFreeEx(process, _eighthItem, 0, MEM_RELEASE);
VirtualFreeEx(process, _ninthItem, 0, MEM_RELEASE);

贴图是我读取windows任务管理器的结果(正常)和目标程序的结果(只能读取到一列)

求高手知道,交流。也可以加我QQ进行交流指导。QQ:315482682

图片贴不上来,可到空间去看,谢谢啦

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!