1
我正在尝试使用桌面列表查看一些东西。我可以使用LVM_GETITEM获得项目的文本,但iImage结构成员始终为零,状态也是如此。我正在运行Win 7 64并使用Dev C++(gcc)编译为64。我也尝试编译它为32位,并在XP上测试,结果相同...只是文本,图像和状态分配为零。我也可以使用ListView_GetItemCount()来获得项目数量没有问题。Windows ListView LVM_GETITEM iImage始终为零
HWND progman = FindWindow("progman", NULL);
HWND shell = FindWindowEx(progman, NULL, "shelldll_defview", NULL);
HWND hwndListView = FindWindowEx(shell, NULL, "syslistview32", NULL);
int ct = ListView_GetItemCount(hwndListView);
const DWORD dwBufSize = 1024;
DWORD dwProcessID;
DWORD dwResult;
HANDLE hProcess;
BYTE *lpRemoteBuffer;
LVITEM lvItem = {0};
BYTE lpLocalBuffer[dwBufSize] = {0};
// Get the process id owning the window
::GetWindowThreadProcessId(hwndListView, &dwProcessID);
// Open the process wih all access (You may not have the rights to do this)
hProcess = ::OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcessID);
// Allocate a buffer in the remote process
lpRemoteBuffer = (BYTE*)::VirtualAllocEx(hProcess, NULL, dwBufSize,
MEM_COMMIT, PAGE_READWRITE);
// Fill in the LVITEM struct, this is in your own process
// Set the pszText member to somewhere in the remote buffer,
// For the example I used the address imediately following the LVITEM stuct
lvItem.mask = LVIF_TEXT|LVIF_IMAGE|LVIF_PARAM|LVIF_STATE;
lvItem.iItem = 0;
lvItem.iSubItem = 0;
lvItem.cchTextMax = MAX_PATH;
// Point to after LVITEM in the remote buffer
lvItem.pszText = (LPTSTR)(lpRemoteBuffer + sizeof(LVITEM));
// Copy the local LVITEM to the remote buffer
::WriteProcessMemory(hProcess, (LPVOID)lpRemoteBuffer, &lvItem, sizeof(LVITEM), NULL);
// Send the message
::SendMessage(hwndListView, LVM_GETITEM, 0, (LPARAM)lpRemoteBuffer);
// Read the struct back from the remote process into local buffer
::ReadProcessMemory(hProcess, (LPVOID)lpRemoteBuffer, lpLocalBuffer, dwBufSize, NULL);
//Fix pszText to point to same offset in local buffer
lvItem.pszText = (LPTSTR)(lpLocalBuffer + sizeof(LVITEM));
MessageBox(hwnd, lvItem.pszText, "", 0);
char txt[10];
ZeroMemory(txt, 10);
MessageBox(hwnd, itoa(lvItem.iImage, txt, 10), "", 0);
MessageBox(hwnd, itoa((int)lvItem.state, txt, 10), "", 0);
// Clean-up
::VirtualFreeEx(hProcess, (LPVOID)lpRemoteBuffer, 0, MEM_RELEASE);
::CloseHandle(hProcess);
太好了,谢谢!我会尝试一下并发布结果。 – kizeloo
原来我不需要这样做了,但感谢您的帮助!我会投票给你的答案。 – kizeloo
这可能是最好的。不建议使用此方法访问桌面。 –