I'm running AMD, XP Service Pack 1
Compiling with a .cpp extension using Borland Bcc32

I'm trying to teach myself how to use File Mapping.
This is just the pertinent parts of my program.

Any file can be used for lpstrFile. I'm presently using
a .txt file for testing, but I plan to put in a .bmp
once I get this working.

When I place "GetLastError" under each API, I get "The
operation completed successfully" until I get under
"ReadFile", then I get "The handle is invalid"

// -------------------------------- MAPVIEW STUFF

HANDLE MyFile;
LPVOID lpvObject;
HANDLE hMap;
HANDLE MyDataMap;
char *hData;
DWORD size;

hfile = CreateFile(lpstrFile, GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ,
        (LPSECURITY_ATTRIBUTES) NULL,OPEN_EXISTING, FILE_ATTRIBUTE_READONLY,
        (HANDLE) NULL);

size = GetFileSize(hfile,NULL);

hMap = CreateFileMapping((HANDLE)INVALID_HANDLE_VALUE,NULL,PAGE_READWRITE,0,size,"MyView");

MyDataMap = (HANDLE)OpenFileMapping(FILE_MAP_READ,TRUE,"MyView");

hData = (char*)MapViewOfFile(hMap,FILE_MAP_ALL_ACCESS,0,0,0);

hmem6 = GlobalAlloc(GHND,size);

lpvObject = (LPVOID)GlobalLock(hmem6);

ReadFile(MyDataMap,lpvObject,size,dwRead6,(LPOVERLAPPED)NULL);

UnmapViewOfFile(hMap);
UnmapViewOfFile(MyDataMap);

MyFile = CreateFile("Map.txt", GENERIC_WRITE,FILE_SHARE_WRITE, 
         (LPSECURITY_ATTRIBUTES) NULL,CREATE_ALWAYS, 
          FILE_ATTRIBUTE_NORMAL,(HANDLE) NULL);

WriteFile(MyFile,lpvObject,size,lpNumberOfBytesWritten1,LPOVERLAPPED(NULL));

you don't call ReadFile() to read the mapped file into memory. See the examples and explaination in this article. The whole purpose of memory mapped files is to share data between processes. When Process A writes to the share memory, such as using strcpy() function, Process B will be able to see it immediately as if it had done the strcpy() itself. You do not use file i/o to do this.

Thanks for gettting back to me so quickly. I've downloaded the article you sent me. I'll mark the thread as solved if I am able to now pass my file as mapped.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.