?
Solved

VirtualAllocEx and MEM_COMMIT without MEM_RESERVE

Posted on 2007-10-06
5
Medium Priority
?
1,842 Views
Last Modified: 2012-05-05
I'm reviewing the example at http://www.codeproject.com/threads/winspy.asp#section_2 (the third code example within the second section of that webpage). The purpose of the example is to allocate memory in another process' VM space, which can then have a string written to it.

...
pLibRemote = ::VirtualAllocEx( hProcess, NULL, sizeof(szLibPath),
                               MEM_COMMIT, PAGE_READWRITE );
::WriteProcessMemory( hProcess, pLibRemote, (void*)szLibPath,
                      sizeof(szLibPath), NULL );
...

Specifically, I'm wondering why the example works without having to specify MEM_COMMIT | MEM_RESERVE. The VirtualAllocEx documentation states that calling MEM_COMMIT without having reserved first will cause problems. But the code in the example doesn't seem to reserve first.

I'm probably missing something simple.
0
Comment
Question by:jimstar
  • 3
  • 2
5 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 20028644
It works because the pages (data segment in the remore DLL) are already commited. They aren't adding new pages, they are altering already committed ones. The docs also state "An attempt to commit a page that is already committed does not cause the function to fail. This means that you can commit pages without first determining the current commitment state of each page." (http://msdn2.microsoft.com/en-us/library/aa366890.aspx)
0
 
LVL 4

Author Comment

by:jimstar
ID: 20028672
>> It works because the pages (data segment in the remore DLL) are already commited. They aren't adding new pages, they are altering already committed ones.

Managing memory allocation outside of malloc/free is still pretty new to me. Could you maybe describe this a little more? I'm having trouble understanding - does VirtualAllocEx always return memory that has been reserved/committed (I'm also new to reserving/committing, so perhaps I'm misunderstanding this too).

I was under the impression that VirtualAllocEx would allocate new memory in the target process. From what you're saying, that's not always the case?

Thanks for the info and patience!
0
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 20028684
Actually I was wrong. They are loading the DLL after that. But also, the docs above have been changed, the originals state different things:

Flag
MEM_COMMIT

Meaning
The function allocates actual physical storage in memory or in the paging file on disk for the specified region of memory pages. The function initializes the memory to zero.  
 An attempt to commit a memory page that is already committed does not cause the function to fail. This means that you can commit a range of pages without first determining the current commitment state of each page.
*If a memory page is not yet reserved, setting this flag causes the function to both reserve and commit the memory page.*

(taken from the VC6 docs)

The last sentence IMO makes the behaviour clear, and that's what I also thought it had been like.
0
 
LVL 4

Author Comment

by:jimstar
ID: 20028700
Thanks for the clarification. It's weird that the more recent docs on msdn.microsoft.com (built on 9/2007) state that it will fail if you try to commit without reserving (and that you must specify both MEM_RESERVE and MEM_COMMIT). I wonder if they're changing the behavior, or if it's an oversight. It seems like the two doc versions describe different functionality that isn't backwards compatible.

=== http://msdn2.microsoft.com/en-us/library/aa366890.aspx ==

MEM_COMMIT
0x1000
 Allocates physical storage in memory or in the paging file on disk for the specified reserved memory pages. The function initializes the memory to zero.

**To reserve and commit pages in one step, call VirtualAllocEx with MEM_COMMIT | MEM_RESERVE.

**The function fails if you attempt to commit a page that has not been reserved. The resulting error code is ERROR_INVALID_ADDRESS.

An attempt to commit a page that is already committed does not cause the function to fail. This means that you can commit pages without first determining the current commitment state of each page.
 
0
 
LVL 86

Expert Comment

by:jkr
ID: 20028713
Well, I guess that this new behaviour is required for Vista (or improves performance there) and they are trying to make people do it that way now...
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question