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 3) |
8 | ** |
9 | ** Purpose: Positive test the CreateFileMappingW API. |
10 | ** Call CreateFileMapping with access PAGE_READWRITE. |
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 | |
30 | /* Initialize the PAL environment. |
31 | */ |
32 | if(0 != PAL_Initialize(argc, argv)) |
33 | { |
34 | return FAIL; |
35 | } |
36 | |
37 | /* Create a file handle with CreateFile. |
38 | */ |
39 | hFile = CreateFile( lpFileName, |
40 | GENERIC_WRITE|GENERIC_READ, |
41 | FILE_SHARE_READ|FILE_SHARE_WRITE, |
42 | NULL, |
43 | CREATE_ALWAYS, |
44 | FILE_ATTRIBUTE_NORMAL, |
45 | NULL); |
46 | |
47 | if (hFile == INVALID_HANDLE_VALUE) |
48 | { |
49 | Fail("ERROR: %u :unable to create file \"%s\".\n" , |
50 | GetLastError(), |
51 | lpFileName); |
52 | } |
53 | |
54 | /* Initialize the buffers. |
55 | */ |
56 | memset(ch, 0, MAPPINGSIZE); |
57 | |
58 | /* Create a unnamed file-mapping object with file handle FileHandle |
59 | * and with PAGE_READWRITE protection. |
60 | */ |
61 | hFileMapping = CreateFileMapping( |
62 | hFile, |
63 | NULL, /*not inherited*/ |
64 | PAGE_READWRITE, /*read and wite*/ |
65 | 0, /*high-order size*/ |
66 | MAPPINGSIZE, /*low-order size*/ |
67 | NULL); /*unnamed object*/ |
68 | |
69 | if(NULL == hFileMapping) |
70 | { |
71 | Trace("ERROR:%u: Failed to create File Mapping.\n" , |
72 | GetLastError()); |
73 | RetVal = FAIL; |
74 | goto CleanUpOne; |
75 | } |
76 | |
77 | /* maps a view of a file into the address space of the calling process. |
78 | */ |
79 | lpMapViewAddress = MapViewOfFile( |
80 | hFileMapping, |
81 | FILE_MAP_ALL_ACCESS, /* access code */ |
82 | 0, /*high order offset*/ |
83 | 0, /*low order offset*/ |
84 | MAPPINGSIZE); /* number of bytes for map */ |
85 | |
86 | if(NULL == lpMapViewAddress) |
87 | { |
88 | Trace("ERROR:%u: Failed to call MapViewOfFile " |
89 | "API to map a view of file!\n" , |
90 | GetLastError()); |
91 | RetVal = FAIL; |
92 | goto CleanUpTwo; |
93 | } |
94 | |
95 | /* Write to the Map view. |
96 | */ |
97 | memcpy(lpMapViewAddress, buf, strlen(buf)); |
98 | |
99 | /* Read from the Map view. |
100 | */ |
101 | memcpy(ch, (LPCSTR)lpMapViewAddress, MAPPINGSIZE); |
102 | |
103 | /* Copy the MapViewOfFile to buffer, so we can |
104 | * compare with value read from file directly. |
105 | */ |
106 | if (memcmp(ch, buf, strlen(buf))!= 0) |
107 | { |
108 | Trace("ERROR: MapViewOfFile not equal to file contents " |
109 | "retrieved \"%s\", expected \"%s\".\n" , |
110 | ch, buf); |
111 | RetVal = FAIL; |
112 | goto CleanUpThree; |
113 | } |
114 | |
115 | CleanUpThree: |
116 | |
117 | /* Unmap the view of file. |
118 | */ |
119 | if ( UnmapViewOfFile(lpMapViewAddress) == FALSE ) |
120 | { |
121 | Trace("ERROR:%u: Failed to UnmapViewOfFile of \"%0x%lx\".\n" , |
122 | GetLastError(), |
123 | lpMapViewAddress); |
124 | RetVal = FAIL; |
125 | } |
126 | |
127 | CleanUpTwo: |
128 | |
129 | /* Close Handle to opend file mapping. |
130 | */ |
131 | if ( CloseHandle(hFileMapping) == FALSE ) |
132 | { |
133 | Trace("ERROR:%u: Failed to CloseHandle \"0x%lx\".\n" , |
134 | GetLastError(), |
135 | hFileMapping); |
136 | RetVal = FAIL; |
137 | } |
138 | |
139 | CleanUpOne: |
140 | |
141 | /* Close Handle to create file mapping. |
142 | */ |
143 | if ( CloseHandle(hFile) == FALSE ) |
144 | { |
145 | Trace("ERROR:%u: Failed to CloseHandle \"0x%lx\".\n" , |
146 | GetLastError(), |
147 | hFile); |
148 | RetVal = FAIL; |
149 | } |
150 | |
151 | /* Terminate the PAL. |
152 | */ |
153 | PAL_TerminateEx(RetVal); |
154 | return RetVal; |
155 | } |
156 | |
157 | |