One-day training of Windows APIs (59) CreateFileMapping and MapViewOfFile Functions

Source: Internet
Author: User
During software development, data sharing between processes is also frequently required. For example, if process A creates computing data, process B displays the Data graph. Such a development method can split a large program into independent small programs to improve the software success rate. It can also be more suitable for the team to develop together and speed up software development. The following describes how to use file ing to share data. First, use the CreateFileMapping function to create a file data handle to be shared, then use MapViewOfFile to obtain the shared memory address, and then use the OpenFileMapping function to open the name of the shared file in another process, in this way, different processes can share data. The CreateFileMapping and MapViewOfFile functions are declared as follows, _ in DWORD dwMaximumSizeLow, _ in_opt LPCSTR lpName); WINBASEAPI _ HANDLE (_ in HANDLE hFile, _ in_opt LPSECURITY_ATTRIBUTES lpFileMappingAttributes, _ in DWORD f LProtect, _ in DWORD encoding, _ in DWORD encoding, _ in_opt LPCWSTR lpName); # ifdef UNICODE # define CreateFileMapping CreateFileMappingW # else # define CreateFileMapping CreateFileMappingA # endif //! Unicode winbaseapi _ HANDLE (_ in HANDLE hFileMappingObject, _ in DWORD dwDesiredAccess, _ in DWORD dwFileOffsetHigh, _ in DWORD dwFileOffsetLow, _ in SIZE_T dwNumberOfBytesToMap ); HFileIs the handle for creating shared files. LpFileMappingAttributesIs the property of file sharing. FlProtectIt is the attribute of the read/write file when the file is mapped. DwMaximumSizeHighIs the size of the file to be shared in high byte. DwMaximumSizeLowIs the low-byte size of file sharing. LpNameIs the name of the shared file object. HFileMappingObjectIs a shared file object. DwDesiredAccessIs the file sharing property. DwFileOffsetHighIs the offset address of the file sharing area. DwFileOffsetLowIs the offset address of the file sharing area. DwNumberOfBytesToMapIs the length of shared data. Example of calling a function: #001 // file sharing. #002 // Cai junsheng 2007/10/27 QQ: 9073204 Shenzhen #003 void FileMapping (void) #004 {# 005 // open the shared file object. #006 m_hMapFile = OpenFileMapping (FILE_MAP_ALL_ACCESS, #007 FALSE, _ T ("TestFileMap "));#008 if (m_hMapFile) #009 {#010 // displays the shared file data. #011 LPTSTR lpMapAddr = (LPTSTR) MapViewOfFile (m_hMapFile, FILE_MAP_ALL_ACCESS, #012 0, 0, 0 );#013 OutputDebugString (lpMapAddr); #014} #015 else #016 {#017 // create a shared file. #018 m_hMapFile = CreateFileMapping (HANDLE) 0 xFFFFFFFF, NULL, #019 PAGE_READWRITE, 0,1024, _ T ("TestFileMap "));#020 #021 // copy data to the shared file. #022 LPTSTR lpMapAddr = (LPTSTR) MapViewOfFile (m_hMapFile, FILE_MAP_ALL_ACCESS, #023 0, 0); #024 std: wstring strTest (_ T ("TestFileMap ")); #025 wcscpy (lpMapAddr, strTest. c_str (); #026 #027 FlushViewOfFile (lpMapAddr, strTest. length () + 1); #028} #029}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.