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: createfilemapping.c (test 3)
8**
9** Purpose: Positive test the CreateFileMapping API.
10** Call CreateFileMapping with access PAGE_READWRITE.
11**
12**
13**============================================================*/
14#include <palsuite.h>
15
16const int MAPPINGSIZE = 2048;
17
18int __cdecl main(int argc, char *argv[])
19{
20
21 HANDLE hFile;
22 char buf[] = "this is a test string";
23 char ch[2048];
24 char lpFileName[] = "test.tmp";
25 HANDLE hFileMapping;
26 LPVOID lpMapViewAddress;
27 int RetVal = PASS;
28
29 /* Initialize the PAL environment.
30 */
31 if(0 != PAL_Initialize(argc, argv))
32 {
33 return FAIL;
34 }
35
36 /* Create a file handle with CreateFile.
37 */
38 hFile = CreateFile( lpFileName,
39 GENERIC_WRITE|GENERIC_READ,
40 FILE_SHARE_READ|FILE_SHARE_WRITE,
41 NULL,
42 CREATE_ALWAYS,
43 FILE_ATTRIBUTE_NORMAL,
44 NULL);
45
46 if (hFile == INVALID_HANDLE_VALUE)
47 {
48 Fail("ERROR: %u :unable to create file \"%s\".\n",
49 GetLastError(),
50 lpFileName);
51 }
52
53 /* Initialize the buffers.
54 */
55 memset(ch, 0, MAPPINGSIZE);
56
57 /* Create a unnamed file-mapping object with file handle FileHandle
58 * and with PAGE_READWRITE protection.
59 */
60 hFileMapping = CreateFileMapping(
61 hFile,
62 NULL, /*not inherited*/
63 PAGE_READWRITE, /*read and wite*/
64 0, /*high-order size*/
65 MAPPINGSIZE, /*low-order size*/
66 NULL); /*unnamed object*/
67
68 if(NULL == hFileMapping)
69 {
70 Trace("ERROR:%u: Failed to create File Mapping.\n",
71 GetLastError());
72 RetVal = FAIL;
73 goto CleanUpOne;
74 }
75
76 /* maps a view of a file into the address space of the calling process.
77 */
78 lpMapViewAddress = MapViewOfFile(
79 hFileMapping,
80 FILE_MAP_ALL_ACCESS, /* access code */
81 0, /*high order offset*/
82 0, /*low order offset*/
83 MAPPINGSIZE); /* 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, MAPPINGSIZE);
101
102 /* Copy the MapViewOfFile to buffer, so we can
103 * compare with value read from file directly.
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
114CleanUpThree:
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
126CleanUpTwo:
127
128 /* Close Handle to opend file mapping.
129 */
130 if ( CloseHandle(hFileMapping) == FALSE )
131 {
132 Trace("ERROR:%u: Failed to CloseHandle \"0x%lx\".\n",
133 GetLastError(),
134 hFileMapping);
135 RetVal = FAIL;
136 }
137
138CleanUpOne:
139
140 /* Close Handle to create file mapping.
141 */
142 if ( CloseHandle(hFile) == FALSE )
143 {
144 Trace("ERROR:%u: Failed to CloseHandle \"0x%lx\".\n",
145 GetLastError(),
146 hFile);
147 RetVal = FAIL;
148 }
149
150 /* Terminate the PAL.
151 */
152 PAL_TerminateEx(RetVal);
153 return RetVal;
154}
155
156