WriteProcessMemory with same address.


I have created a main application which do the following thing:

1. OpenProcess Function (Let's say we open "calc.exe").
2. VirtualAllocEx Function (Allocate some bytes in "calc.exe" memory).
3. WriteProcessMemory Function (Write some bytes in "calc.exe")
4. CloseHandle

Then I try to do the the same thing again with the same instance of "calc.exe".
But this time I know the allocated memory address from the last time so I only need to do the following thing:

1. OpenProcess Function (Same instance).
2. WriteProcessMemory Function (Write some bytes. I know the address from before)
3. CloseHandle

Why is this not posible?
I get an error when I second time tries to write in the same memory address
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You cannot write to any processes address space, or even your own, until physical storage has been committed to the address range in question. That is what VirtualAllocEx does. WriteProcessMemory does not do this. Read more about this on MSDN:

DelphiUKAuthor Commented:
Yes but I use "VirtualAllocEx" the first time to allocate some memory as you can see.
So I just can't get why I am not able to use WriteProcessMemory second time.
I use the same address as I was using the first time?

Dagan HooverDeveloperCommented:
What is the error code returned?
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

DelphiUKAuthor Commented:
Error code 87 - Wrong parameter (In WriteProcessMemory function)
DelphiUKAuthor Commented:
Because it is only in the second time when I am using WriteProcessMemory  I get the error.

And I don't get it because I use the same address as I was using in the first time.
Dagan HooverDeveloperCommented:
You wouldn't get that error because the address is wrong. Can you post the code of both uses of WriteProcessMemory() and the surrounding code?
DelphiUKAuthor Commented:
I did find the problem. The problem occur because I was using a wrong Process PID in OpenProcess function. Thats why I get ErrorCode 87.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dagan HooverDeveloperCommented:
A good! Glad you found it.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.