//---------------------------------------------------------------------------
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.