window中进程间如何通信

类别:VC语言 点击:0 评论:0 推荐:

PI已替你想好了一个间接办法,你可用一个“共享名”申请一块共享
内存块,进行读写:

HANDLE  GetShare(char * &ShareP,int size,char *ShareName)
      {  ShareP申请的内存块地址,size字节数,ShareName共享名
            HANDLE fh=CreateFileMapping((HANDLE)-1,0,
                            PAGE_READWRITE,0,
                            Size,
                            ShareName);
            ShareP=(char *)MapViewOfFile(fh,
                            FILE_MAP_ALL_ACCESS,
                            0,0,0);
            if (GetLastError()!=ERROR_ALREADY_EXISTS)
                ZeroMemory(ShareP,size);  // 共享区初始化
            return(fh);
      }

char * ShareP=NULL;
void  test()  // 申请一块128个字节的字符数组
      {
          HANDLE fh=GetShare(ShareP,128,"ShareForMyProg");
          for (int i=0;i<128;i++)
              ShareP[i]=i;
          CloseHandle(fh);
      }
HANDLE  GetShare(char * &ShareP,int size,char *ShareName)
      {  ShareP申请的内存块地址,size字节数,ShareName共享名
            HANDLE fh=CreateFileMapping((HANDLE)-1,0,
                            PAGE_READWRITE,0,
                            Size,
                            ShareName);
            ShareP=(char *)MapViewOfFile(fh,
                            FILE_MAP_ALL_ACCESS,
                            0,0,0);
            if (GetLastError()!=ERROR_ALREADY_EXISTS)
                ZeroMemory(ShareP,size);  // 共享区初始化
            return(fh);
      }


      char * ShareP=NULL;
// 申请一块128个字节的字符数组
   
      HANDLE fh=GetShare(ShareP,128,"ShareForMyProg"); file://申请一块128个字节符数组
   
      for (int i=0;i<128;i++)  file://使用、测试
            ShareP[i]=i;
        .....................

      CloseHandle(fh);        // 释放共享内存句柄

本文地址:http://com.8s8s.com/it/it3573.htm