常量区的修改

来源:互联网 发布:缎面 婚纱 知乎 编辑:程序博客网 时间:2024/04/29 09:18
#include <Windows.h>#include <stdio.h>int main(){const char* a = "123456";const char* b = "123456";char* c = "654321";DWORD oldprot; HANDLE hProcess = GetCurrentProcess(); VirtualProtectEx(hProcess, (LPVOID)b, 7, PAGE_EXECUTE_READWRITE, &oldprot);WriteProcessMemory(hProcess, (LPVOID)b, (LPVOID)c, 7, NULL);printf("%s \n", a);}


以上程序输出  654321


“123456”是储存在常量区的,也就是说是在编译的时候就确定的,a、b、c只是一个指向常量区的指针。

由于编译器的优化,此时 a = b

VirtualProtectEx : Changes the protection on a region of committed pages in the virtual address space of a specified process.

WriteProcessMemory:Writes data to an area of memory in a specified process. The entire area to be written to must be accessible or the operation fails.

原创粉丝点击