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