We help IT Professionals succeed at work.

About VirtualQuery??

chenda19780925
on
Medium Priority
2,566 Views
Last Modified: 2007-12-19

 DWORD base;
 MEMORY_BASIC_INFOMATION mbi;
 ...
 base = 0;
 While(VirtualQuery(base,&mbi,sizeof(mbi)) > 0)
  ...

 -------------------------
 Once I run this code,It will show me a that "[C++ Error] Unit1.cpp(107): E2034 Cannot convert 'unsigned long' to 'const void *'",

 What can I do?Thanks.
Comment
Watch Question

AxterSenior Software Engineer

Commented:
You have the wrong type for the first variable in function VirtualQuery().

The first variable should be a pointer to the base address of the region of pages to be queried.

You're not passing a pointer, and you're not passing the address.
Senior Software Engineer
Commented:
Example code:

void SomeFunction(void)
{
      
      LPVOID lpMemory = NULL;       // Pointer to a region of memory
      LPVOID lpPage = NULL;         // Pointer to a page of memory
      
      lpMemory = VirtualAlloc (NULL, THIRTY_K,MEM_RESERVE | MEM_COMMIT,PAGE_READWRITE);
      
      if (NULL == lpMemory)
      {
            // Your error-handling code goes here. You can use the
            // GetLastError function to obtain more information.
      }
      
      // Your code that uses the just-allocated memory goes here.
      // For example:     LPBYTE MyPointer = (LPBYTE)lpMemory;
      //                  memset (MyPointer, 0, THIRTY_K);
      //                  return MyPointer;
      // . . .
      
      // When you are finished with the memory, you must release it.
      if (!VirtualFree (lpMemory, 0, MEM_RELEASE))
      {
            // Your error-handling code goes here. You can use the
            // GetLastError function to obtain more information.
      }
      
      // Remember to clear the pointer to prevent reuse.
      lpMemory = NULL;
      
      //The following code example shows how to reserve a bundle of memory and then commit that memory, as needed.
            
      lpMemory = VirtualAlloc (NULL, THIRTY_K,MEM_RESERVE, PAGE_READWRITE);
      
      if (NULL == lpMemory)
      {
            // Your error-handling code goes here. You can use the
            // GetLastError function to obtain more information.
      }
      
      // Determine the page size on this system.
      SYSTEM_INFO siSystemInfo;
      
      GetSystemInfo (&siSystemInfo);
      
      // Commit the first page of the just-reserved block.
      // (To commit more than one page, multiply the dwPageSize
      // parameter that is used below by the number of pages to commit.)
      lpPage = VirtualAlloc (lpMemory,        // Base for this commit
            siSystemInfo.dwPageSize,
            // number of bytes
            // to commit
            MEM_COMMIT,      // Selects the operation
            PAGE_READWRITE); // Selects permissions
      
      if (NULL == lpPage)
      {
            // Your error-handling code goes here. You can use the
            // GetLastError function to obtain more information.
      }
      
      // To commit additional pages, add the appropriate
      // offset to ?lpMemory? and call VirtualAlloc again.
      // For example, to commit the tenth page:
      // lpPage = VirtualAlloc ((LPBYTE)lpMemory + 9 * dwPageSize,
      // and so on.
      // . . .
      
      // The following code example shows how to use the VirtualQuery function to examine the characteristics of
      // a page of virtual memory that you previously allocated.
            
      DWORD dwResult;
      MEMORY_BASIC_INFORMATION mbiMemory;
      
      // Clear the results structure.
      memset (&mbiMemory, 0, sizeof(MEMORY_BASIC_INFORMATION));
      
      dwResult = VirtualQuery (lpPage,        // Page to examine
                  &mbiMemory,    // Structure for results
                  sizeof(MEMORY_BASIC_INFORMATION));
      
      if (sizeof(MEMORY_BASIC_INFORMATION) != dwResult)
      {
            // Your error-handling code goes here.
      }
      
      // Here, write code to use the contents of the mbiMemory
      // structure that was filled in by the above call.
      // For example: if (mbiMemory.Protect != PAGE_READWRITE)
      // and so on.
      // . . .
      
      // The following code example shows how to use the VirtualProtect function to
      // change the characteristics of a page of virtual memory that you previously allocated.
            
      DWORD dwOldProtect;
      DWORD dwSize = 2048;  // Or, set this to ?siSystemInfo.dwPageSize?
      // (See the above code for how to get this.)
      
      if (!VirtualProtect (lpPage,        // Beginning of the set of
            // pages to change
            dwSize,        // Length, in bytes, of the
            // set of pages to change
            PAGE_READONLY, // What to change it to
            &dwOldProtect  // Place to store the old setting
            ))
      {
            // Your error-handling code goes here. You can use the
            // GetLastError function to obtain more information.
      }
      
      // The following code example shows how to decommit a page by using
      // the VirtualFree function. Presumably, you could recommit the page later with different attributes.

      if (!VirtualFree (lpPage, 0, MEM_DECOMMIT))
      {
            // Your error-handling code goes here. You can use the
            // GetLastError function to obtain more information.
      }
            
}
AxterSenior Software Engineer

Commented:
The above example code was taken from a MS site.
For more information see the following link, and do a keyword search for VirtualQuery.

http://msdn.microsoft.com/library/en-us/wcedesgn/htm/memmanag_2.asp?frame=true