Top.Mail.Ru
Ответы
Аватар пользователя
Аватар пользователя
Аватар пользователя
Аватар пользователя
Программирование
+2

Проблема с запуском машинного кода в C++

12345678910111213141516171819202122232425
 #include <iostream>  
 
#pragma section("runstuff", read, execute)  
 
__declspec(allocate("runstuff"))  
const unsigned char compare_code[] = {  
    0x55, 
    0x89, 0xE5, 
    0x8B, 0x45, 0x08, 
    0x3B, 0x45, 0x0C, 
    0x7C, 0x06, 
    0xB8, 0x01, 0x00, 0x00, 0x00, 
    0xEB, 0x02, 
    0xB8, 0xFF, 0xFF, 0xFF, 0xFF, 
    0xC3, 
    0x90, 
    0x90 
};  
 
int main() {  
    int(*compare_func)(int, int) = reinterpret_cast<int(*)(int, int)>(&compare_code[0]);  
 
    std::cout << compare_func(5, 3) << std::endl;  
    return 0;  
} 

Код выхода " (0xc0000005)". Почему нет доступа к памяти?

По дате
По рейтингу
Аватар пользователя
Знаток
9мес
1234567891011121314151617181920212223242526272829
 #include <iostream> 
#include <Windows.h> 
 
#pragma section("runstuff", read, execute) 
 
__declspec(allocate("runstuff")) 
const unsigned char compare_code[] = { 
    0x55,  
    0x89, 0xE5,  
    0x8B, 0x45, 0x08,  
    0x3B, 0x45, 0x0C,  
    0x7C, 0x06,  
    0xB8, 0x01, 0x00, 0x00, 0x00,  
    0xEB, 0x02,  
    0xB8, 0xFF, 0xFF, 0xFF, 0xFF,  
    0xC3,  
    0x90,  
    0x90  
}; 
 
int main() { 
    DWORD oldProtect; 
    VirtualProtect(&compare_code[0], sizeof(compare_code), PAGE_EXECUTE_READ, &oldProtect); 
 
    int(*compare_func)(int, int) = reinterpret_cast<int(*)(int, int)>(&compare_code[0]); 
 
    std::cout << compare_func(5, 3) << std::endl; 
    return 0; 
}