读取其他应用程序中ListView中的内容
关键字:读取ListView,ListView_GetItem,WriteProcessMemory,VirtualAllocEx
作者:ccrun 更新:2003-11-14 浏览:9893
//---------------------------------------------------------------------------
// 读取ListView中的Item
// hWindow为目标ListView的句柄
// strlist用来存放ListView的Item字符串
// 来自
// by ccrun(老妖)
//---------------------------------------------------------------------------
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;
//注意:本文来自,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 ");
strlist->Add("ListView的Item总数: " + String(nLVItemCount));
strlist->Add("---------------------------");
for(int i=0;i
{
ZeroMemory(szBuf,nMaxLen+1);
bWriteOK= WriteProcessMemory(hProcess,lpTextRemote,(LPVOID)szBuf,nMaxLen+1,(LPDWORD)&dwBytesWrite);
// 本文转自 C++Builder研究 - /article.asp?i=583&d=eahk4z
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);
阅读(780) | 评论(0) | 转发(0) |