C++ CreateFileMapping 或 MapViewOfFileEx 示例

180it 2021-09-14 PM 649℃ 0条

CreateFileMapping 或 MapViewOfFileEx 示例

#include <windows.h> 
#include <string> 
#include <winbase.h> 
#include <iostream> 
using namespace std;
 
void main()
{
    HANDLE  h;
 
    CHAR *filename;
  
    filename =new CHAR[2500];
    wcout<<"enter the file name:";
    cin.getline (filename,2500);
    h= CreateFile( filename,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN,NULL);
    cout<<filename;
    if (h!=INVALID_HANDLE_VALUE)
    {
    printf("\nFile is opened/created");
    DWORD size = GetFileSize(h, NULL);
    HANDLE hFileMapping = CreateFileMapping(h, NULL,PAGE_READONLY, 0, 0, NULL); 
     
    CloseHandle(h);
 
    MapViewOfFileEx(hFileMapping, FILE_MAP_READ, 0,  0,0,NULL);           
        system("Pause");
 
    UnmapViewOfFile(hFileMapping);
     
    }
    else
    {
    printf("\nUnable to open or create file");
    }
    system ("pause");
 
}
支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

C++ CreateFileMapping 或 MapViewOfFileEx 示例