183(file already exists, not really an error) for CreateFile,
5(access denied) for CreateFileMapping,
6(invalid handle) for MapViewOfFile.
I really, really can't work out why CreateFileMapping won't work(access
denied), and I was hoping some kind soul would know why. I suspect it might
be because I'm not very sure of what the DWORDs are for(specifically, the
significance of high-order and low-order, I just tried to make it look a bit
like the code I've found).
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#include <windows.h>
HANDLE hFile, fileMappingObject;
LPVOID mappedView;
void main(void)
{
hFile = CreateFile("Ass3C.dat", // file name
GENERIC_WRITE & GENERIC_READ, // access mode
FILE_SHARE_WRITE, // share mode
NULL, // security
CREATE_ALWAYS, // how to create
FILE_ATTRIBUTE_NORMAL, // file attributes
NULL); // handle to template
cout << GetLastError() << endl;
fileMappingObject = CreateFileMapping(hFile, // handle to file
NULL, //security
PAGE_READWRITE, // protection
0, // high-order DWORD of size
1024, // low-order DWORD of size
NULL); // object name
cout << GetLastError() << endl;
mappedView = MapViewOfFile(fileMappingObject,
FILE_MAP_WRITE, // access mode
0, // high-order DWORD of offset
0, // low-order DWORD of offset
0); // number of bytes to map
cout << GetLastError() << endl;
Thankyou in advance for any reply.
This is "kick yourself" time...
> GENERIC_WRITE & GENERIC_READ, // access mode
|
--
Lucian Wischik, Queens' College, Cambridge CB3 9ET. www.wischik.com/lu
Small note besides what Lucian said. Calling GetLastError is
meaningless without checking return value of the function. You should
call GetLastError only when function fails and documentation says that
GetLastError will retrieve error code. Otherwise, GetLastError will
report result of some internal calls that you don't care of until
function doesn't fail.