Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

CreateFileMapping() woes

266 views
Skip to first unread message

Trent Kerin

unread,
Oct 25, 2001, 12:20:20 PM10/25/01
to
The following piece of code has three function calls in it(CreateFile,
CreateFileMapping, MapViewOfFile) to open a file that I can write to(later
in the program) as if it were memory. Immediately after each function call I
print the GetLastError() error, the results turn out to be:

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.


Lucian Wischik

unread,
Oct 25, 2001, 12:12:16 PM10/25/01
to
Trent Kerin <e...@egg.egg> wrote:
> 5(access denied) for CreateFileMapping,

This is "kick yourself" time...


> GENERIC_WRITE & GENERIC_READ, // access mode

|

--
Lucian Wischik, Queens' College, Cambridge CB3 9ET. www.wischik.com/lu

Alex Blekhman

unread,
Oct 26, 2001, 2:45:56 AM10/26/01
to
"Trent Kerin" <e...@egg.egg> wrote in message
news:bJWB7.29$kN1.1...@news.interact.net.au...

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.

0 new messages