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