How can I write from a file to shared memory using the Win32 API?
I have this code:
hFile = CreateFile("input.map",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
hMapFile = CreateFileMapping(hFile,
NULL,
PAGE_READWRITE,
0,
0,
TEXT("SharedObject"));
lpMapAddress = (LPTSTR) MapViewOfFile(hMapFile,
FILE_MAP_ALL_ACCESS,
0,
0,
0);
ReadFile(
hFile,
lpMapAddress,
75,
&bytesRead,
NULL);
sprintf((char*)lpMapAddress, "<output 1>");
printf((char*) lpMapAddress);
However, the printf call only returns "< output 1 >" and not the contents of the file.
EDIT: Found the problem. I'm writing to the input file when I call sprintf. But I still don't know why...