利用DLL在程序中实现文件拷贝
附源程序如下:
library copf;
uses
SysUntils,
Classes;
Procedure copyfile(Sfname.Dfname:string):far;export;//带路径的文件名;
Var
Sourcef,Destinef:file;
NumRead,NumWritten: Integer;
Buf: array[1..4096] of Char;//定义缓冲区;
Begin
AssignFile(Sourcef,Dfname);
Reset(Sourcef,1);
AssignFile(Destinef,Dfname);
Rewrite(Destinef,1);
Repeat
BlockRead(Sourcef,Buf,Size Of(Buf),NumRead);//读源文件;
BlockWrite(Destinef,Buf,NumRead,NumWritten);//写目标文件;
Until (NumRead = 0) or (NumWritten<>NumRead);
CloseFile(Soucef);
CloseFile(Destinef);
End;
Exports
Copyfile;//输出过程;
End;
以上源程序编译后便生成Copf.dll。
现就静态调用举例:
unit Name;
interface
uses
Windows…;
Type
Tzcform=class(Tform)
…
end;
var
Form1: Tform1;
Implementation
Procedure copyfile(Sfname,Dfname:string);far;external
′c:\copf′;//DDL路径名;
{$R *.DFM}
Procedure Tform1.CopyButtonClick(Sender: Tobject);
Begin
…
if fileexist(Sfname)//如果源文件;
then copyfile(Sfname,Dfname);
…
end;