Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to get data from a file without reading it into memory Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
20-Oct-02
Category
Files Operation
Language
Delphi 2.x
Views
128
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Jonas Bilinkevicius

Is there a way to point a pointer to a text data file on a hard drive with out 
reading into memory. Here is the problem. I have a third-party DLL that requires a 
pointer to a large char string 10000 + chars. If I were to read into memory and 
then call the DLL it could cause problems.

Answer:

You can use Mapped Files. A mapped file is a region in memory that is mapped to a 
file on disk. After you map a file to memory you get a pointer to the memory region 
and use it like any other pointer - Window will load and unload pages from the file 
to memory as needed. Here is a very simple implementation of a mapped file. It is 
used only to read data from the file so you might want to change it to also allow 
writing. After you create an instance, the Content property is a pointer to the 
file content.

1   { ... }
2   type
3     TMappedFile = class
4     private
5       FMapping: THandle;
6       FContent: PChar;
7       FSize: Integer;
8       procedure MapFile(const FileName: string);
9     public
10      constructor Create(const FileName: string);
11      destructor Destroy; override;
12      property Content: PChar read FContent;
13      property Size: Integer read FSize;
14    end;
15  
16  implementation
17  
18  uses
19    sysutils;
20  
21  { TMappedFile }
22  
23  constructor TMappedFile.Create(const FileName: string);
24  begin
25    inherited Create;
26    MapFile(FileName);
27  end;
28  
29  destructor TMappedFile.Destroy;
30  begin
31    UnmapViewOfFile(FContent);
32    CloseHandle(FMapping);
33    inherited;
34  end;
35  
36  procedure TMappedFile.MapFile(const FileName: string);
37  var
38    FileHandle: THandle;
39  begin
40    FileHandle := FileOpen(FileName, fmOpenRead or fmShareDenyWrite);
41    Win32Check(FileHandle <> 0);
42    try
43      FSize := GetFileSize(FileHandle, nil);
44      FMapping := CreateFileMapping(FileHandle, nil, PAGE_READONLY, 0, 0, nil);
45      Win32Check(FMapping <> 0);
46    finally
47      FileClose(FileHandle);
48    end;
49    FContent := MapViewOfFile(FMapping, FILE_MAP_READ, 0, 0, 0);
50    Win32Check(FContent <> nil);
51  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC