Windows API一日一練 59 CreateFileMapping和MapViewOfFile函式
阿新 • • 發佈:2018-12-14
在開發軟體過程裡,也經常碰到程序間共享資料的需求。比如
A
程序建立計算資料,
B
程序進行顯示資料的圖形。這樣的開發方式可以把一個大程式分開成獨立的小程式,提高軟體的成功率,也可以更加適合團隊一起開發,加快軟體的開發速度。下面就來使用檔案對映的方式進行共享資料。先要使用函式
CreateFileMapping
來建立一個想共享的檔案資料控制代碼,然後使用
MapViewOfFile
來獲取共享的記憶體地址,然後使用
OpenFileMapping
函式在另一個程序裡開啟共享檔案的名稱,這樣就可以實現不同的程序共享資料。
函式
CreateFileMapping
、
MapViewOfFile
宣告如下:
WINBASEAPI
__out
HANDLE
WINAPI
CreateFileMappingA(
__in HANDLE hFile,
__in_opt LPSECURITY_ATTRIBUTES lpFileMappingAttributes,
__in DWORD flProtect,
__in DWORD dwMaximumSizeHigh,
__in DWORD dwMaximumSizeLow,
__in_opt LPCSTR lpName
);
WINBASEAPI
__out
HANDLE
WINAPI
CreateFileMappingW(
__in HANDLE hFile,
__in_opt LPSECURITY_ATTRIBUTES lpFileMappingAttributes,
__in DWORD flProtect,
__in DWORD dwMaximumSizeHigh,
__in DWORD dwMaximumSizeLow,
__in_opt LPCWSTR lpName
);
#ifdef UNICODE
#define CreateFileMapping CreateFileMappingW
#else
#define CreateFileMapping CreateFileMappingA
#endif // !UNICODE
WINBASEAPI
__out
LPVOID
WINAPI
MapViewOfFile(
__in HANDLE hFileMappingObject,
__in DWORD dwDesiredAccess,
__in DWORD dwFileOffsetHigh,
__in DWORD dwFileOffsetLow,
__in SIZE_T dwNumberOfBytesToMap
);
hFile
是建立共享檔案的控制代碼。
lpFileMappingAttributes
是檔案共享的屬性。
flProtect
是當檔案對映時讀寫檔案的屬性。
dwMaximumSizeHigh
是檔案共享的大小高位位元組。
dwMaximumSizeLow
是檔案共享的大小低位位元組。
lpName
是共享檔案物件名稱。
hFileMappingObject
是共享檔案物件。
dwDesiredAccess
是檔案共享屬性。
dwFileOffsetHigh
是檔案共享區的偏移地址。
dwFileOffsetLow
是檔案共享區的偏移地址。
dwNumberOfBytesToMap
是共享資料長度。
呼叫函式的例子如下:
#001 //
檔案共享。
#002 //
蔡軍生
2007/10/27 QQ:9073204
深圳
#003 void FileMapping(void)
#004 {
#005 //
開啟共享的檔案物件。
#006 m_hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS,
#007 FALSE,_T("TestFileMap"));
#008 if (m_hMapFile)
#009 {
#010 //
顯示共享的檔案資料。
#011 LPTSTR lpMapAddr = (LPTSTR)MapViewOfFile(m_hMapFile,FILE_MAP_ALL_ACCESS,
#012 0,0,0);
#013 OutputDebugString(lpMapAddr);
#014 }
#015 else
#016 {
#017 //
建立共享檔案。
#018 m_hMapFile = CreateFileMapping( (HANDLE)0xFFFFFFFF,NULL,
#019 PAGE_READWRITE,0,1024,_T("TestFileMap"));
#020
#021 //
拷貝資料到共享檔案裡。
#022 LPTSTR lpMapAddr = (LPTSTR)MapViewOfFile(m_hMapFile,FILE_MAP_ALL_ACCESS,
#023 0,0,0);
#024 std::wstring strTest(_T("TestFileMap"));
#025 wcscpy(lpMapAddr,strTest.c_str());
#026
#027 FlushViewOfFile(lpMapAddr,strTest.length()+1);
#028 }
#029 }
2. 俄羅斯方塊遊戲開發
1. WiX安裝工具的使用
http://edu.csdn.net/course/detail/52072. 俄羅斯方塊遊戲開發
http://edu.csdn.net/course/detail/5110
3. boost庫入門基礎
http://edu.csdn.net/course/detail/5029
4.Arduino入門基礎
http://edu.csdn.net/course/detail/4931
5.Unity5.x遊戲基礎入門
http://edu.csdn.net/course/detail/4810
6. TensorFlow API攻略
http://edu.csdn.net/course/detail/4495
7. TensorFlow入門基本教程
http://edu.csdn.net/course/detail/4369
8. C++標準模板庫從入門到精通
http://edu.csdn.net/course/detail/3324
9.跟老菜鳥學C++
http://edu.csdn.net/course/detail/2901
10. 跟老菜鳥學python
http://edu.csdn.net/course/detail/2592
11. 在VC2015裡學會使用tinyxml庫
http://edu.csdn.net/course/detail/2590
12. 在Windows下SVN的版本管理與實戰
http://edu.csdn.net/course/detail/2579
13.Visual Studio 2015開發C++程式的基本使用
http://edu.csdn.net/course/detail/2570
14.在VC2015裡使用protobuf協議
http://edu.csdn.net/course/detail/2582
15.在VC2015裡學會使用MySQL資料庫
http://edu.csdn.net/course/detail/2672
再分享一下我老師大神的人工智慧教程吧。零基礎!通俗易懂!風趣幽默!希望你也加入到我們人工智慧的隊伍中來!http://www.captainbed.net