| 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: createfilemappingw.c (test 7) |
| 8 | ** |
| 9 | ** Purpose: Positive test the CreateFileMappingW API. |
| 10 | ** Test CreateFileMappingW to a "swap" handle with |
| 11 | ** access PAGE_READWRITE. |
| 12 | ** |
| 13 | ** |
| 14 | **============================================================*/ |
| 15 | #define UNICODE |
| 16 | #include <palsuite.h> |
| 17 | |
| 18 | const int MAPPINGSIZE = 2048; |
| 19 | HANDLE SWAP_HANDLE = ((VOID *)(-1)); |
| 20 | |
| 21 | int __cdecl main(int argc, char *argv[]) |
| 22 | { |
| 23 | char testString[] = "this is a test string" ; |
| 24 | WCHAR lpObjectName[] = {'m','y','O','b','j','e','c','t','\0'}; |
| 25 | char results[2048]; |
| 26 | int RetVal = PASS; |
| 27 | |
| 28 | HANDLE hFileMapRW; |
| 29 | LPVOID lpMapViewRW; |
| 30 | LPVOID lpMapViewRO; |
| 31 | |
| 32 | /* Initialize the PAL environment. |
| 33 | */ |
| 34 | if(0 != PAL_Initialize(argc, argv)) |
| 35 | { |
| 36 | return FAIL; |
| 37 | } |
| 38 | |
| 39 | /* Initialize the buffers. |
| 40 | */ |
| 41 | memset(results, 0, MAPPINGSIZE); |
| 42 | |
| 43 | /* Create a named file-mapping object with file handle FileHandle |
| 44 | * and with PAGE_READWRITE protection. |
| 45 | */ |
| 46 | hFileMapRW = CreateFileMapping( |
| 47 | SWAP_HANDLE, |
| 48 | NULL, /*not inherited*/ |
| 49 | PAGE_READWRITE, /*read write*/ |
| 50 | 0, /*high-order size*/ |
| 51 | MAPPINGSIZE, /*low-order size*/ |
| 52 | lpObjectName); /*unnamed object*/ |
| 53 | |
| 54 | if(NULL == hFileMapRW) |
| 55 | { |
| 56 | Fail("ERROR:%u: Failed to create File Mapping.\n" , |
| 57 | GetLastError()); |
| 58 | } |
| 59 | |
| 60 | /* Create a map view to the READWRITE file mapping. |
| 61 | */ |
| 62 | lpMapViewRW = MapViewOfFile( |
| 63 | hFileMapRW, |
| 64 | FILE_MAP_ALL_ACCESS,/* access code */ |
| 65 | 0, /* high order offset*/ |
| 66 | 0, /* low order offset*/ |
| 67 | MAPPINGSIZE); /* number of bytes for map */ |
| 68 | |
| 69 | if(NULL == lpMapViewRW) |
| 70 | { |
| 71 | Trace("ERROR:%u: Failed to call MapViewOfFile " |
| 72 | "API to map a view of file!\n" , |
| 73 | GetLastError()); |
| 74 | RetVal = FAIL; |
| 75 | goto CleanUpOne; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | /* Create a map view to the READWRITE file mapping. |
| 80 | */ |
| 81 | lpMapViewRO = MapViewOfFile( |
| 82 | hFileMapRW, |
| 83 | FILE_MAP_READ, /* access code */ |
| 84 | 0, /* high order offset*/ |
| 85 | 0, /* low order offset*/ |
| 86 | MAPPINGSIZE); /* number of bytes for map */ |
| 87 | |
| 88 | if(NULL == lpMapViewRO) |
| 89 | { |
| 90 | Trace("ERROR:%u: Failed to call MapViewOfFile " |
| 91 | "API to map a view of file!\n" , |
| 92 | GetLastError()); |
| 93 | RetVal = FAIL; |
| 94 | goto CleanUpTwo; |
| 95 | } |
| 96 | |
| 97 | /* Write the test string to the Map view. |
| 98 | */ |
| 99 | memcpy(lpMapViewRW, testString, strlen(testString)); |
| 100 | |
| 101 | /* Read from the second Map view. |
| 102 | */ |
| 103 | memcpy(results, (LPCSTR)lpMapViewRO, MAPPINGSIZE); |
| 104 | |
| 105 | /* Verify the contents of the file mapping, |
| 106 | * by comparing what was written to what was read. |
| 107 | */ |
| 108 | if (memcmp(results, testString, strlen(testString))!= 0) |
| 109 | { |
| 110 | Trace("ERROR: MapViewOfFile not equal to file contents " |
| 111 | "retrieved \"%s\", expected \"%s\".\n" , |
| 112 | results, |
| 113 | testString); |
| 114 | RetVal = FAIL; |
| 115 | goto CleanUpThree; |
| 116 | } |
| 117 | |
| 118 | /* Test successful. |
| 119 | */ |
| 120 | RetVal = PASS; |
| 121 | |
| 122 | CleanUpThree: |
| 123 | |
| 124 | /* Unmap the view of file. |
| 125 | */ |
| 126 | if ( UnmapViewOfFile(lpMapViewRO) == FALSE ) |
| 127 | { |
| 128 | Trace("ERROR:%u: Failed to UnmapViewOfFile of \"%0x%lx\".\n" , |
| 129 | GetLastError(), |
| 130 | lpMapViewRO); |
| 131 | RetVal = FAIL; |
| 132 | } |
| 133 | |
| 134 | CleanUpTwo: |
| 135 | |
| 136 | /* Unmap the view of file. |
| 137 | */ |
| 138 | if ( UnmapViewOfFile(lpMapViewRW) == FALSE ) |
| 139 | { |
| 140 | Trace("ERROR:%u: Failed to UnmapViewOfFile of \"%0x%lx\".\n" , |
| 141 | GetLastError(), |
| 142 | lpMapViewRW); |
| 143 | RetVal = FAIL; |
| 144 | } |
| 145 | |
| 146 | |
| 147 | CleanUpOne: |
| 148 | |
| 149 | /* Close Handle to create file mapping. |
| 150 | */ |
| 151 | if ( CloseHandle(hFileMapRW) == FALSE ) |
| 152 | { |
| 153 | Trace("ERROR:%u: Failed to CloseHandle \"0x%lx\".\n" , |
| 154 | GetLastError(), |
| 155 | hFileMapRW); |
| 156 | RetVal = FAIL; |
| 157 | } |
| 158 | |
| 159 | |
| 160 | /* Terminate the PAL. |
| 161 | */ |
| 162 | PAL_TerminateEx(RetVal); |
| 163 | return RetVal; |
| 164 | } |
| 165 | |
| 166 | |