The MASM Forum Archive 2004 to 2012

General Forums => The Workshop => Topic started by: charles on May 22, 2010, 10:38:44 AM

Title: How do I get Windows permission to modify code?
Post by: charles on May 22, 2010, 10:38:44 AM
I need to set breakpoints in my code, while debugging (I use my own debugger). This means I have to ask Windows to allow me to do this for the memory address that contains the code. So I did this with the VirtualProtect API call for a Windows 98 application and it worked fine. But now I am trying to run the application under XP, but the VirtualProtect call fails. The description of VirtualProtect says that the stated memory must first have been committed by VirtualAlloc. OK, but I can't do a VirtualAlloc for the code, because the code is there already. Do I have to move the code to somewhere else that I have reserved with VirtualAlloc, and then run the code from there? Does it work? Is there another way of doing it? Thanks for any help.
Title: Re: How do I get Windows permission to modify code?
Post by: jj2007 on May 22, 2010, 10:50:47 AM
Quote from: charles on May 22, 2010, 10:38:44 AM
I need to set breakpoints in my code, ....  because the code is there already.

Why don't you just change your code?
Title: Re: How do I get Windows permission to modify code?
Post by: charles on May 22, 2010, 01:43:51 PM
Thanks for the reply. I have to change the code dynamically because I am putting a breakpoint byte (INT 3) at the start of an instruction, so that processing can stop there and go to my debugging routine. So there is an absolute need to be able to put a byte into RAM that is normally regarded by Windows as read-only. So I have tio use the VirtualProtect call, but it doesn't work because (I guess) I haven't set up the code-space with a VirtualAlloc. But how you can set up the already existing code RAM with a VirtualAlloc - not possible, because it is already there!
Title: Re: How do I get Windows permission to modify code?
Post by: jj2007 on May 22, 2010, 01:47:27 PM
I understand. I guess the best solution is that you post your full code here, so that we can help you to identify the culprit.
Title: Re: How do I get Windows permission to modify code?
Post by: MichaelW on May 22, 2010, 02:05:30 PM
VirtualProtect fails how? If it is returning zero (failure), then what does GetLastError return? If it is returning non-zero (success) but the memory is still write-protected, then you probably have an error in one or more of the parameters you are passing.

Title: Re: How do I get Windows permission to modify code?
Post by: dedndave on May 22, 2010, 03:03:14 PM
it was a much simpler mechanism under Win98   :P
your debugger won't be fully functional under Win 2K or later without a lot of re-write
...probably involving a kernal mode driver, which is somewhat advanced
Title: Re: How do I get Windows permission to modify code?
Post by: redskull on May 22, 2010, 06:52:42 PM
Assuming the other program is already started, it will already be 'virtual alloced'.  In reality, it will be a section object and not a virtual page, but either way it will most certainly be committed.  Since your debugger and your program are two different processess, you need the virtualprotectEX version, with all the appropriate permissions, to change the other processess memory, and then use WriteProcessMemory to do so.  If it still fails, then use GetLastError to see why.  And while your intentions are probably good, you are likely to run up against the grey area of forum rules, which discourage (if not prohibit) these types of discussions.

-r
Title: Re: How do I get Windows permission to modify code?
Post by: Tedd on May 27, 2010, 10:35:34 AM
SeDebugPrivilege : Required to debug and adjust the memory of a process owned by another account.

http://msdn.microsoft.com/en-us/library/bb530716(v=VS.85).aspx