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