Reputation: 1776
I am trying to follow this tutorial on MSDN: Creating Named Shared Memory.
Unfortunately, I get NULL
from CreateFileMapping()
. The file shmfile.txt
exists on my file system, so I thought no problems should occur, the mapping would be created, and the file would take its role as my shared memory object.
What am I doing wrong?
This is my code:
...
#define BUF_SIZE 256
TCHAR szName[]=TEXT("C:\\Users\\joe\\shmfolder\\shmfile.txt");
int main(){
HANDLE hMapFile; // filehandle
LPCTSTR pBuf;
hMapFile = CreateFileMapping(
INVALID_HANDLE_VALUE, // use paging file
NULL, // default security
PAGE_READWRITE, // read/write access
0, // maximum object size (high-order DWORD)
BUF_SIZE, // maximum object size (low-order DWORD)
szName); // name of mapping object
if (hMapFile == NULL)
{
_tprintf(TEXT("Could not create file mapping object (%d).\n"), GetLastError());
return 1;
}
...
}
Upvotes: 0
Views: 5003
Reputation: 490118
Right now, you're telling CreateFileMapping
to create the mapping in the page file, then use the name of your existing file as the name of the file mapping.
The name you give for a file mapping object can have local\
or global\
as a prefix, but can't contain any other back-slashes.
If you want to map your pre-existing file as a shared memory region, you'd do something on this general order:
TCHAR szName[]=TEXT("C:\\Users\\joe\\shmfolder\\shmfile.txt");
TCHAR szMapName[]=TEXT("SharedFile");
HANDLE file = CreateFile(szName, ...);
HANDLE mapping = CreateFileMapping(file, ..., szMapName);
Upvotes: 5