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 (test 1)
8**
9** Purpose: Positive test the OpenFileMapping API.
10** Call OpenFileMapping to open a named file-mapping
11** object with FILE_MAP_ALL_ACCESS access
12**
13**
14**============================================================*/
15#include <palsuite.h>
16
17int __cdecl main(int argc, char *argv[])
18{
19 HANDLE FileMappingHandle;
20 HANDLE OpenFileMappingHandle;
21 HANDLE lpMapViewAddress;
22 const int LOWORDERSIZE = 1024;
23 char buf[] = "this is a test";
24 char MapObject[] = "myMappingObject";
25 char ch[1024];
26 int err;
27 int RetVal = PASS;
28
29 /* Initialize the PAL environment.
30 */
31 err = PAL_Initialize(argc, argv);
32 if(0 != err)
33 {
34 return FAIL;
35 }
36
37
38 /* Create a named file-mapping object with
39 * file handle FileHandle.
40 */
41 FileMappingHandle = CreateFileMapping(
42 INVALID_HANDLE_VALUE,
43 NULL, /* not inherited */
44 PAGE_READWRITE, /* read and write */
45 0, /* high-order size */
46 LOWORDERSIZE, /* low-order size */
47 MapObject); /* named object */
48
49 if(NULL == FileMappingHandle)
50 {
51 Fail("ERROR:%u:Failed to call CreateFileMapping to "
52 "create a mapping object.\n",
53 GetLastError());
54 }
55 if(GetLastError() == ERROR_ALREADY_EXISTS)
56 {
57 Trace("ERROR:File mapping object already exists\n");
58 RetVal = FAIL;
59 goto CleanUpOne;
60 }
61
62 /* Open a named file-mapping object with
63 * FILE_MAP_ALL_ACCESS access.
64 */
65 OpenFileMappingHandle = OpenFileMapping(
66 FILE_MAP_ALL_ACCESS,
67 TRUE,
68 MapObject );
69
70 if(NULL == OpenFileMappingHandle)
71 {
72 Trace("ERROR:%u:Failed to Call OpenFileMapping API!\n",
73 GetLastError());
74 RetVal = FAIL;
75 goto CleanUpOne;
76 }
77
78 /* Test the opened map view.
79 */
80 lpMapViewAddress = MapViewOfFile(
81 OpenFileMappingHandle,
82 FILE_MAP_ALL_ACCESS, /* access code */
83 0, /* high order offset */
84 0, /* low order offset */
85 LOWORDERSIZE); /* number of bytes for map */
86
87 if(NULL == lpMapViewAddress)
88 {
89 Trace("ERROR:%u: Failed to call MapViewOfFile "
90 "API to map a view of file!\n",
91 GetLastError());
92 RetVal = FAIL;
93 goto CleanUpTwo;
94 }
95
96 /* Write to the Map View.
97 */
98 memcpy(lpMapViewAddress, buf, strlen(buf));
99
100 /* Read from the Map View.
101 */
102 memcpy(ch, (LPCSTR)lpMapViewAddress, LOWORDERSIZE);
103
104 /* Compare what was written to the Map View,
105 * to what was read.
106 */
107 if (memcmp(ch, buf, strlen(buf))!= 0)
108 {
109 Trace("ERROR: MapViewOfFile not equal to file contents "
110 "retrieved \"%s\", expected \"%s\".\n",
111 ch, buf);
112 RetVal = FAIL;
113 goto CleanUpThree;
114
115 }
116
117CleanUpThree:
118
119 /* Unmap the view of file.
120 */
121 if ( UnmapViewOfFile(lpMapViewAddress) == FALSE )
122 {
123 Trace("ERROR:%u: Failed to UnmapViewOfFile of \"%0x%lx\".\n",
124 GetLastError(),
125 lpMapViewAddress);
126 RetVal = FAIL;
127 }
128
129CleanUpTwo:
130
131 /* Close Handle to opend file mapping.
132 */
133 if ( CloseHandle(OpenFileMappingHandle) == 0 )
134 {
135 Trace("ERROR:%u: Failed to CloseHandle \"0x%lx\".\n",
136 GetLastError(),
137 OpenFileMappingHandle);
138 RetVal = FAIL;
139 }
140
141CleanUpOne:
142
143 /* Close Handle to create file mapping.
144 */
145 if ( CloseHandle(FileMappingHandle) == 0 )
146 {
147 Trace("ERROR:%u: Failed to CloseHandle \"0x%lx\".\n",
148 GetLastError(),
149 FileMappingHandle);
150 RetVal = FAIL;
151 }
152
153
154 /* Terminat the PAL.*/
155 PAL_TerminateEx(RetVal);
156 return RetVal;
157}
158