windows核心编程之进程间共享数据

来源:互联网 发布:网络电视看不了直播 编辑:程序博客网 时间:2024/06/08 18:15

  有时候我们会遇到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;}

我们先运行第二个程序,然后再运行第一个程序,发现第一个程序打印出了第二个程序一个结构体的值,达到了数据共享的目的。

进程间的通信还包括剪切板,邮槽,管道等,但是他们本质上都是利用的内存映射文件实现的。

0 0