方法一
bool GetListViewText(HWND hWnd, TStrings *list)
{
int vColumnCount;
int vItemCount;
int i, j;
wchar_t vBuffer[255];
DWORD vProcessId;
HANDLE vProcess;
LPVOID vPointer;
DWORD vNumberOfBytesRead;
String S;
TLVItem vItem;
if (!list)
exit;
vColumnCount = Header_GetItemCount(ListView_GetHeader(hWnd));
if (vColumnCount <= 0)
exit;
vItemCount = ListView_GetItemCount(hWnd);
GetWindowThreadProcessId(hWnd, & vProcessId);
vProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE, false, vProcessId);
vPointer = VirtualAllocEx(vProcess, NULL, 4096, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
list->BeginUpdate();
list->Clear();
for (i = 0; i < vItemCount; i++)
{
S = "";
for (j = 0; j < vColumnCount; j++)
{
vItem.mask = LVIF_TEXT;
vItem.iItem = i;
vItem.iSubItem = j;
vItem.cchTextMax = sizeof(vBuffer);
vItem.pszText = (LPTSTR)vPointer;
WriteProcessMemory(vProcess, vPointer, &vItem, sizeof(TLVItem), &vNumberOfBytesRead);
SendMessageA(hWnd, LVM_GETITEM, i, (LPARAM)vPointer);
ReadProcessMemory(vProcess, vPointer, vBuffer, sizeof(vBuffer), & vNumberOfBytesRead);
S = S +char(9) + String(vBuffer);
}
S = S.Delete(1, 1);
list->Add(S);
}
VirtualFreeEx(vProcess, vPointer, 0, MEM_RELEASE);
CloseHandle(vProcess);
list->EndUpdate();
return True;
}
void __fastcall TForm21::Button2Click(TObject * Sender)
{
GetListViewText((HWND)0x00140B24, Memo1->Lines);
}
方法二
void ListGetText(HWND hWnd, int row, int col, wchar_t* pSave, int sz)
{
const int BUFSIZE = 1024;
DWORD dwProcess;
HANDLE hProcess;
wchar_t buf[BUFSIZE];
LVITEM* pItem = (LVITEM*)buf;
pSave[0] = '\0';
GetWindowThreadProcessId(hWnd, &dwProcess);
// 申请内存
hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, dwProcess);
LPLVITEM pTemp = (LPLVITEM)VirtualAllocEx(hProcess, 0, BUFSIZE, MEM_COMMIT, PAGE_READWRITE);
memset(pItem, 0, sizeof(buf));
pItem->pszText = (LPTSTR) pTemp;
pItem->cchTextMax = BUFSIZE -sizeof(LVITEM);
pItem->iSubItem = col; // 列
pItem->mask = LVIF_TEXT;
DWORD dWord = 0;
WriteProcessMemory(hProcess, pTemp, pItem, sizeof(buf), &dWord);
int nChar = SendMessage(hWnd, LVM_GETITEMTEXT, (WPARAM)row, (LPARAM)pTemp);
// printf("ListGetText read char count : %d\n", nChar);
// 读数据
ReadProcessMemory(hProcess, pTemp, pItem, sizeof(buf), &dWord);
pItem->pszText = (LPTSTR) pItem;
// 释放内存
VirtualFreeEx(hProcess, pTemp, BUFSIZE, MEM_RELEASE);
CloseHandle(hProcess);
wsprintf(pSave, L"%s", pItem->pszText);
}
void __fastcall TForm21::Button1Click(TObject * Sender)
{
int row, col;
wchar_t pSave[100];
int sz;
row = 1;
col = 1;
sz = 50;
ListGetText((HWND)0x00140B24, row, col, pSave, sz);
Caption = pSave;
}
方法三 from CCRUN
void MyGetListViewItem(HWND hWindow, TStrings *strlist)
{
const nMaxLen = 1023;
wchar_t szBuf[nMaxLen + 1];
int nLVItemCount;
DWORD dwProcessID;
HANDLE hProcess;
LVITEM lvItemLocal;
DWORD dwBytesRead, dwBytesWrite;
bool bSuccess, bWriteOK;
GetWindowThreadProcessId(hWindow, &dwProcessID);
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcessID);
if (!hProcess) // 得不到指定进程的句柄
return;
// 在指定进程内分配存储空间
LPVOID lpTextRemote = VirtualAllocEx(hProcess, NULL, nMaxLen + 1, MEM_COMMIT, PAGE_READWRITE);
LPVOID lpListItemRemote = VirtualAllocEx(hProcess, NULL, sizeof(LVITEM), MEM_COMMIT, PAGE_READWRITE);
if ((!lpTextRemote) || (!lpListItemRemote)) // 不能在指定进程内分配存储空间
return;
nLVItemCount = ListView_GetItemCount(hWindow);
for (int i = 0; i < nLVItemCount; i++)
{
ZeroMemory(szBuf, nMaxLen + 1);
bWriteOK = WriteProcessMemory(hProcess, lpTextRemote, (LPVOID)szBuf, nMaxLen + 1, (LPDWORD) & dwBytesWrite);
if (!bWriteOK) // 写内存错误
return;
lvItemLocal.iItem = i;
lvItemLocal.iSubItem = 0;
lvItemLocal.mask = LVIF_TEXT;
lvItemLocal.cchTextMax = nMaxLen;
lvItemLocal.pszText = (LPTSTR)lpTextRemote;
dwBytesWrite = 0;
bWriteOK = WriteProcessMemory(hProcess, lpListItemRemote, (LPVOID) & lvItemLocal, sizeof(LVITEM), (LPDWORD) & dwBytesWrite);
if (!bWriteOK) // 写内存错误
return;
SendMessage(hWindow, LVM_GETITEMTEXT, (WPARAM)i, (LPARAM)lpListItemRemote);
bSuccess = ReadProcessMemory(hProcess, lpTextRemote, szBuf, nMaxLen + 1, &dwBytesRead);
// 从指定进程存储空间读取文本
if (!bSuccess) // 不能在指定进程内读取文本
return;
strlist->Add(String(szBuf));
} // end of for(i)
// 在指定进程内释放存储空间
VirtualFreeEx(hProcess, lpListItemRemote, 0, MEM_RELEASE);
VirtualFreeEx(hProcess, lpTextRemote, 0, MEM_RELEASE);
// 关闭指定进程句柄
CloseHandle(hProcess);
}