如何访问C中父进程创建的内存映射文件(Windows)

时间:2013-12-03 10:55:54

标签: c memory-management memory-mapped-files memory-mapping

我创建了映射文件并从父进程中的视图中读取。但是,我无法让子进程访问内存映射文件。你能检查下面的代码并帮我解决一下吗?

这是我的子流程代码:

#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;

}

这是父代码:

#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();
}

1 个答案:

答案 0 :(得分:1)

OpenFileMapping期望映射名称本身不是文件。

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

创建映射时,请在CreateFileMapping

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

可能在您的孩子和父母之间共享映射名称,并使用随机名称。