Search code examples
cmemory-managementmemory-mapped-filesmemory-mapping

How to access memory mapped file created by parent process in C (Windows)


I created the mapped file and read from view in the parent process. However, I couldn't make the child process access the memory-mapped file. Can you please examine the code below and help me figure it out?

Here is my child process code:

#include <Windows.h>
#include <stdio.h>
#define SIZE 1024 *40
int main(){

HANDLE hLogMap;
char* pView, *start;

if((hLogMap = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, "a.txt")) == NULL)
{
   fprintf(stderr,"Unable to open memory mapping: %d\n", GetLastError());
}

if((pView = (char *) MapViewOfFile(hLogMap, FILE_MAP_ALL_ACCESS, 0, 0, SIZE)) == NULL)
{
   fprintf(stderr,"Unable to create map view: %d\n", GetLastError());

}


start=pView;

while(pView < start + SIZE){
    fprintf(stderr,*(pView++));
    pView++;
}

system("pause");

 return 1;

}

Here is the parent code:

#include <Windows.h>
#include <stdio.h>
#define BUFF_SIZE 1024 *40
#define FILE_NAME "a.txt"

void exitPrompt(){
system("pause");
exit(0);
}

void main(){

STARTUPINFO si;
PROCESS_INFORMATION pi;
SECURITY_ATTRIBUTES sa;
char* lpCommandLine="child.exe";
HANDLE hFile, hMMap, handle;
char * pFile, * start, *rFile;

SecureZeroMemory(&si, sizeof(STARTUPINFO)); 
si.cb = sizeof(STARTUPINFO);
SecureZeroMemory(&pi, sizeof(PROCESS_INFORMATION));

sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
si.dwFlags = STARTF_USESTDHANDLES;
si.hStdError = GetStdHandle(STD_ERROR_HANDLE);

if (!CreateProcess(NULL, lpCommandLine, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi))
    {
        printf("unable to create new process!");
        system("pause");
        exit(0);
    }
    else
    {
        printf("parent is now working!\n");
        handle = pi.hProcess;
    }

if((hFile = CreateFile( FILE_NAME,
                        GENERIC_READ|GENERIC_WRITE,
                        FILE_SHARE_WRITE|FILE_SHARE_READ,
                        NULL,
                        OPEN_ALWAYS,
                        FILE_ATTRIBUTE_NORMAL,
                        NULL)) 
                                == INVALID_HANDLE_VALUE){
    fprintf(stderr,"Unable to open file %s: %d\n",FILE_NAME,GetLastError());
    exitPrompt();
}

if((hMMap = CreateFileMapping(  hFile,
                                &sa,
                                PAGE_READWRITE,
                                0,
                                BUFF_SIZE,
                                NULL)) 
                                            == NULL){
    fprintf(stderr,"Unable to create memory mapping: %d\n",GetLastError());
    exitPrompt();
}

if( ( pFile = (char *) MapViewOfFile(   hMMap,
                                        FILE_MAP_WRITE,
                                        0,
                                        0,
                                        BUFF_SIZE)) 
                                                    == NULL)
{
    fprintf(stderr,"Unable to create map view: %d\n",GetLastError());
    exitPrompt();
}
start = pFile;
while(pFile < start + BUFF_SIZE){
    *(pFile++) = 'f';
    *(pFile++) = 'i';
    *(pFile++) = 'g';
    *(pFile++) = 'e';
    *(pFile++) = 'n';
    *(pFile++) = 'g';
    *(pFile++) = '_';
    *(pFile++) = 10; //in ascii 10 is new line
}

if( ( rFile = (char *) MapViewOfFile(   hMMap,
                                        FILE_MAP_READ,
                                        0,
                                        0,
                                        BUFF_SIZE)) 
                                                    == NULL)
{
    fprintf(stderr,"Unable to create map view: %d\n",GetLastError());
    exitPrompt();
}


rFile=start;

/*while(rFile < start + BUFF_SIZE){
    printf("%c",*(rFile));
    rFile++;
}*/

WaitForSingleObject(handle,INFINITE);

CloseHandle(handle);
CloseHandle(pi.hThread);

CloseHandle(hMMap);
CloseHandle(hFile);


exitPrompt();
}

Solution

  • The OpenFileMapping expected the mapping name not the file in itself.

    if((hLogMap = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, "mapping")) == NULL)
    ...
    

    When you create the mapping provide the mapping name in the CreateFileMapping.

    if((hMMap = CreateFileMapping(  hFile,
                                &sa,
                                PAGE_READWRITE,
                                0,
                                BUFF_SIZE,
                                "mapping")) 
                                            == NULL){
        fprintf(stderr,"Unable to create memory mapping: %d\n",GetLastError());
    

    Possibly share the mapping name between your child and parent and use a random name.