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: openfilemappingw.c (test 3)
8**
9** Purpose: Positive test the OpenFileMappingW API.
10** Call OpenFileMappingW to open a named file-mapping
11** object with FILE_MAP_READ access
12**
13**
14**============================================================*/
15#define UNICODE
16#include <palsuite.h>
17
18int __cdecl main(int argc, char *argv[])
19{
20 char buf[] = "this is a test";
21 char ch[1024];
22 HANDLE FileMappingHandle;
23 HANDLE OpenFileMappingHandle;
24 HANDLE OpenFileMappingHandle2;
25 HANDLE lpMapViewAddress;
26 HANDLE lpMapViewAddress2;
27 const int LOWORDERSIZE = 1024;
28 int RetVal = PASS;
29 WCHAR wpMappingFileObject[] = {'m','y','O','b','j','e','c','t','\0'};
30
31
32 /* Initialize the PAL environment.
33 */
34 if(0 != PAL_Initialize(argc, argv))
35 {
36 return FAIL;
37 }
38
39 /* Create a unnamed file-mapping object with file handle FileHandle.
40 */
41 FileMappingHandle = CreateFileMapping(
42 INVALID_HANDLE_VALUE,
43 NULL, /* not inherited */
44 PAGE_READWRITE, /* read and wite */
45 0, /* high-order size */
46 LOWORDERSIZE, /* must be non-zero */
47 wpMappingFileObject);/* named object */
48
49 if(NULL == FileMappingHandle)
50 {
51 Fail("\nFailed to call CreateFileMapping to create mapping object!\n");
52 }
53 if(GetLastError() == ERROR_ALREADY_EXISTS)
54 {
55 Trace("\nFile mapping object already exists!\n");
56 RetVal = FAIL;
57 goto CleanUpOne;
58 }
59
60 /* Open a named file-mapping object with FILE_MAP_ALL_ACCESS access.
61 */
62 OpenFileMappingHandle = OpenFileMapping(
63 FILE_MAP_READ,
64 FALSE,
65 wpMappingFileObject);
66
67 if(NULL == OpenFileMappingHandle)
68 {
69 Trace("\nFailed to Call OpenFileMapping API!\n");
70 RetVal = FAIL;
71 goto CleanUpOne;
72 }
73
74 /* Open a file mapping with FILE_MAP_ALL_ACCESS access,
75 * to verify the FILE_MAP_READ.
76 */
77 OpenFileMappingHandle2 = OpenFileMapping(
78 FILE_MAP_ALL_ACCESS,
79 FALSE,
80 wpMappingFileObject);
81
82 if(NULL == OpenFileMappingHandle2)
83 {
84 Trace("\nFailed to Call OpenFileMapping API!\n");
85 RetVal = FAIL;
86 goto CleanUpTwo;
87 }
88
89 /* Test the opened map view.
90 */
91 lpMapViewAddress = MapViewOfFile(
92 OpenFileMappingHandle,
93 FILE_MAP_READ, /* access code */
94 0, /* high order offset */
95 0, /* low order offset */
96 LOWORDERSIZE); /* number of bytes for map */
97
98 if(NULL == lpMapViewAddress)
99 {
100 Trace("ERROR:%u: Failed to call MapViewOfFile "
101 "API to map a view of file!\n",
102 GetLastError());
103 RetVal = FAIL;
104 goto CleanUpThree;
105 }
106
107 /* Open a map view with FILE_MAP_ALL_ACCESS to verify,
108 * the FILE_MAP_READ view.
109 */
110 lpMapViewAddress2 = MapViewOfFile(
111 OpenFileMappingHandle2,
112 FILE_MAP_ALL_ACCESS, /* access code */
113 0, /* high order offset */
114 0, /* low order offset */
115 LOWORDERSIZE); /* number of bytes for map */
116
117 if(NULL == lpMapViewAddress2)
118 {
119 Trace("2ERROR:%u: Failed to call MapViewOfFile "
120 "API to map a view of file!\n",
121 GetLastError());
122 RetVal = FAIL;
123 goto CleanUpFour;
124 }
125
126 /* Write to the Map View.
127 */
128 memcpy(lpMapViewAddress2, buf, strlen(buf));
129 /* Read from the Map View.
130 */
131 memcpy(ch, (LPCSTR)lpMapViewAddress, LOWORDERSIZE);
132
133 /* Compare what was written to the Map View,
134 * to what was read.
135 */
136 if (memcmp(ch, buf, strlen(buf))!= 0)
137 {
138 Trace("ERROR: MapViewOfFile not equal to file contents "
139 "retrieved \"%s\", expected \"%s\".\n",
140 ch,
141 buf);
142 RetVal = FAIL;
143 goto CleanUpFive;
144 }
145
146CleanUpFive:
147
148 /* Unmap the view of file.
149 */
150 if ( UnmapViewOfFile(lpMapViewAddress2) == FALSE )
151 {
152 Trace("ERROR:%u: Failed to UnmapViewOfFile of \"%0x%lx\".\n",
153 GetLastError(),
154 lpMapViewAddress2);
155 RetVal = FAIL;
156 }
157
158CleanUpFour:
159
160 /* Unmap the view of file.
161 */
162 if ( UnmapViewOfFile(lpMapViewAddress) == FALSE )
163 {
164 Trace("ERROR:%u: Failed to UnmapViewOfFile of \"%0x%lx\".\n",
165 GetLastError(),
166 lpMapViewAddress);
167 RetVal = FAIL;
168 }
169
170CleanUpThree:
171
172 /* Close Handle to opened file mapping.
173 */
174 if ( CloseHandle(OpenFileMappingHandle2) == 0 )
175 {
176 Trace("ERROR:%u: Failed to CloseHandle \"0x%lx\".\n",
177 GetLastError(),
178 OpenFileMappingHandle2);
179 RetVal = FAIL;
180 }
181
182CleanUpTwo:
183
184 /* Close Handle to opened file mapping.
185 */
186 if ( CloseHandle(OpenFileMappingHandle) == 0 )
187 {
188 Trace("ERROR:%u: Failed to CloseHandle \"0x%lx\".\n",
189 GetLastError(),
190 OpenFileMappingHandle);
191 RetVal = FAIL;
192 }
193
194CleanUpOne:
195
196 /* Close Handle to create file mapping.
197 */
198 if ( CloseHandle(FileMappingHandle) == 0 )
199 {
200 Trace("ERROR:%u: Failed to CloseHandle \"0x%lx\".\n",
201 GetLastError(),
202 FileMappingHandle);
203 RetVal = FAIL;
204 }
205
206 /* Terminate the PAL.
207 */
208 PAL_TerminateEx(RetVal);
209 return RetVal;
210}
211