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