標籤:pos include rom 管道 could glob 多少 代碼 變數
有時候我們會遇到window進程間共用資料的需求,例如說我想知道系統當前有多少某個進程的執行個體。
我們能夠在程式中定義一個全域變數。初始化為0。每當程式啟動後就加1。當然我們我們能夠藉助第三方介質來儲存這個變數,然後解析。
這樣做必須做到先寫入後解析。不能即時更新資料。假設不考慮其它儲存介質。僅僅是進程中的通訊,應該怎麼做呢?windows提供了一些可行的方法,以下介紹經常使用的兩種。
一、共用資料區段
#include "stdafx.h"#include <Windows.h>};#pragma data_seg("Shared")volatile int g_lAppInstances = 0 ;#pragma data_seg()#pragma comment(linker, "/Section:Shared,RWS")int _tmain(int argc, _TCHAR* argv[]){printf("the instance of app is %d\n", ++g_lAppInstances) ;getchar() ;return 0;}
以上就是在代碼中增加共用資料區段。當執行一個程式的執行個體的同一時候開啟還有一個執行個體。g_lAppInstances會指向同一個記憶體,這樣就能夠做到資料共用。可是這樣的方法的缺點是僅僅能共用一個變數的資料,對於結構體是不行的。
二、記憶體映射文件
第一個程式:
#include "stdafx.h"#include <Windows.h>struct SharedData{int a ;int b;float c ;SharedData(int x, int y, float z){a = x ;b = y ;c = z ;}};const int BUF_SIZE = 256 ;TCHAR szName[] = _T("Global\\MyFileMappingObj") ;int _tmain(int argc, _TCHAR* argv[]){HANDLE hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE,NULL, PAGE_READWRITE, 0, BUF_SIZE, szName) ;if(hMapFile == NULL){_tprintf(_T("Could not create file mapping obj\n")) ;return 1 ;}LPCTSTR pBuf = (LPCTSTR)MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, BUF_SIZE) ;if(pBuf == NULL){_tprintf(_T("could not mapping file\n")) ;CloseHandle(hMapFile) ;return 2 ;}<span style="white-space:pre"></span><pre name="code" class="cpp"><span style="white-space:pre"></span>SharedData *pSd = (SharedData*)pBuf ;_tprintf(_T("the data from IPC2 is %d, %d, %f\n"), pSd->a, pSd->b, pSd->c) ;getchar() ;
UnmapViewOfFile(pBuf) ;CloseHandle(hMapFile) ;return 0;}
第二個程式:
#include "stdafx.h"#include <Windows.h>struct SharedData{int a ;int b;float c ;SharedData(int x, int y, float z){a = x ;b = y ;c = z ;}};const int BUF_SIZE = 256 ;TCHAR szName[] = _T("Global\\MyFileMappingObj") ;int _tmain(int argc, _TCHAR* argv[]){HANDLE hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE,NULL, PAGE_READWRITE, 0, BUF_SIZE, szName) ;if(hMapFile == NULL){_tprintf(_T("Could not create file mapping obj\n")) ;return 1 ;}LPCTSTR pBuf = (LPCTSTR)MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, BUF_SIZE) ;if(pBuf == NULL){_tprintf(_T("could not mapping file\n")) ;CloseHandle(hMapFile) ;return 2 ;}<pre name="code" class="cpp"><span style="white-space:pre"></span>TCHAR s[BUF_SIZE] ;SharedData sd(1, 2, 3.14) ;memcpy((LPVOID)pBuf, &sd, sizeof(sd)) ;
UnmapViewOfFile(pBuf) ;CloseHandle(hMapFile) ;return 0;}
我們先執行第二個程式,然後再執行第一個程式,發現第一個程式列印出了第二個程式一個結構體的值,達到了資料共用的目的。
進程間的通訊還包含剪下板,郵槽。管道等,可是他們本質上都是利用的記憶體映射檔案實現的。
windows核心編程之進程間共用資料