应用程序的的修改及读取。(100分)

  • 主题发起人 主题发起人 cjsh
  • 开始时间 开始时间
C

cjsh

Unregistered / Unconfirmed
GUEST, unregistred user!
我要做一个程序加入一些信息到应用程序的尾部,然后这个程序就从尾部读取这些信息。
因为我的程序要非常小所以不能用Delphi里面的流操作。我的程序才几十K,都是用SDK写的。生成程序大小无防,就是这个几十K的程序要有这种功能,能够读取自己的尾部信息,用什么方法呢。有人会用汇编吗,或者用Windows函数也可以,只要能保证我这个程序不会很大。
 
怎样用WindowsApi函数操作
1、CreateFile函数分别打开文件和新建文件
2、ReadFile读取新文件中的内容。
3、SetFilePointer把打开的文件定位到文件尾部。
4、WriteFile把新文件写入到打开文件的尾部。

各位能否把下面这段代码转换成用这些函数操作,还有用到的函数没有写出来。
//合并文件
Function Cjt_AddtoFile(SourceFile, TargetFile: String):Boolean;
var
Target, Source: TFileStream;
MyFileSize: integer;
begin
try
Source := TFileStream.Create(SourceFile, fmOpenRead or fmShareExclusive);
Target := TFileStream.Create(TargetFile, fmOpenWrite or fmShareExclusive);
try
Target.Seek(0, soFromEnd);//往尾部添加资源
Target.CopyFrom(Source, 0);
MyFileSize := Source.Size + Sizeof(MyFileSize);//计算资源大小,并写入辅程尾部
Target.WriteBuffer(MyFileSize, Sizeof(MyFileSize));
finally
Target.Free;
Source.Free;
end;
except
Result:=False;
Exit;
end;
Result:=True;
end;
 
还有下面的也要转换
Function Cjt_IniLoad(SourceFile, TargetFile: String):Boolean;
var
Source: TFileStream;
Target: TMemoryStream;
MyFileSize: integer;
begin
Source := TFileStream.Create(SourceFile, fmOpenRead or fmShareDenyNone);
Target := TMemoryStream.Create;
try
try
Source.Seek(-sizeof(MyFileSize), soFromEnd);
Source.ReadBuffer(MyFileSize, sizeof(MyFileSize));//读出资源
Source.Seek(-MyFileSize, soFromEnd);//定位到资源开始位置
Target.CopyFrom(Source, MyFileSize-sizeof(MyFileSize));//取出资源
Target.SaveToFile(TargetFile);
finally
Source.Free;
Target.Free;
end;
except
Result:=False;
Exit;
end;
Result:=True;
end;
-----------------------
完全用API操作。
 
{the data structure for our information}
Information = record
Name: array[0..255] of char;
Title: array[0..255] of char;
Age: Integer;
end;

var
Form1: TForm1;

implementation

procedure TForm1.Button1Click(Sender: TObject);
var
FileHandle: THandle; // a handle to the opened file
TheInfo: Information; // holds our information

NumBytesWritten: DWORD; // variable to track bytes written
Security: TSecurityAttributes; // opened file security attributes
begin
{copy the supplied information to the data structure}
StrPCopy(TheInfo.Name, Edit1.Text);
StrPCopy(TheInfo.Title, Edit2.Text);
TheInfo.Age := StrToInt(Edit3.Text);

{create a generic, binary file}
Security.nLength := SizeOf(TSecurityAttributes);
Security.bInheritHandle := FALSE;

FileHandle := CreateFile('TempFile.nfo', GENERIC_WRITE, 0, @Security,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL or
FILE_FLAG_SEQUENTIAL_SCAN, 0);

{write the data in the data structure directly to the file}
WriteFile(FileHandle, TheInfo, SizeOf(Information), NumBytesWritten, nil);

{implicitly set the end of the file. this could be used to set
the end of the file to somewhere in the middle}

SetEndOfFile(FileHandle);

{force any cached file buffers to write the file to disk}
FlushFileBuffers(FileHandle);

{close the file}
CloseHandle(FileHandle);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
FileHandle: THandle; // a handle to the opened file
TheInfo: Information; // holds our information
NumBytesRead: DWORD; // holds the number of bytes read

Security: TSecurityAttributes; // opened file security attributes
TheTitle: array[0..255] of char; // holds a title string
begin
{open the existing file for reading}
Security.nLength := SizeOf(TSecurityAttributes);
Security.bInheritHandle := FALSE;
FileHandle := CreateFile('TempFile.nfo', GENERIC_READ, 0, @Security,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL or
FILE_FLAG_SEQUENTIAL_SCAN, 0);

{indicate an error if the file does not exist}
if FileHandle=INVALID_HANDLE_VALUE then
begin
ShowMessage('No file exists yet. Press the ''Write to file'' button to '+
'create a file.');
Exit;
end;

{lock the entire file so no other process may use it}
LockFile(FileHandle, 0, 0, SizeOf(Information), 0);

{read in a block of information and store it in our data structure}

ReadFile(FileHandle, TheInfo, SizeOf(Information), NumBytesRead, nil);

{display the information}
Label7.Caption := TheInfo.Name;
Label8.Caption := TheInfo.Title;
Label9.Caption := IntToStr(TheInfo.Age);

{the title is located 256 bytes into the file from the beginning (this is
how long the Name string is), so reposition the file pointer for reading}
SetFilePointer(FileHandle, SizeOf(TheInfo.Name), nil, FILE_BEGIN);

{read one particular string from the file}

ReadFile(FileHandle, TheTitle, SizeOf(TheTitle), NumBytesRead, nil);

{display the string}
Label11.Caption := TheTitle;

{unlock and close the file}
UnlockFile(FileHandle, 0, 0, SizeOf(Information), 0);
CloseHandle(FileHandle);
end;
 
接受答案了.
 
后退
顶部