Salutations,
I'm experiencing a strange problem with Visual C++ 2010 and Visual C++ 6.0. I have the following piece of code:
Code:
hWnd = FindWindow("Progman", NULL);
if (NULL != hWnd) hWnd = FindWindowEx(hWnd, NULL, "SHELLDLL_DefView", NULL);
if (NULL != hWnd) hWnd = FindWindowEx(hWnd, NULL, "SysListView32", NULL);
if (NULL != hWnd)
{
DWORD pid;
GetWindowThreadProcessId(hWnd, &pid);
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
RECT* ptritemrect;
RECT itemrect;
ptritemrect = (RECT*)VirtualAllocEx(hProcess, NULL, sizeof(RECT), MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
if (ptritemrect == NULL) {
DWORD error = GetLastError();
char cavalo[10];
sprintf(cavalo, "%i", error);
MessageBox(w, cavalo, "", 0);
} else {
SendMessage(hWnd, LVM_GETITEMRECT, 0, (LPARAM)ptritemrect);
DWORD copied = 0;
ReadProcessMemory(hProcess, (void*)ptritemrect, (LPVOID)&itemrect, sizeof(itemrect), &copied);
VirtualFreeEx(hProcess, ptritemrect, 0, MEM_RELEASE);
}
}
The problem is this: I've tried to compile this code in Visual C++ 2010 (at Windows 7) and in Visual C++ 6 (at Windows XP Professional SP3).
The code compiled with Visual C++ 6 works on both computers. But the code compiled with Visual C++ 2010 works only on Windows 7, not on Windows XP. On Windows XP, VirtualAllocEx doesn't work and ptritemrect gets the value NULL.
The function GetLastError() accuses error code 6, which means "The handle is invalid.".
I have no idea on why it works in Windows 7 and Windows XP when compiled with Visual C++ 2010, while it works on both when compiled with Visual C++ 6.
Any ideas?
Thank you in advance.