我尝试为进程间通信(IPC)创建两个不同的visual c ++控制台应用程序。这两个代码的构建都是成功的。但是,当我尝试调试它时,我得到异常,例如FileMapServer_Parent.exe中的“异常抛出0x00007FFF168E1657(vcruntime140d.dll):0xC0000005:访问冲突写入位置0x0000000000000000 “
//PARENT PROCESS:
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <Tlhelp32.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <cstring>
#include <cctype>
#include <fstream>
using namespace std
int main()
{
cout << "\t\t.....FILEMAPPING SERVER or PARENT....." << endl;
cout << endl;
//Local Variable Definitions
HANDLE hFileMap;
BOOL bResult;
PCHAR lpBuffer = NULL;
char Buffer[256] = "Hello From File Map Server";
size_t szBuffer = size(Buffer);
// STEP 1 : Create File Map
hFileMap = CreateFileMapping(
INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE,
0,
256,
L"LOCAL\\MyFileMap);
if (hFileMap == FALSE)
{
cout << "CreateFileMapping Failed & Error Number - " << GetLastError() << endl;
}
else
cout << "CreateFileMapping Success - " << endl;
// STEP 2 : Map View of File
lpBuffer = (PCHAR)MapViewOfFile(
hFileMap,
FILE_MAP_ALL_ACCESS,
0,
0,
256);
if (lpBuffer == NULL)
{
cout << "MapViewOf File Failes & Error No - " << GetLastError() << endl;
}
else
cout << "MapViewOf File Success " << endl;
//STEP 3 : Copy Memory Function
CopyMemory(lpBuffer,Buffer,szBuffer);
//STEP 4 : Unmap View Of File
bResult = UnmapViewOfFile(lpBuffer);
if (bResult == FALSE)
{
cout << "UnMapViewOfFile Failed & Error No - " << GetLastError() << endl;
}
else
cout << "UnMapViewOfFile FSuccess - " << endl;
system("PAUSE");
return 0;
}
运行父进程时出现异常: [1]:https://i.stack.imgur.com/bomQB.png
//CHILD PREOCESS:
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <Tlhelp32.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <cstring>
#include <cctype>
#include <fstream>
using namespace std;
int main()
{
cout << "\t\t.....FILEMAPPING CLIENT or CHILD....." << endl;
cout << endl;
// Local Variable Definitions
HANDLE hFileMap;
BOOL bResult;
PCHAR lpBuffer = NULL;
// STEP 1 : OpenFileMapping
hFileMap = OpenFileMapping(
FILE_MAP_ALL_ACCESS,
FALSE,
L"LOCAL\\MyFileMap");
if(hFileMap == NULL)
{
cout << "OpenFileMap Failed & error - " << GetLastError() << endl;
}
else
cout << "OpenFileMap success " << endl;
//STEP 2 : MapViewOfFile
lpBuffer = (PCHAR)MapViewOfFile(
hFileMap,
FILE_MAP_ALL_ACCESS,
0,
0,
256);
if (lpBuffer == NULL)
{
cout << "MapViewOf File Failes & Error No - " << GetLastError() << endl;
}
else
cout << "MapViewOf File Success " << endl;
//STEP 3 : Reading the data from File Map Object
cout << "DATA READING FROM PARENT PROCESS--->" <<lpBuffer<< endl;
//STEP 4 : UnMapViewOfFile
bResult = UnmapViewOfFile(lpBuffer);
if (bResult == FALSE)
{
cout << "UnMapViewOfFile Failed & Error No - " << GetLastError() << endl;
}
else
cout << "UnMapViewOfFile FSuccess - " << endl;
//STEP 5 : Close Handle
CloseHandle(hFileMap);
system("PAUSE");
return 0;
}
答案 0 :(得分:1)
问题在于:
hFileMap = CreateFileMapping(
INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE,
0,
256,
L"LOCAL\\MyFileMap");
从内核对象命名空间的文档中,它们区分大小写,因此您必须将LOCAL
更改为Local
才能使其生效。
除了&#34; Global \&#34;前缀,客户端进程可以使用&#34; Local \&#34;前缀以在其会话命名空间中显式创建对象。这些关键字区分大小写。
https://msdn.microsoft.com/en-us/library/aa382954(v=vs.85).aspx
在调试此问题时,如果映射失败,我还将代码更改为退出:
if (hFileMap == NULL)
{
cout << "CreateFileMapping Failed & Error Number - " << GetLastError() << endl;
return -1;
}
else
cout << "CreateFileMapping Success - " << endl;
这避免了原版&#34; LOCAL&#34; (映射失败)。