15,473
社区成员




#include <Windows.h>
#include <tchar.h>
#include "Commctrl.h"
#include <iostream>
#include <string>
using namespace std;
#define ALLOC_SIZE 200
//取桌面 ListView 的句柄
string OnSetDeskIcon()
{
HWND hDeskTop;
DWORD dwPID;
HANDLE hProcess;
LPVOID pData;
LPVOID pString;
LVITEM lvi;
//BOOL fResult;
//SIZE_T BytesRead;
//SIZE_T BytesWritten;
char szText[ALLOC_SIZE];
hDeskTop = FindWindow(_T("progman"), NULL);
hDeskTop = FindWindowEx(hDeskTop, 0, _T("shelldll_defview"), NULL);
hDeskTop = FindWindowEx(hDeskTop, 0, _T("syslistview32"), NULL);
int count = ListView_GetItemCount(hDeskTop);
// get desktop window process ID (explorer.exe)
GetWindowThreadProcessId(hDeskTop, &dwPID);
// open process to get explorer process handle
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwPID);
// allocate memory in explorer's address space
// allocate space for LVITEM struct
pData = VirtualAllocEx(hProcess, NULL, ALLOC_SIZE, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
// allocate space for string (i.e. "Network Neighborhood")
pString = VirtualAllocEx(hProcess, NULL, ALLOC_SIZE, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
// init LV_ITEM struct
ZeroMemory(&lvi, sizeof(LVITEM));
//lvi.iItem = 0;
lvi.cchTextMax = 500;
// write the contents of lvi into explorer's address space
for(int i = 0; i < count; i++)
{
// get item's name
lvi.iSubItem = 0;
lvi.pszText = (LPWSTR)pString;
WriteProcessMemory(hProcess, pData, &lvi, sizeof(LVITEM),NULL);
::SendMessage(hDeskTop, LVM_GETITEMTEXT, (WPARAM)i, (LPARAM)pData);
// read back lvi struct (for debugging purposes only)
//ReadProcessMemory(hProcess, pData, szText, ALLOC_SIZE, &BytesRead);
// read text of queried item
ReadProcessMemory(hProcess, pString, szText, ALLOC_SIZE,NULL);
}
VirtualFreeEx(hProcess, pString, ALLOC_SIZE, MEM_DECOMMIT);
VirtualFreeEx(hProcess, pData, ALLOC_SIZE, MEM_DECOMMIT);
CloseHandle(hProcess);
string s = szText;
return s;
}
int main()
{
//int n = OnSetDeskIcon();
//cout << n << endl;
//cout <<
string s = OnSetDeskIcon();
cout << s;
}
bool getItemTextFromListBox(HWND hWndListView, int item, int subItem, TCHAR** ppwszText)
{
bool retcode=false;
TCHAR szReadBuffer[1024];
memset(szReadBuffer, 0, sizeof(szReadBuffer) );
LV_ITEM* plvi=NULL;
DWORD dwProcessId;
GetWindowThreadProcessId(hWndListView, &dwProcessId);
// Open a handle to the remote process's kernel object
HANDLE hProcess = OpenProcess(
PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE,
FALSE, dwProcessId);
if (hProcess == NULL) {
//MessageBox(NULL, __TEXT("Could not communicate with process"),
// "ERROR", MB_OK | MB_ICONWARNING);
goto cleanup;
}
// Allocate memory in the remote process's address space
plvi = (LV_ITEM*) VirtualAllocEx(hProcess,
NULL, 4096, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
if(plvi==NULL) {
goto cleanup;
}
// Get the ListView item's text data
// Initialize a local LV_ITEM structure
LV_ITEM lvi;
memset(&lvi, 0, sizeof(LV_ITEM) );
lvi.mask = LVIF_TEXT;
lvi.iItem = item;
lvi.iSubItem = subItem;
// NOTE: The text data immediately follows the LV_ITEM structure
// in the memory block allocated in the remote process.
lvi.pszText = (LPTSTR) (plvi + 1);
lvi.cchTextMax = 100;
// Write the local LV_ITEM structure to the remote memory block
if( !WriteProcessMemory(hProcess, plvi, &lvi, sizeof(lvi), NULL) ) {
goto cleanup;
}
// Tell the ListView control to fill the remote LV_ITEM structure
ListView_GetItem(hWndListView, plvi);
// Read the remote text string into the end of our clipboard buffer
if( !ReadProcessMemory(hProcess, plvi + 1, (LPVOID) &szReadBuffer, sizeof(szReadBuffer), NULL) ) {
goto cleanup;
}
*ppwszText = (TCHAR*) malloc( _tcslen( szReadBuffer ) + sizeof(TCHAR) );
if(!*ppwszText) {
goto cleanup;
}
retcode=true;
_tcscpy(*ppwszText, szReadBuffer);
// ListView_SetItemState(hWndListView, -1, 0, LVIS_SELECTED);
cleanup:
// Free the memory in the remote process's address space
if(hProcess) {
VirtualFreeEx(hProcess, plvi, 0, MEM_RELEASE);
// Cleanup and put our results on the clipboard
CloseHandle(hProcess);
}
return retcode;
}
hDeskTop = FindWindow(_T("progman"), NULL);
hDeskTop = FindWindowEx(hDeskTop, 0, _T("shelldll_defview"), NULL);
hDeskTop = FindWindowEx(hDeskTop, 0, _T("syslistview32"), NULL);
换成
hDeskTop= ::GetTopWindow(::GetTopWindow(::FindWindow("ProgMan", NULL)));
试试看