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