News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

How do I get Windows permission to modify code?

Started by charles, May 22, 2010, 10:38:44 AM

Previous topic - Next topic

charles

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.

jj2007

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?

charles

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!

jj2007

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.

MichaelW

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.

eschew obfuscation

dedndave

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

redskull

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
Strange women, lying in ponds, distributing swords, is no basis for a system of government

Tedd

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
No snowflake in an avalanche feels responsible.