| 1 | // Licensed to the .NET Foundation under one or more agreements. |
|---|---|
| 2 | // The .NET Foundation licenses this file to you under the MIT license. |
| 3 | // See the LICENSE file in the project root for more information. |
| 4 | |
| 5 | /*============================================================= |
| 6 | ** |
| 7 | ** Source: virtualquery.c |
| 8 | ** |
| 9 | ** Purpose: Positive test the VirtualQuery API. |
| 10 | ** Call VirtualQuery to get the virtual |
| 11 | ** page info |
| 12 | ** |
| 13 | ** |
| 14 | **============================================================*/ |
| 15 | #include <palsuite.h> |
| 16 | |
| 17 | #define VIRTUALMEMORYSIZE 1024 |
| 18 | |
| 19 | int __cdecl main(int argc, char *argv[]) |
| 20 | { |
| 21 | int err; |
| 22 | LPVOID lpVirtualAddress; |
| 23 | MEMORY_BASIC_INFORMATION PageInfo; |
| 24 | DWORD dwBufferSize; |
| 25 | |
| 26 | //Initialize the PAL environment |
| 27 | err = PAL_Initialize(argc, argv); |
| 28 | if(0 != err) |
| 29 | { |
| 30 | ExitProcess(FAIL); |
| 31 | } |
| 32 | |
| 33 | //Allocate the physical storage in memory or in the paging file on disk |
| 34 | lpVirtualAddress = VirtualAlloc(NULL,//determine where to allocate the region |
| 35 | VIRTUALMEMORYSIZE, //specify the size |
| 36 | MEM_COMMIT, //allocation type |
| 37 | PAGE_READONLY); //access protection |
| 38 | if(NULL == lpVirtualAddress) |
| 39 | { |
| 40 | Fail("\nFailed to call VirtualAlloc API!\n"); |
| 41 | } |
| 42 | |
| 43 | //get the virtual page info |
| 44 | dwBufferSize = |
| 45 | VirtualQuery(lpVirtualAddress,&PageInfo,sizeof(MEMORY_BASIC_INFORMATION)); |
| 46 | |
| 47 | if(dwBufferSize <= 0 || |
| 48 | PageInfo.RegionSize <=0 || |
| 49 | PAGE_READONLY != PageInfo.AllocationProtect || |
| 50 | MEM_COMMIT != PageInfo.State) |
| 51 | { |
| 52 | Fail("\nFailed to call VirtualQuery API!\n"); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | |
| 57 | //decommit the specified region |
| 58 | err = VirtualFree(lpVirtualAddress, //virtual page base address |
| 59 | VIRTUALMEMORYSIZE,//specify the size |
| 60 | MEM_DECOMMIT);//free operation |
| 61 | if(0 == err) |
| 62 | { |
| 63 | Fail("\nFailed to call VirtualFree API!\n"); |
| 64 | } |
| 65 | |
| 66 | PAL_Terminate(); |
| 67 | return PASS; |
| 68 | } |
| 69 |