VC读写其他进程ListCtrl数据到本进程的实例,下面用Windows任务管理器来做测试:
1、捕获窗口句柄:
用SPY++可以看到如下父子窗口关系:
添加ListCtrl,设置style / Report、关联控件变量m_ListCtrl,再添加一个按钮,如下图:
2、相关代码:
- BOOL CTestDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- ......
- // TODO: Add extra initialization here
- // 设置ListCtrl窗口样式
- LONG lStyle = m_ListCtrl.SendMessage(LVM_GETEXTENDEDLISTVIEWSTYLE);
- lStyle |= LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES;
- m_ListCtrl.SendMessage(LVM_SETEXTENDEDLISTVIEWSTYLE, 0,(LPARAM)lStyle);
- // 添加上面的列表头
- m_ListCtrl.InsertColumn(0,"映像名称",NULL,100, -1);
- m_ListCtrl.InsertColumn(1,"PID",NULL,60,-1);
- m_ListCtrl.InsertColumn(2,"用户名",NULL,120,-1);
- m_ListCtrl.InsertColumn(3,"CPU",NULL,60,-1);
- return TRUE; // return TRUE unless you set the focus to a control
- }
- void CTestDlg::OnBtnRead()
- {
- // TODO: Add your control notification handler code here
- m_ListCtrl.DeleteAllItems();
- // 查找窗口句柄
- HWND hWnd,hListview;
- hWnd=::FindWindow(NULL,_T("Windows 任务管理器"));
- hWnd=::FindWindowEx(hWnd,0,"#32770",0);
- hListview=::FindWindowEx(hWnd,0,_T("SysListView32"),NULL);
- if(!hListview)
- {
- MessageBox("Listview handle is NULL !");
- return;
- }
- int count = (int)::SendMessage(hListview,LVM_GETITEMCOUNT,0,0);
- int i,nItem;
- // 获取ListView行数
- CString strTemp;
- strTemp.Format("总共%d行记录",count);
- MessageBox(strTemp);
- CString str1="";
- CString str2="";
- CString str3="";
- CString str4="";
- DWORD pid;
- HANDLE process;
- LVITEM lvi, *_lvi;
- char firstitem[512], secitem[512],thirditem[512],fourthitem[512];
- char *_firstitem, *_secitem,*_thirditem,*_fourthitem;
- memset(firstitem,0,512);
- memset(secitem,0,512);
- memset(thirditem,0,512);
- memset(fourthitem,0,512);
- // 获取窗口PID、打开进程
- GetWindowThreadProcessId(hListview, &pid);
- process=OpenProcess(PROCESS_VM_OPERATION|PROCESS_VM_READ|
- PROCESS_VM_WRITE|PROCESS_QUERY_INFORMATION, FALSE, pid);
- // 申请内存空间
- _lvi=(LVITEM*)VirtualAllocEx(process, NULL, sizeof(LVITEM),MEM_COMMIT, PAGE_READWRITE);
- _firstitem=(char*)VirtualAllocEx(process, NULL, 512, MEM_COMMIT,PAGE_READWRITE);
- _secitem =(char*)VirtualAllocEx(process, NULL, 512, MEM_COMMIT,PAGE_READWRITE);
- _thirditem=(char*)VirtualAllocEx(process, NULL, 512, MEM_COMMIT,PAGE_READWRITE);
- _fourthitem=(char*)VirtualAllocEx(process,NULL, 512, MEM_COMMIT,PAGE_READWRITE);
- lvi.cchTextMax=512;
- // 这里应该递减、保持与任务管理器一样顺序
- for(i=count; i>=0; i--)
- {
- // 获取第一列记录
- lvi.iSubItem=0;
- lvi.pszText=_firstitem;
- WriteProcessMemory(process, _lvi, &lvi, sizeof(LVITEM), NULL);
- ::SendMessage(hListview, LVM_GETITEMTEXT, (WPARAM)i, (LPARAM)_lvi);
- // 获取第二列记录
- lvi.iSubItem=1;
- lvi.pszText=_secitem;
- WriteProcessMemory(process, _lvi, &lvi, sizeof(LVITEM), NULL);
- ::SendMessage(hListview, LVM_GETITEMTEXT, (WPARAM)i, (LPARAM)_lvi);
- // 获取第三列记录
- lvi.iSubItem=2;
- lvi.pszText=_thirditem;
- WriteProcessMemory(process, _lvi, &lvi, sizeof(LVITEM), NULL);
- ::SendMessage(hListview, LVM_GETITEMTEXT, (WPARAM)i, (LPARAM)_lvi);
- // 获取第四列记录
- lvi.iSubItem=3;
- lvi.pszText=_fourthitem;
- WriteProcessMemory(process, _lvi, &lvi, sizeof(LVITEM), NULL);
- ::SendMessage(hListview, LVM_GETITEMTEXT, (WPARAM)i, (LPARAM)_lvi);
- // 读取进程内存
- ReadProcessMemory(process, _firstitem, firstitem, 512, NULL);
- ReadProcessMemory(process, _secitem, secitem, 512, NULL);
- ReadProcessMemory(process, _thirditem, thirditem, 512, NULL);
- ReadProcessMemory(process, _fourthitem, fourthitem, 512, NULL);
- // 格式化字符串
- str1.Format("%s",firstitem);
- str2.Format("%s",secitem);
- str3.Format("%s",thirditem);
- str4.Format("%s",fourthitem);
- // 增加到列表
- nItem = m_ListCtrl.InsertItem(0,str1);
- m_ListCtrl.SetItem(nItem,1,1,str2,NULL,0,0,0);
- m_ListCtrl.SetItem(nItem,2,1,str3,NULL,0,0,0);
- m_ListCtrl.SetItem(nItem,3,1,str4,NULL,0,0,0);
- }
- // 释放内存空间
- VirtualFreeEx(process, _lvi, 0, MEM_RELEASE);
- VirtualFreeEx(process, _firstitem, 0, MEM_RELEASE);
- VirtualFreeEx(process, _secitem, 0, MEM_RELEASE);
- VirtualFreeEx(process, _thirditem, 0, MEM_RELEASE);
- VirtualFreeEx(process, _fourthitem, 0, MEM_RELEASE);
- }
读取文件列表:
- void CTestDlg::OnBtnReadFile()
- {
- // TODO: Add your control notification handler code here
- m_ListCtrl.DeleteAllItems();
- int nRow;
- int j;
- nRow = m_ListCtrl.GetHeaderCtrl()->GetItemCount();
- for(j=0;j<=nRow;j++)
- {
- m_ListCtrl.DeleteColumn(0);
- }
- // 添加上面的列表头
- m_ListCtrl.InsertColumn(0,"文件名称",NULL,80, -1);
- m_ListCtrl.InsertColumn(1,"大小",NULL,60,-1);
- m_ListCtrl.InsertColumn(2,"类型",NULL,100,-1);
- m_ListCtrl.InsertColumn(3,"修改日期",NULL,120,-1);
- // 查找窗口句柄
- HWND hWnd,hListview;
- hWnd=::FindWindow(NULL,_T("C://Documents and Settings//Koma//桌面//Test"));
- hWnd=::FindWindowEx(hWnd,0,_T("SHELLDLL_DefView"),0);
- hWnd=::FindWindowEx(hWnd,0,_T("DUIViewWndClassName"),0);
- hWnd=::FindWindowEx(hWnd,0,_T("DirectUIHWND"),0);
- hWnd=::FindWindowEx(hWnd,0,_T("CtrlNotifySink"),0);
- hListview=::FindWindowEx(hWnd,0,_T("SysListView32"),NULL);
- if(!hListview)
- {
- MessageBox("Listview handle is NULL !");
- return;
- }
- ......
- }
3、程序运行效果:
4、程序不足:
A、在获取任务管理器“进程”标签内容时,因为有些列表项没有显示、即它的iSubItem项被设置隐藏(设置为0宽度即可),所以需要在“查看”/ “选择列”打开相关选项,不然你可能会觉得程序读错。(如果切换成“用户”标签则不会.)
B、在获取SysListView32句柄时,有些程序的ListCtrl某些列是可编辑的,这里我还没有搞明白为什么可编辑的没有获取到,比如像这样的:
(比如文件列表试图里,文件名是可以编辑的,获取这个又没有问题,这个编辑区域有点像个指定大小的EditBox,不知道这程序是如何重写的CListCtrl类,或是那个开源的项目,见过或用过的友友请说声哈!)
这种可编辑的ListCtrl内容该怎么获取,哪位兄弟知道的,麻烦你有空请告诉我一声吧!
详情请查看此贴:
http://www.codeproject.com/Messages/3244596/How-to-read-the-characters-in-the-EditTable-of-Sys.aspx