To jest wycinek mojego programu do zapisu & przywracania układu ikon:
const PROCESS_MEMORY = PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE;
struct MYDATATYPE
{
POINT pt;
union
{
LVITEM lvi;
LVFINDINFO lvfi;
};
TCHAR text[MAX_PATH];
};
BOOL FindListview(HWND hwnd, HWND *ppv)
{
*ppv = 0;
while (1)
{
HWND h = FindWindow(TEXT("Progman"), NULL);
if (!h) {
MessageBox(hwnd, TEXT("Progman not found"));
break;
}
h = FindWindowEx(h, 0, TEXT("SHELLDLL_DefView"), NULL);
if (!h) {
MessageBox(hwnd, TEXT("SHELLDLL_DefView not found"));
break;
}
*ppv = FindWindowEx(h, 0, WC_LISTVIEW, NULL);
if (!*ppv) {
MessageBox(hwnd, TEXT("SysListView32 not found"));
}
break;
}
return *ppv;
}
BOOL save(HWND hwnd)
{
MYDATATYPE data;
BOOL success = FALSE;
XML root, desktop, litem;
while (1)
{
HWND hwndList;
if (!FindListview(hwnd, &hwndList)) {
break;
}
root.InitNew(L"1.0");
root.AppendChildNode(L"Desktop", NULL, &desktop);
int itemcount = ListView_GetItemCount(hwndList);
if (itemcount)
{
DWORD pid;
GetWindowThreadProcessId(hwndList, &pid);
HANDLE hProcess = OpenProcess(PROCESS_MEMORY, FALSE, pid);
if (!hProcess) {
MessageBox(hwnd, TEXT("Failed to open Explorer process"));
break;
}
MYDATATYPE *memory = VirtualAllocEx(hProcess, 0, sizeof(data), MEM_COMMIT, PAGE_READWRITE);
if (!memory) {
CloseHandle(hProcess);
MessageBox(hwnd, TEXT("Failed to allocate memory"));
break;
}
data.lvi.iSubitem = 0;
data.lvi.mask = LVIF_TEXT;
data.lvi.pszText = &(memory->text);
data.lvi.cchTextMax = MAX_PATH;
for (int item=0; item<itemcount; item++)
{
data.lvi.iItem = item;
if (WriteProcessMemory(hProcess, memory, &data, sizeof(data), NULL)
&& SendMessage(hwndList, LVM_GETITEMPOSITION, item, &memory->pt)
&& SendMessage(hwndList, LVM_GETITEM, 0, &memory->lvi)
&& ReadProcessMemory(hProcess, memory, &data, sizeof(data), NULL))
{
desktop.AppendChildNode(L"Item", NULL, &litem);
litem.SetAttribute(L"Name", data.text);
litem.SetAttributeInt(L"x", data.pt.x);
litem.SetAttributeInt(L"y", data.pt.y);
}
}
VirtualFreeEx(hProcess, memory, 0, MEM_RELEASE);
CloseHandle(hProcess);
}
GetSettingsPath(data.text);
success = root.Save(data.text);
if (!success) {
_stprintf(&data, TEXT("Failed to write %s"), data.text);
MessageBox(hwnd, &data);
}
break;
}
return success;
}