读取其他应用措施中ListView中的内容
//---------------------------------------------------------------------------
void MyGetListViewItem(HWND hWindow,TStrings *strlist)
{
const nMaxLen=1023;
char szBuf[nMaxLen+1];
int nLVItemCount;
DWORD dwProcessID;
HANDLE hProcess;
LVITEM lvItemLocal;
DWORD dwBytesRead, dwBytesWrite;
bool bSuccess,bWriteOK;
//留意:本文来自www.ccrun.com,by ccrun(老妖),转载请注明出处。
//为防备某些不认真任的转载者,故出此下策,在代码中插手声明,请各人原谅。
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);
strlist->Add("Welcome to www.ccrun.com");
strlist->Add("ListView的Item总数: " + String(nLVItemCount));
strlist->Add("---------------------------");
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(AnsiString(szBuf));
}//end of for(i)
//在指定历程内释放存储空间
VirtualFreeEx(hProcess,lpListItemRemote,0,MEM_RELEASE);
VirtualFreeEx(hProcess,lpTextRemote,0,MEM_RELEASE);
//封锁指定历程句柄
CloseHandle(hProcess);
}
挪用的时候这样:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
MyGetListViewItem((void *)0x000100DC,Memo1->Lines);
}
上面的((void *)0x000100DC是我用MiniSpy取得的桌面的名柄(win2000下的桌面是个ListView),在实际挪用时换成你取得的ListView的句柄即可。而且在上面的这段代码只是获取ListView的Item的Caption的。
如
HWND hListView;
//….获取ListView的句柄
MyGetListViewItem(hListView,Memo1->Lines);
假如其ViewStyle为vsReport,就要思量读取ListView的HeadItem了。呵呵。GoodLuck.