C++過程間同享數據實例。本站提示廣大學習愛好者:(C++過程間同享數據實例)文章只能為提供參考,不一定能成為您想要的結果。以下是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++法式設計有所贊助。