Article View: borland.public.cppbuilder.nativeapi
Article #5265How to use MapViewOfFile properly
From: Thorsten Kettner
Date: Thu, 22 Sep 2005 16:11
Date: Thu, 22 Sep 2005 16:11
36 lines
2403 bytes
2403 bytes
Hello all,
for the first time I try to read a file by CreateFileMapping and
MapViewOfFile. As I found no complete example in the web, I have
tried to make the best I can get of the MSDN syntax description.
It works, but I'd like to make sure that I've done it correctly.
Please tell me if the parameters are okay (I find it strange for
instance that I must state again and again that I want to have
read access). Then: How do I tell how many bytes to read? Do I
use GetFileSize on the file handle? Or do I get the file size
elsewhere? In some forum I found VirtualQuery being used. Is
that appropriate? It gives me a slightly other file size (some
bytes more then GetFileSize and Windows Explorer do).
TIA for your help. Here is my test code:
HANDLE MyFileHandle = CreateFile("C:\\temp\\test.txt", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if(!MyFileHandle) return;
HANDLE MyFileMappingHandle = CreateFileMapping(MyFileHandle, NULL, PAGE_READONLY, 0, 0, NULL);
if(!MyFileMappingHandle) return;
LPVOID pFileView = MapViewOfFile(MyFileMappingHandle, FILE_MAP_READ, 0, 0, 0);
if(!pFileView) return;
ShowMessage(GetFileSize(MyFileHandle, NULL));
TMemoryBasicInformation MemInfo;
VirtualQuery(pFileView, &MemInfo, sizeof(MemInfo));
ShowMessage(MemInfo.RegionSize);
Memo1->Lines->Text = (char*)pFileView; // TEST, works coz it's a text file
UnmapViewOfFile(pFileView);
CloseHandle(MyFileMappingHandle);
CloseHandle(MyFileHandle);
Technical Details
Message-ID:
<4332d7c6$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,4205730aeea5d81c,start X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Thorsten Kettner" <QUESTIONBYthorsten.kettner@otto.de> Sender: "Thorsten Kettner" <QUESTIONBYthorsten.kettner@otto.de> Reply-To: "Thorsten Kettner" <QUESTIONBYthorsten.kettner@otto.de> Subject: How to use MapViewOfFile properly Newsgroups: borland.public.cppbuilder.nativeapi X-User-Info: 80.85.196.92 NNTP-Posting-Host: 207.105.83.66 Message-ID: <4332d7c6$1@newsgroups.borland.com> Date: 22 Sep 2005 09:11:50 -0700 X-Trace: newsgroups.borland.com 1127405510 207.105.83.66 (22 Sep 2005 09:11:50 -0700) Lines: 38 Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail