Thread: Strange problem with VirtualAllocEx

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    49

    Strange problem with VirtualAllocEx

    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.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    How about reading the manual?

    Process Security and Access Rights (Windows)
    Quote Originally Posted by msdn
    PROCESS_ALL_ACCESS All possible access rights for a process object.

    Windows Server 2003 and Windows XP/2000: The size of the PROCESS_ALL_ACCESS flag increased on Windows Server 2008 and Windows Vista. If an application compiled for Windows Server 2008 and Windows Vista is run on Windows Server 2003 or Windows XP/2000, the PROCESS_ALL_ACCESS flag is too large and the function specifying this flag fails with ERROR_ACCESS_DENIED. To avoid this problem, specify the minimum set of access rights required for the operation. If PROCESS_ALL_ACCESS must be used, set _WIN32_WINNT to the minimum operating system targeted by your application (for example,
    #define _WIN32_WINNT _WIN32_WINNT_WINXP
    ). For more information, see Using the Windows Headers.
    Paying attention to return values would help as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Wow, talk about making a molehill into a mountain.

    Code:
    RECT workArea;
    SystemParametersInfo(SPI_GETWORKAREA, 0, &workArea, 0);
    That's it. Now it'll work whenever the order of the windows change, like they did in Win7.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    49
    Quote Originally Posted by Salem View Post
    How about reading the manual?

    Paying attention to return values would help as well.
    Thank you, I put
    Code:
    #define _WIN32_WINNT _WIN32_WINNT_WINXP
    at the top and it works now.
    You are right, I should have checked the return value of OpenProcess.
    I don't know why I was in such a hurry to send this question.
    I've obviously wasted your time.
    Quote Originally Posted by adeyblue View Post
    Wow, talk about making a molehill into a mountain.
    Code:
    RECT workArea;
    SystemParametersInfo(SPI_GETWORKAREA, 0, &workArea, 0);
    That's it. Now it'll work whenever the order of the windows change, like they did in Win7.
    Yes, I use the code you mentioned, but for a different thing.
    Last edited by pc2-brazil; 02-17-2011 at 04:25 PM.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    pc-2 Brazil.... Ok, I'm stumped... what does this code do???

    That is, I can follow each step... but what's the overall effect?

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    49
    Quote Originally Posted by CommonTater View Post
    pc-2 Brazil.... Ok, I'm stumped... what does this code do???

    That is, I can follow each step... but what's the overall effect?
    Actually, I've written this part in a simplified way, but it should have the following line after taking getting the desktop ListView handler:
    Code:
    int count = ListView_GetItemCount(hWnd);
    It counts the number of icons of the desktop.
    Then, the whole part from the call to VirtualAllocEx on should be inside a loop, and the line
    Code:
    SendMessage(hWnd, LVM_GETITEMRECT, 0, (LPARAM)ptritemrect);
    should be:
    Code:
    SendMessage(hWnd, LVM_GETITEMRECT, i, (LPARAM)ptritemrect);
    where i is an index from 0 to count - 1. Actually, it loops through every icon in the desktop and gets its rectangle.
    Anyway, the problem is solved already, and it was that I should have checked the every return value.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ahh.. makes sense now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange problem with GETLINE
    By wco5002 in forum C++ Programming
    Replies: 13
    Last Post: 07-07-2008, 09:57 AM
  2. Strange problem
    By G4B3 in forum C Programming
    Replies: 6
    Last Post: 05-14-2008, 02:07 PM
  3. Strange problem with classes in header files
    By samGwilliam in forum C++ Programming
    Replies: 2
    Last Post: 02-29-2008, 04:55 AM
  4. Strange problem
    By ~Kyo~ in forum Game Programming
    Replies: 0
    Last Post: 02-14-2006, 10:35 PM