/**************************************《 Proficient in Windows API * Sample Code * virtual. C * 5.4.2 allocate and release readable and writable virtual memory pages **************************** * ******** // * header file */# include <windows. h> # include <stdio. h> /************************************* * int main (void) * function Demonstration: the use of virtual memory ** parameter is not used **************************** * ********/INT main (void) {size_t sizevirtual = 4000; // lpvoid lpround = (lpvoid) 0x100000ff; // address memory_basic_inform Ation MBI; // memory information // allocate memory, directly allocate submitted memory lpvoid lpaddress = virtualalloc (lpround, sizevirtual, mem_commit | mem_reserve, page_readwrite ); if (lpaddress = NULL) {printf ("virtualalloc error: % d \ n", getlasterror (); return 1 ;}printf ("alloc: mem_commit | mem_reserve \ n "); // copy data to the memory copymemory (lpaddress," hello ", lstrlen (" hello "); printf (" allocated and copied successfully, address: 0x %. 8x, content: % s \ n ", lpaddress, lpaddress); // obtain the memory information and print virtualquery (lpaddress, & MBI, S Izeof (MBI); printf ("information obtained using virtualquery: \ n" "baseaddress: 0x %. 8x \ tallocationbase: 0x %. 8x \ t "" allocationprotect: 0x %. 8x \ tregionsize: % u \ t "" State: 0x %. 8x \ tprotect: 0x %. 8x \ tType: 0x %. 8x \ n ", MBI. baseaddress, MBI. allocationbase, MBI. allocationprotect, MBI. regionsize, MBI. state, MBI. protect, MBI. type); // set it to the read-only attribute // If (! Virtualprotect (lpaddress, 0, page_readonly, null) // {// printf ("virtualprotect error: % d", getlasterror (); // return 1; //} // test the read-only attribute. // copymemory (lpaddress, "Read Only", lstrlen ("Read Only ")); // printf (lpaddress); // obtain memory information and print // virtualquery (lpaddress, & MBI, sizeof (MBI )); // printf ("information obtained using virtualquery: \ n" // "baseaddress: 0x %. 8x \ tallocationbase: 0x %. 8x \ t "//" allocationprotect: 0x %. 8x \ tregionsize: % d \ t "//" stat E: 0x %. 8x \ tprotect: 0x %. 8x \ tType: 0x %. 8x \ n ", // MBI. baseaddress, MBI. allocationbase, // MBI. allocationprotect, MBI. regionsize, // MBI. state, MBI. protect, MBI. type //); // decommit is released, and the page changes to the reserved status printf ("free: decommit \ n"); If (! Virtualfree (lpround, sizevirtual, mem_decommit) {printf ("virtualfree error: % d", getlasterror (); return 1;} // obtain memory information and print virtualquery (lpaddress, & MBI, sizeof (MBI); printf ("information obtained using virtualquery: \ n" "baseaddress: 0x %. 8x \ tallocationbase: 0x %. 8x \ t "" allocationprotect: 0x %. 8x \ tregionsize: % u \ t "" State: 0x %. 8x \ tprotect: 0x %. 8x \ tType: 0x %. 8x \ n ", MBI. baseaddress, MBI. allocationbase, MBI. allocationprotect, MBI. regionsize, MBI. stat E, MBI. Protect, MBI. type); // release the memory printf ("free: Release \ n"); If (! Virtualfree (lpaddress, 0, mem_release) {printf ("virtualfree error: % d", getlasterror (); return 1 ;}return 0 ;}