C++进程间共享数据实例
本文实例讲述了C++进程间共享数据的实现方法,分享给大家供大家参考。具体实现方法如下:
int main(int argc, char *argv[])
{
//RecursiveDelete("C:\\20_128\\");
//SelfRun("runModel");
//进程间内存共享
LPCTSTR lpName= "hello";
LPCTSTR lpContent = "123456";
HANDLE hMap = ::OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, lpName);
if (NULL == hMap) //打开失败
{
//创建
hMap = ::CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_EXECUTE_READWRITE, 0, strlen(lpContent), lpName);
if (hMap == NULL)
{
return -1;
}
//写入
LPVOID lpBase = ::MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, 0);
strcpy((char*)lpBase, lpContent);
printf("write %s\n", lpContent);
}
else
{
//打开成功就只读取
LPVOID lpBase = ::MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, 0);
if (lpBase == NULL)
{
return -1;
}
printf("read %s\n",(char*)lpBase);
}
getchar();
return 0;
}
希望本文所述对大家的C++程序设计有所帮助。