| 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 1) | 
|---|
| 8 | ** | 
|---|
| 9 | ** Purpose: Positive test the CreateFileMappingW API. | 
|---|
| 10 | **          Call CreateFileMappingW with access PAGE_READONLY. | 
|---|
| 11 | ** | 
|---|
| 12 | ** | 
|---|
| 13 | **============================================================*/ | 
|---|
| 14 | #define UNICODE | 
|---|
| 15 | #include <palsuite.h> | 
|---|
| 16 |  | 
|---|
| 17 | const int MAPPINGSIZE = 2048; | 
|---|
| 18 |  | 
|---|
| 19 | int __cdecl main(int argc, char *argv[]) | 
|---|
| 20 | { | 
|---|
| 21 |  | 
|---|
| 22 | HANDLE  hFile; | 
|---|
| 23 | char    buf[] = "this is a test string"; | 
|---|
| 24 | char    ch[2048]; | 
|---|
| 25 | WCHAR   lpFileName[] = {'t','e','s','t','.','t','m','p','\0'}; | 
|---|
| 26 | DWORD   dwBytesWritten; | 
|---|
| 27 | BOOL    err; | 
|---|
| 28 | int     RetVal = PASS; | 
|---|
| 29 |  | 
|---|
| 30 | HANDLE hFileMapping; | 
|---|
| 31 | LPVOID lpMapViewAddress; | 
|---|
| 32 |  | 
|---|
| 33 | /* Initialize the PAL environment. | 
|---|
| 34 | */ | 
|---|
| 35 | if(0 != PAL_Initialize(argc, argv)) | 
|---|
| 36 | { | 
|---|
| 37 | return FAIL; | 
|---|
| 38 | } | 
|---|
| 39 |  | 
|---|
| 40 | /* Create a file handle with CreateFile. | 
|---|
| 41 | */ | 
|---|
| 42 | hFile = CreateFile( lpFileName, | 
|---|
| 43 | GENERIC_WRITE|GENERIC_READ, | 
|---|
| 44 | FILE_SHARE_READ|FILE_SHARE_WRITE, | 
|---|
| 45 | NULL, | 
|---|
| 46 | CREATE_ALWAYS, | 
|---|
| 47 | FILE_ATTRIBUTE_NORMAL, | 
|---|
| 48 | NULL); | 
|---|
| 49 |  | 
|---|
| 50 | if (hFile == INVALID_HANDLE_VALUE) | 
|---|
| 51 | { | 
|---|
| 52 | Fail( "ERROR: %u :unable to create file \"%s\".\n", | 
|---|
| 53 | GetLastError(), | 
|---|
| 54 | lpFileName); | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|
| 57 | /* Initialize the buffers. | 
|---|
| 58 | */ | 
|---|
| 59 | memset(ch,  0, MAPPINGSIZE); | 
|---|
| 60 |  | 
|---|
| 61 | /* Write to the File handle. | 
|---|
| 62 | */ | 
|---|
| 63 | err = WriteFile(hFile, | 
|---|
| 64 | buf, | 
|---|
| 65 | strlen(buf), | 
|---|
| 66 | &dwBytesWritten, | 
|---|
| 67 | NULL); | 
|---|
| 68 |  | 
|---|
| 69 | if (err == FALSE) | 
|---|
| 70 | { | 
|---|
| 71 | Trace( "ERROR: %u :unable to write to file handle " | 
|---|
| 72 | "hFile=0x%lx\n", | 
|---|
| 73 | GetLastError(), | 
|---|
| 74 | hFile); | 
|---|
| 75 | RetVal = FAIL; | 
|---|
| 76 | goto CleanUpOne; | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | /* Flush to the hard-drive. | 
|---|
| 80 | */ | 
|---|
| 81 | FlushFileBuffers(hFile); | 
|---|
| 82 |  | 
|---|
| 83 | /* Create a unnamed file-mapping object with file handle FileHandle | 
|---|
| 84 | * and with PAGE_READWRITE protection. | 
|---|
| 85 | */ | 
|---|
| 86 | hFileMapping = CreateFileMapping( | 
|---|
| 87 | hFile, | 
|---|
| 88 | NULL,               /*not inherited*/ | 
|---|
| 89 | PAGE_READONLY,      /*read and wite*/ | 
|---|
| 90 | 0,                  /*high-order size*/ | 
|---|
| 91 | 0,                  /*low-order size*/ | 
|---|
| 92 | NULL);              /*unnamed object*/ | 
|---|
| 93 |  | 
|---|
| 94 | if(NULL == hFileMapping) | 
|---|
| 95 | { | 
|---|
| 96 | Trace( "ERROR:%u: Failed to create File Mapping.\n", | 
|---|
| 97 | GetLastError()); | 
|---|
| 98 | RetVal = FAIL; | 
|---|
| 99 | goto CleanUpOne; | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | /* maps a view of a file into the address space of the calling process. | 
|---|
| 103 | */ | 
|---|
| 104 | lpMapViewAddress = MapViewOfFile( | 
|---|
| 105 | hFileMapping, | 
|---|
| 106 | FILE_MAP_READ,  /* access code */ | 
|---|
| 107 | 0,              /*high order offset*/ | 
|---|
| 108 | 0,              /*low order offset*/ | 
|---|
| 109 | 0);             /* number of bytes for map */ | 
|---|
| 110 |  | 
|---|
| 111 | if(NULL == lpMapViewAddress) | 
|---|
| 112 | { | 
|---|
| 113 | Trace( "ERROR:%u: Failed to call MapViewOfFile " | 
|---|
| 114 | "API to map a view of file!\n", | 
|---|
| 115 | GetLastError()); | 
|---|
| 116 | RetVal = FAIL; | 
|---|
| 117 | goto CleanUpTwo; | 
|---|
| 118 | } | 
|---|
| 119 |  | 
|---|
| 120 | /* Copy the MapViewOfFile to buffer, so we can | 
|---|
| 121 | * compare with value read from file directly. | 
|---|
| 122 | */ | 
|---|
| 123 | memcpy(ch, (LPCSTR)lpMapViewAddress, MAPPINGSIZE); | 
|---|
| 124 | if (memcmp(ch, buf, strlen(buf))!= 0) | 
|---|
| 125 | { | 
|---|
| 126 | Trace( "ERROR: MapViewOfFile not equal to file contents " | 
|---|
| 127 | "retrieved \"%s\", expected \"%s\".\n", | 
|---|
| 128 | ch, buf); | 
|---|
| 129 | RetVal = FAIL; | 
|---|
| 130 | goto CleanUpThree; | 
|---|
| 131 | } | 
|---|
| 132 |  | 
|---|
| 133 | CleanUpThree: | 
|---|
| 134 |  | 
|---|
| 135 | /* Unmap the view of file. | 
|---|
| 136 | */ | 
|---|
| 137 | if ( UnmapViewOfFile(lpMapViewAddress) == FALSE ) | 
|---|
| 138 | { | 
|---|
| 139 | Trace( "ERROR:%u: Failed to UnmapViewOfFile of \"%0x%lx\".\n", | 
|---|
| 140 | GetLastError(), | 
|---|
| 141 | lpMapViewAddress); | 
|---|
| 142 | RetVal = FAIL; | 
|---|
| 143 | } | 
|---|
| 144 |  | 
|---|
| 145 | CleanUpTwo: | 
|---|
| 146 |  | 
|---|
| 147 | /* Close Handle to opend file mapping. | 
|---|
| 148 | */ | 
|---|
| 149 | if ( CloseHandle(hFileMapping) == FALSE ) | 
|---|
| 150 | { | 
|---|
| 151 | Trace( "ERROR:%u: Failed to CloseHandle \"0x%lx\".\n", | 
|---|
| 152 | GetLastError(), | 
|---|
| 153 | hFileMapping); | 
|---|
| 154 | RetVal = FAIL; | 
|---|
| 155 | } | 
|---|
| 156 |  | 
|---|
| 157 | CleanUpOne: | 
|---|
| 158 |  | 
|---|
| 159 | /* Close Handle to create file mapping. | 
|---|
| 160 | */ | 
|---|
| 161 | if ( CloseHandle(hFile) == FALSE ) | 
|---|
| 162 | { | 
|---|
| 163 | Trace( "ERROR:%u: Failed to CloseHandle \"0x%lx\".\n", | 
|---|
| 164 | GetLastError(), | 
|---|
| 165 | hFile); | 
|---|
| 166 | RetVal = FAIL; | 
|---|
| 167 | } | 
|---|
| 168 |  | 
|---|
| 169 | /* Terminate the PAL. | 
|---|
| 170 | */ | 
|---|
| 171 | PAL_TerminateEx(RetVal); | 
|---|
| 172 | return RetVal; | 
|---|
| 173 | } | 
|---|
| 174 |  | 
|---|
| 175 |  | 
|---|