Skip to content

Instantly share code, notes, and snippets.

@chrislattman
Created June 27, 2024 04:50
Show Gist options
  • Save chrislattman/e402cdbd1e4f95d0443e97f53c72ba82 to your computer and use it in GitHub Desktop.
Save chrislattman/e402cdbd1e4f95d0443e97f53c72ba82 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <Windows.h>
static char error_message[1024];
static char *StrGetLastError(DWORD error_code)
{
LPSTR messageBuffer = NULL;
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
error_code,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR) &messageBuffer,
0,
NULL);
strncpy(error_message, messageBuffer, sizeof(error_message) - 1);
LocalFree(messageBuffer);
return error_message;
}
int main(void)
{
LPVOID ptr = VirtualAlloc(NULL, 24, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (ptr == NULL) {
fprintf(stderr, "VirtualAlloc: %s\n", StrGetLastError(GetLastError()));
return 1;
}
DWORD oldFlProtect;
BOOL status = VirtualProtect(ptr, 24, PAGE_READONLY, &oldFlProtect);
if (!status) {
fprintf(stderr, "VirtualProtect: %s\n", StrGetLastError(GetLastError()));
return 1;
}
status = VirtualFree(ptr, 0, MEM_RELEASE);
if (!status) {
fprintf(stderr, "VirtualFree: %s\n", StrGetLastError(GetLastError()));
return 1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment