| 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: openfilemappinga.c (test 2) |
| 8 | ** |
| 9 | ** Purpose: Positive test the OpenFileMapping API. |
| 10 | ** Call OpenFileMapping to open a named file-mapping |
| 11 | ** object with FILE_MAP_WRITE access |
| 12 | ** |
| 13 | ** |
| 14 | **============================================================*/ |
| 15 | #include <palsuite.h> |
| 16 | |
| 17 | int __cdecl main(int argc, char *argv[]) |
| 18 | { |
| 19 | HANDLE FileMappingHandle; |
| 20 | HANDLE OpenFileMappingHandle; |
| 21 | HANDLE lpMapViewAddress; |
| 22 | HANDLE OpenFileMappingHandle2; |
| 23 | HANDLE lpMapViewAddress2; |
| 24 | const int LOWORDERSIZE = 1024; |
| 25 | char MapObject[] = "myMappingObject" ; |
| 26 | char buf[] = "this is a test" ; |
| 27 | char ch[1024]; |
| 28 | int RetVal = PASS; |
| 29 | |
| 30 | |
| 31 | /* Initialize the PAL environment. |
| 32 | */ |
| 33 | if(0 != PAL_Initialize(argc, argv)) |
| 34 | { |
| 35 | return FAIL; |
| 36 | } |
| 37 | |
| 38 | /* Create a named file-mapping object with file handle FileHandle. |
| 39 | */ |
| 40 | FileMappingHandle = CreateFileMapping( |
| 41 | INVALID_HANDLE_VALUE, |
| 42 | NULL, /* not inherited */ |
| 43 | PAGE_READWRITE, /* read and write */ |
| 44 | 0, /* high-order size */ |
| 45 | LOWORDERSIZE, /* low-order size */ |
| 46 | MapObject); /* named object */ |
| 47 | |
| 48 | |
| 49 | if(NULL == FileMappingHandle) |
| 50 | { |
| 51 | Fail("\nFailed to call CreateFileMapping to create " |
| 52 | "a mapping object!\n" ); |
| 53 | } |
| 54 | if(GetLastError() == ERROR_ALREADY_EXISTS) |
| 55 | { |
| 56 | Trace("\nFile mapping object already exists!\n" ); |
| 57 | RetVal = FAIL; |
| 58 | goto CleanUpOne; |
| 59 | } |
| 60 | |
| 61 | /* Open a named file-mapping object with FILE_MAP_WRITE access. |
| 62 | */ |
| 63 | OpenFileMappingHandle = OpenFileMapping( |
| 64 | FILE_MAP_WRITE, |
| 65 | FALSE, |
| 66 | MapObject); |
| 67 | |
| 68 | if(NULL == OpenFileMappingHandle) |
| 69 | { |
| 70 | Trace("\nFailed to Call OpenFileMapping API!\n" ); |
| 71 | RetVal = FAIL; |
| 72 | goto CleanUpOne; |
| 73 | } |
| 74 | |
| 75 | /* Open a named file-mapping object with |
| 76 | * FILE_MAP_ALL_ACCESS access, to verify |
| 77 | * the FILE_MAP_WRITE access map. |
| 78 | */ |
| 79 | OpenFileMappingHandle2 = OpenFileMapping( |
| 80 | FILE_MAP_ALL_ACCESS, |
| 81 | FALSE, |
| 82 | MapObject); |
| 83 | |
| 84 | if(NULL == OpenFileMappingHandle2) |
| 85 | { |
| 86 | Trace("\nFailed to Call OpenFileMapping API!\n" ); |
| 87 | RetVal = FAIL; |
| 88 | goto CleanUpTwo; |
| 89 | } |
| 90 | |
| 91 | /* Create map view of the open mapping that has |
| 92 | * FILE_MAP_WRITE access. |
| 93 | */ |
| 94 | lpMapViewAddress = MapViewOfFile( |
| 95 | OpenFileMappingHandle, |
| 96 | FILE_MAP_WRITE, /* access code */ |
| 97 | 0, /* high order offset */ |
| 98 | 0, /* low order offset */ |
| 99 | LOWORDERSIZE); /* number of bytes for map */ |
| 100 | |
| 101 | if(NULL == lpMapViewAddress) |
| 102 | { |
| 103 | Trace("ERROR:%u: Failed to call MapViewOfFile " |
| 104 | "API to map a view of file!\n" , |
| 105 | GetLastError()); |
| 106 | RetVal = FAIL; |
| 107 | goto CleanUpThree; |
| 108 | } |
| 109 | |
| 110 | /* Create map view of the open mapping that has |
| 111 | * FILE_MAP_ALL_ACCESS access. |
| 112 | */ |
| 113 | lpMapViewAddress2 = MapViewOfFile( |
| 114 | OpenFileMappingHandle2, |
| 115 | FILE_MAP_ALL_ACCESS, /* access code */ |
| 116 | 0, /* high order offset */ |
| 117 | 0, /* low order offset */ |
| 118 | LOWORDERSIZE); /* number of bytes for map */ |
| 119 | |
| 120 | if(NULL == lpMapViewAddress2) |
| 121 | { |
| 122 | Trace("ERROR:%u: Failed to call MapViewOfFile " |
| 123 | "API to map a view of file!\n" , |
| 124 | GetLastError()); |
| 125 | RetVal = FAIL; |
| 126 | goto CleanUpFour; |
| 127 | } |
| 128 | |
| 129 | /* Write to the Map View. |
| 130 | */ |
| 131 | memcpy(lpMapViewAddress, buf, strlen(buf)); |
| 132 | |
| 133 | /* Read from the Map View. |
| 134 | */ |
| 135 | memcpy(ch, (LPCSTR)lpMapViewAddress, LOWORDERSIZE); |
| 136 | |
| 137 | /* Compare what was written to the Map View, |
| 138 | * to what was read. |
| 139 | */ |
| 140 | if (memcmp(ch, buf, strlen(buf))!= 0) |
| 141 | { |
| 142 | Fail("ERROR: MapViewOfFile not equal to file contents " |
| 143 | "retrieved \"%s\", expected \"%s\".\n" , |
| 144 | ch, buf); |
| 145 | RetVal = FAIL; |
| 146 | goto CleanUpFive; |
| 147 | } |
| 148 | |
| 149 | CleanUpFive: |
| 150 | |
| 151 | /* Unmap the view of file. |
| 152 | */ |
| 153 | if ( UnmapViewOfFile(lpMapViewAddress2) == FALSE ) |
| 154 | { |
| 155 | Trace("ERROR:%u: Failed to UnmapViewOfFile of \"%0x%lx\".\n" , |
| 156 | GetLastError(), |
| 157 | lpMapViewAddress2); |
| 158 | RetVal = FAIL; |
| 159 | } |
| 160 | |
| 161 | CleanUpFour: |
| 162 | |
| 163 | /* Unmap the view of file. |
| 164 | */ |
| 165 | if ( UnmapViewOfFile(lpMapViewAddress) == FALSE ) |
| 166 | { |
| 167 | Trace("ERROR:%u: Failed to UnmapViewOfFile of \"%0x%lx\".\n" , |
| 168 | GetLastError(), |
| 169 | lpMapViewAddress); |
| 170 | RetVal = FAIL; |
| 171 | } |
| 172 | |
| 173 | CleanUpThree: |
| 174 | |
| 175 | /* Close Handle to opened file mapping. |
| 176 | */ |
| 177 | if ( CloseHandle(OpenFileMappingHandle2) == 0 ) |
| 178 | { |
| 179 | Trace("ERROR:%u: Failed to CloseHandle \"0x%lx\".\n" , |
| 180 | GetLastError(), |
| 181 | OpenFileMappingHandle2); |
| 182 | RetVal = FAIL; |
| 183 | } |
| 184 | |
| 185 | CleanUpTwo: |
| 186 | |
| 187 | /* Close Handle to opened file mapping. |
| 188 | */ |
| 189 | if ( CloseHandle(OpenFileMappingHandle) == 0 ) |
| 190 | { |
| 191 | Trace("ERROR:%u: Failed to CloseHandle \"0x%lx\".\n" , |
| 192 | GetLastError(), |
| 193 | OpenFileMappingHandle); |
| 194 | RetVal = FAIL; |
| 195 | } |
| 196 | |
| 197 | CleanUpOne: |
| 198 | |
| 199 | /* Close Handle to create file mapping. |
| 200 | */ |
| 201 | if ( CloseHandle(FileMappingHandle) == 0 ) |
| 202 | { |
| 203 | Trace("ERROR:%u: Failed to CloseHandle \"0x%lx\".\n" , |
| 204 | GetLastError(), |
| 205 | FileMappingHandle); |
| 206 | RetVal = FAIL; |
| 207 | } |
| 208 | |
| 209 | |
| 210 | /* Terminate the PAL. |
| 211 | */ |
| 212 | PAL_TerminateEx(RetVal); |
| 213 | return RetVal; |
| 214 | } |
| 215 | |