#include <iostream.h>
#include <windows.h>
static long
get_alignment(void)
{
SYSTEM_INFO sys_info;
GetSystemInfo(&sys_info);
return(sys_info.dwAllocationGranularity);
}
int
main(int, char **)
{
const long align = get_alignment();
HANDLE file_handle = CreateFile("file.txt",
GENERIC_READ|GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (file_handle == INVALID_HANDLE_VALUE) {
cerr << "Unable to create mappable file\n";
return(1);
}
HANDLE map_handle = CreateFileMapping(file_handle, NULL,
PAGE_READWRITE, 0, align, "mymap");
if (map_handle == INVALID_HANDLE_VALUE) {
cerr << "Unable to create actual mapping\n";
return(1);
}
char *pointer = (char *) MapViewOfFile(map_handle, FILE_MAP_WRITE,
0, 0, align);
if (pointer == NULL) {
cerr << "Unable to map into address space\n";
return(1);
}
strcpy(pointer, "hello\n");
//"Should" clean things up here, but since we didn't,
//HIU: map_handle still accessible and valid
//HIU: file_handle still accessible and valid
return(0);
}
(C) Copyright IBM Corporation 1992, 2010.