Skip to main content

 none
Problem when get desktop icons' text RRS feed

  • Question

  • Hello everyone.

    Recently I'm stucked in developing a program which get the desktop icon's text (like "This PC" or "Recycle Bin"), the code that I wrote is like this:

    #include <Windows.h>
    #include <iostream>
    #include <CommCtrl.h>
    #include <list>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	HWND listview = NULL;
    	HWND workerw_handle = NULL;
    	do {
    		workerw_handle = FindWindowEx(NULL, workerw_handle, "WorkerW", NULL);
    	} while ((listview = FindWindowEx(workerw_handle, NULL, "SHELLDLL_DefView", NULL)) == NULL);
    	listview = FindWindowEx(listview, NULL, "SysListView32", NULL);
    
    	DWORD dwProcessId;
    	GetWindowThreadProcessId(listview, &dwProcessId);
    	HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcessId);
    
    	char item[512];
    
    	LVITEM *_lvi = (LVITEM*)VirtualAllocEx(hProcess, NULL, sizeof(LVITEM), MEM_COMMIT, PAGE_READWRITE);
    	char *_item = (char*)VirtualAllocEx(hProcess, NULL, 512, MEM_COMMIT, PAGE_READWRITE);
    
    	LVITEM lvi;
    	lvi.mask = LVIF_TEXT;
    	lvi.cchTextMax = 512;
    	lvi.pszText = _item;
    	lvi.iSubItem = 0;
    
    	std::list<std::string> m_lItemName;
    	int ItemCount = ListView_GetItemCount(listview);
    
    	for (int i = 0; i <= ItemCount; i++) {
    		WriteProcessMemory(hProcess, _lvi, &lvi, sizeof(LVITEM), NULL);
    		::SendMessage(listview, LVM_GETITEMTEXT, (WPARAM)i, (LPARAM)_lvi);
    		ReadProcessMemory(hProcess, _item, item, 512, NULL);
    		std::cout << item << std::endl;
    		m_lItemName.push_back(item);
    	}
    
    	VirtualFreeEx(hProcess, _lvi, 0, MEM_RELEASE);
    	VirtualFreeEx(hProcess, _item, 0, MEM_RELEASE);
    	return 0;
    }

    It's very simple and should have work. The ProcessMemory are allocated properly. But I can't get any text after sent the message. It's really weird. Hopefully someone can give me a hand. :)

    Tuesday, October 30, 2018 3:17 AM