//
// http://www.codeproject.com/KB/threads/int64_memsteal.aspx
//
HANDLE InjectProcess(HWND listview)
{
unsigned long pid;
HANDLE process;
GetWindowThreadProcessId(listview, &pid);
process=OpenProcess(PROCESS_VM_OPERATION|PROCESS_VM_READ|
PROCESS_VM_WRITE|PROCESS_QUERY_INFORMATION, FALSE, pid);
return process;
}
void* MallocInPocess(HANDLE process, size_t size, void *def_val=NULL)
{
void* p = VirtualAllocEx(process, NULL, size,
MEM_COMMIT, PAGE_READWRITE);
if(def_val)
VERIFY(WriteProcessMemory(process, p, def_val, size, NULL));
return p;
}
void FreeInPocess(HANDLE process, void* _lvi)
{
VirtualFreeEx(process, _lvi, 0, MEM_RELEASE);
}
CString CListCtrl_GetItemText(HWND m_hWnd, HANDLE hProcess, int nItem, int nSubItem)
{
ASSERT(::IsWindow(m_hWnd));
CString s;
//
// lvi
//
LVITEM lvi;
memset(&lvi, 0, sizeof(LVITEM));
lvi.iSubItem = nSubItem;
const int nLen = 1280;
int nRes;
lvi.cchTextMax = nLen;
//memory
TCHAR *str = (TCHAR*)MallocInPocess(hProcess, nLen*sizeof(TCHAR));
if(!str)
return s;
lvi.pszText = str;
LVITEM *plvi = (LVITEM*)MallocInPocess(hProcess, sizeof(LVITEM), &lvi);
if(!plvi)
return s;
nRes = (int)::SendMessage(m_hWnd, LVM_GETITEMTEXT, (WPARAM)nItem,
(LPARAM)plvi);
TCHAR str2[nLen];
VERIFY(ReadProcessMemory(hProcess, str, str2, 512, NULL));
s = str2;
FreeInPocess(hProcess, plvi);
FreeInPocess(hProcess, str);
return s;
}
void Ctest2Dlg::OnBnClickedButton1()
{
HWND hMain = ::FindWindowEx(NULL,NULL,NULL,_T("test1"));
HWND hList = ::FindWindowEx(hMain, NULL , _T("SysListView32"), NULL);
ASSERT(hList);
CListCtrl List;
VERIFY(List.Attach(hList));
int nItem = List.GetItemCount();
int nCol = List.GetHeaderCtrl()->GetItemCount();
HANDLE hPoc = InjectProcess(hList);
for(int i=0; i<nItem; i++)
{
for(int j=0; j<nCol; j++)
{
CString szText = CListCtrl_GetItemText(hList,hPoc,i,j);
TRACE("%s\n", szText);
MessageBox(szText);
}
}
List.Detach();
}