刚刚完成的一个进程通信及托管非托管混合编程的总结之CFileMapping

参见《 刚刚完成的一个进程通信及托管非托管混合编程的总结之概述》和《 刚刚完成的一个进程通信及托管非托管混合编程的总结之自定义数据

FileMapping.h:

#pragma  once

#include 
" MyData.h "

#ifndef   __FILEMAPPING_H__  
#define    __FILEMAPPING_H__  

class    CFileMapping    
{  
public:  
    CFileMapping();  
    
~CFileMapping();  

    
//   Operations  
    bool   Initialize(LPCTSTR,   int);  
    
void   Write(char*);  
    
void   Read(char*&);

private:  
    HANDLE   m_hFileHandle;  
    LPVOID   m_pViewOfFile;  
    
int   m_nFileSize;  
}
;  

#endif     // __FILEMAPPING_H__
 
FileMapping.cpp:
#include  " StdAfx.h "
#include 
" FileMapping.h "

#ifdef   _DEBUG  
#define    new   DEBUG_NEW  
#undef    THIS_FILE  
static     char    THIS_FILE[]    =    __FILE__;  
#endif   

////  
//    Constructor   /   Destructor      //   
////  
CFileMapping::CFileMapping()  
{  
    m_hFileHandle   
=   NULL;  
    m_pViewOfFile   
=   NULL;  
    m_nFileSize   
=   0;  
}
  


CFileMapping::
~ CFileMapping()  
{  
    
if(   m_pViewOfFile   !=   NULL   )  
    
{  
        
if(   UnmapViewOfFile(m_pViewOfFile)   ==   0)  
            TRACE(
"Unable   to   unmap   view   of   file.   Error   is   %d",   GetLastError());  
    }
  
    
if(   m_hFileHandle!=   NULL)  
        CloseHandle(m_hFileHandle);  
}
  


////  
//    Operations                                                                                                                                    //   
////  
bool    CFileMapping::Initialize(LPCTSTR   pFileName,    int    pFileSize)  
{  
    m_nFileSize   
=   pFileSize;  
    
//   creates   as   well   as   opens   a   file   map.  
    m_hFileHandle   =   CreateFileMapping( INVALID_HANDLE_VALUE,                               //   handle   to   file  
        NULL, //   security  
        PAGE_READWRITE,                                           //   protection  
        0,                         //   high-order   DWORD   of   size  
        m_nFileSize,                         //   low-order   DWORD   of   size  
        pFileName                                             //   object   name  
        );  

    
if(   !m_hFileHandle   )   {  
        TRACE(
"Unable   to   create   file   mapping.   Error   is   %d",   GetLastError());  
        
return   false;  
    }
  

    
//   Map   the   file   into   the   current   address   space.  
    m_pViewOfFile   =   MapViewOfFile( m_hFileHandle, //   handle   to   file-mapping   object  
        FILE_MAP_ALL_ACCESS, //   access   mode  
        0//   high-order   DWORD   of   offset  
        0//   low-order   DWORD   of   offset  
        0 //   number   of   bytes   to   map  
        );  
    
if(   m_pViewOfFile   ==   NULL   )   {  
        TRACE(
"Unable   to   map   to   the   current   address   space.   Error   is   %d",   GetLastError());  
        
return   false;  
    }
  
    
return   true;  
}
  


//    Since   the   file   Object   is   in   the   same   address   space,   we   can   use   just   like   dynamic  
//    allocated   memory      
void    CFileMapping::Write( char *    pData)  
{  
    ASSERT(   m_nFileSize   
!=   0   );  
    memcpy(   m_pViewOfFile,   pData,   m_nFileSize   );  
}
  


void    CFileMapping::Read( char *&    pBuffer)  
{  
    ASSERT(   m_nFileSize   
!=   0   );  
    memcpy(   pBuffer,   m_pViewOfFile,   m_nFileSize   );  
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值