1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
4 /*=============================================================
6 ** Source: createfilemapping.c (test 7)
8 ** Purpose: Positive test the CreateFileMapping API.
9 ** Test CreateFileMapping to a "swap" handle with
10 ** access PAGE_READWRITE.
13 **============================================================*/
16 const int MAPPINGSIZE = 2048;
17 HANDLE SWAP_HANDLE = ((VOID *)(-1));
19 int __cdecl main(int argc, char *argv[])
21 char testString[] = "this is a test string";
22 char lpObjectName[] = "myMappingObject";
30 /* Initialize the PAL environment.
32 if(0 != PAL_Initialize(argc, argv))
37 /* Initialize the buffers.
39 memset(results, 0, MAPPINGSIZE);
41 /* Create a named file-mapping object with file handle FileHandle
42 * and with PAGE_READWRITE protection.
44 hFileMapRW = CreateFileMapping(
46 NULL, /*not inherited*/
47 PAGE_READWRITE, /*read write*/
48 0, /*high-order size*/
49 MAPPINGSIZE, /*low-order size*/
50 lpObjectName); /*unnamed object*/
52 if(NULL == hFileMapRW)
54 Fail("ERROR:%u: Failed to create File Mapping.\n",
58 /* Create a map view to the READWRITE file mapping.
60 lpMapViewRW = MapViewOfFile(
62 FILE_MAP_ALL_ACCESS,/* access code */
63 0, /* high order offset*/
64 0, /* low order offset*/
65 MAPPINGSIZE); /* number of bytes for map */
67 if(NULL == lpMapViewRW)
69 Trace("ERROR:%u: Failed to call MapViewOfFile "
70 "API to map a view of file!\n",
77 /* Create a map view to the READWRITE file mapping.
79 lpMapViewRO = MapViewOfFile(
81 FILE_MAP_READ, /* access code */
82 0, /* high order offset*/
83 0, /* low order offset*/
84 MAPPINGSIZE); /* number of bytes for map */
86 if(NULL == lpMapViewRO)
88 Trace("ERROR:%u: Failed to call MapViewOfFile "
89 "API to map a view of file!\n",
95 /* Write the test string to the Map view.
97 memcpy(lpMapViewRW, testString, strlen(testString));
99 /* Read from the second Map view.
101 memcpy(results, (LPCSTR)lpMapViewRO, MAPPINGSIZE);
103 /* Verify the contents of the file mapping,
104 * by comparing what was written to what was read.
106 if (memcmp(results, testString, strlen(testString))!= 0)
108 Trace("ERROR: MapViewOfFile not equal to file contents "
109 "retrieved \"%s\", expected \"%s\".\n",
122 /* Unmap the view of file.
124 if ( UnmapViewOfFile(lpMapViewRO) == FALSE )
126 Trace("ERROR:%u: Failed to UnmapViewOfFile of \"%0x%lx\".\n",
134 /* Unmap the view of file.
136 if ( UnmapViewOfFile(lpMapViewRW) == FALSE )
138 Trace("ERROR:%u: Failed to UnmapViewOfFile of \"%0x%lx\".\n",
147 /* Close Handle to create file mapping.
149 if ( CloseHandle(hFileMapRW) == FALSE )
151 Trace("ERROR:%u: Failed to CloseHandle \"0x%lx\".\n",
158 /* Terminate the PAL.
160 PAL_TerminateEx(RetVal);