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