#define _WIN32_WINNT 0x0502
#include <windows.h>
#include <stdio.h>

int main(int argc, char* argv[])
{
	LPVOID lpAddress = VirtualAlloc(NULL,4,MEM_RESERVE|MEM_COMMIT,PAGE_READWRITE);

	if(lpAddress == NULL){
		printf("Alloc failed %d\n",GetLastError());
		exit(1);
	}

	PDWORD lpflOldProtect;

	// Change page protection to no access
	if(!VirtualProtect(lpAddress,4,PAGE_NOACCESS,lpflOldProtect)){
		printf("protect failed %d",GetLastError());
		exit(1);
	}

	int iBuf;

	// Check accessiblity
	if(!ReadProcessMemory(GetCurrentProcess(),lpAddress,&iBuf,4,0))
		printf("Unreadable\n");
	else
		exit(1);

	// Try crash sandbox application, os won't crash because handle is invalid
	RegOpenKey(0,(char*)lpAddress,0);

	// Try to create file to test if the application isn't crashed
	CreateFile("C:\\log",
    		   GENERIC_WRITE,
			   FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE,
			   NULL,
			   OPEN_ALWAYS,
			   FILE_ATTRIBUTE_NORMAL,
			   NULL);
	
	return 0;
}
