获取桌面图标的名称

shoufaqi2 2013-10-25 10:19:06



#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;

}




只能显示图标数量,不能显示名称
...全文
183 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
dangers_boys 2013-10-29
  • 打赏
  • 举报
回复
下面是一个获取listview中item的名称的代码,楼主可以参考一下
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;
}
爆豆 2013-10-25
  • 打赏
  • 举报
回复
你这个“szText”只剩下了遍历的最后一个图标名,注意要累加出来

    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)));
试试看

15,473

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧