我在写自我修改的代码。我试图将函数的地址权限更改为要执行,但是当我尝试将用Dev C++编写的代码迁移到Visual 2017时,我得到了一个错误。
对于此任务,我使用Microsoft 10和Visual 20017进行默认配置。
int change_page_permissions_of_address(void *addr) {
// Move the pointer to the page boundary
int page_size = getpagesize();
DWORD dwOldProtect;
addr -= (uintptr_t)addr % page_size;
if (VirtualProtect(addr, page_size, PAGE_EXECUTE_READWRITE, &dwOldProtect) == -1) {
return -1;
}
return 0;
}
int main() {
char *foo_addr = (char*)foo;
if (change_page_permissions_of_address(foo_addr) == -1) {
printf("Error while changing page permissions of foo(): %s\n");
return 1;
}
}
int main(){
// Call the unmodified foo()
puts("Calling foo...");
foo();
// Change the immediate value in the addl instruction in foo() to 42
unsigned char *instruction = (unsigned char*)foo_addr + 18;
*instruction = 0x2A;
// Call the modified foo()
puts("Calling foo..., but I am the self-modifying");
foo();
}
}我希望在visual studio中具有与dev c++相同的行为。
错误
错误(活动) E0852表达式必须是指向完整对象类型的指针 错误C2036 'void *':未知大小

发布于 2019-04-12 01:35:09
addr -= (uintptr_t)addr % page_size;是问题所在。指针上的-=将左边作为整数减去指针所指向的事物的大小,并从指针中减去它。编译器抱怨是因为它不知道void的大小。
你能把参数变成char *吗?
https://stackoverflow.com/questions/55643165
复制相似问题