Memory Mapped File

Code 2012.08.01 23:51
Memory Mapped File

Memory Mapped File

http://www.codeproject.com/KB/threads/ipc_tute.aspx?display=Print

 

 

  • 초기화#

파일열기 - 생략가능

CreateFile()

 

  • 파일 맵핑 생성#

const LPCTSTR sMemoryFileName = _T("D9287E19-6F9E-45fa-897C-D392F73A0F2F");
HANDLE m_hFileMapping = CreateFileMapping(
        INVALID_HANDLE_VALUE,           // system paging file
        NULL,                           // security attributes
        PAGE_READWRITE,                 // protection
        0,                              // high-order DWORD of size
        dwMemoryFileSize*sizeof(TCHAR), // low-order DWORD of size
        sMemoryFileName);               // name

 

  • 파일 맵핑과 메모리 연결#

     LPVOIDm_pViewOfFile = MapViewOfFile(
                m_hFileMapping,             // handle to file-mapping object
                FILE_MAP_ALL_ACCESS,        // desired access
                0,
                0,
                0);                         // map all file

    HANDLE m_hMutex = CreateMutex(
            NULL,                       // security attributes
            FALSE,                      // initial owner
            sMutexFileName);            // name

 

  • 동기화 개체 추가#

HANDLE m_hMutex = CreateMutex(
        NULL,                       // security attributes
        FALSE,                      // initial owner
        sMutexFileName);            // name

 

  • 사용#
    • 읽기#

          TCHAR s[dwMemoryFileSize];
          if ( WaitForSingleObject(m_hMutex, dwMaxWait) == WAIT_OBJECT_0 )   {//동기화 추가
                  lstrcpy(s, (LPCTSTR) m_pViewOfFile);//읽기
                  ReleaseMutex(m_hMutex);//동기화 추가
          }

    • 쓰기#

      lstrcpy( (LPTSTR) m_pViewOfFile, s);

 

  • 닫기#

    UnmapViewOfFile(m_pViewOfFile);
    CloseHandle(m_hFileMapping);
    CloseHandle(m_hMutex);//동기화 개체 제거

 

 

신고

'Code' 카테고리의 다른 글

ADO 'ATL OLEDB' DAO ODBC  (0) 2012.08.18
APM 설치  (0) 2012.08.11
Memory Mapped File  (0) 2012.08.01
멀티 쓰레드 동기화(Syncronazation)  (0) 2012.08.01
Quake 3 source build  (0) 2008.11.18
filemon , regmon source  (0) 2008.11.08
Posted by 코덴스

댓글을 달아 주세요