* Proficient in Windows API 5.4.2 allocate and release readable and writable virtual memory pages

Source: Internet
Author: User
/**************************************《 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 ;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.