注册 登录
编程论坛 VC++/MFC

无法获取任务管理器listview控件内容,我百思不得其解,请高手看看哪里有问题?

findstory 发布于 2022-10-11 21:02, 1430 次点击
    主要代码如下
    HWND hwnd, hListview, header;
    hwnd = ::FindWindow(_T("#32770"),_T("Windows 任务管理器"));
    hwnd = ::FindWindowEx(hwnd,0,_T("#32770"), _T("Processes"));
    hListview = ::FindWindowEx(hwnd, 0, _T("SysListView32"), _T("进程"));
    header = ::FindWindowEx(hListview, 0, _T("SysHeader32"), 0);
    int rows = ::SendMessage(hListview, LVM_GETITEMCOUNT, 0, 0);
    int cols = ::SendMessage(header, HDM_GETITEMCOUNT, 0, 0);
   
    HANDLE hProcess;
    LVITEM vItem, *pointer;
    char ItemBuf[512], *pItem;
    DWORD ThreadID, PID;
    ThreadID = GetWindowThreadProcessId(hListview, &PID);
    hProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_QUERY_INFORMATION, FALSE, PID);
    pointer = (LVITEM*)VirtualAllocEx(hProcess, NULL, sizeof(LVITEM), MEM_COMMIT, PAGE_READWRITE);
    for (int i = 0; i < 1; i++)
    {
        //m_listctrl.InsertItem(i, _T(""));
        for (int j = 0; j < 3; j++)
        {
            vItem.mask = LVIF_TEXT;    //说明pszText是有效的
            vItem.iItem = i;        //行号
            vItem.iSubItem = j;        //列号
            vItem.cchTextMax = 512;    //所能存储的最大的文本为256字节
            //LPWSTR pItem = NULL;
            //申请内存空间
            pItem = (char *)VirtualAllocEx(hProcess, NULL, 512, MEM_COMMIT, PAGE_READWRITE);
            vItem.pszText = pItem;
            WriteProcessMemory(hProcess, pointer, &vItem, sizeof(LVITEM), NULL);
            ::SendMessage(hListview, LVM_GETITEMW, (WPARAM)i, (LPARAM)pointer);
            memset(ItemBuf, 0, 512);
            ReadProcessMemory(hProcess, pItem, ItemBuf, 512, NULL);
            CString str;
            str.Format(_T("%s"), ItemBuf);
            MessageBox(str);
            //释放内存空间
            //m_listctrl.SetItemText(i, j, str);
            VirtualFreeEx(hProcess, pItem, 0, MEM_RELEASE);
        }
    }
    VirtualFreeEx(hProcess, pointer, 0, MEM_RELEASE);
    CloseHandle(hProcess);//关闭打开的进程对象
    程序运行时得到的行数、列数都正确,但是MessageBox(str)弹窗弹出的信息都是空的是为什么?
2 回复
#2
findstory2022-10-12 09:02
参考
https://
https://blog.
https://www.

[此贴子已经被作者于2022-10-17 15:41编辑过]

#3
findstory2022-10-22 15:11
https://www.
1