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