#include <windows.h> #include <process.h> #include <stdlib.h> #include <stdio.h> #include <TCHAR.h> #include <iostream> using namespace std;
class CShareMessage { public: CShareMessage(LPCTSTR lpszFileName,int nFileSize,int bServer); ~CShareMessage(); private: HANDLE m_hFileHandle; LPVOID m_pBuffer; }; CShareMessage::CShareMessage(LPCTSTR lpszFileName,int nFileSize,int bServer) { if(bServer) { m_hFileHandle=CreateFileMapping(INVALID_HANDLE_VALUE,NULL,PAGE_READWRITE,0,nFileSize,lpszFileName); } else { m_hFileHandle=OpenFileMapping(FILE_MAP_ALL_ACCESS,FALSE,lpszFileName); } m_pBuffer=(LPVOID)MapViewOfFile(m_hFileHandle,FILE_MAP_ALL_ACCESS,0,0,0); if(bServer){ *(DWORD*)m_pBuffer=200; }else{ printf("m_pBuffer=%d\n", *(DWORD*)m_pBuffer); } } CShareMessage::~CShareMessage() { UnmapViewOfFile(m_pBuffer); CloseHandle(m_hFileHandle); }
int main(int argc,char **argv[]){ CShareMessage ser(_T("MyData"), sizeof(DWORD), 1); Sleep(10000); }
|