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