Created a Copy Function and when trying to use 'CopyFile' in it, at point of compiling, Delphi flags it as undeclared identifier.
Am I doing something wrong?
function TdmData.CopyAFile(Sourcefile, DestFile: string): boolean;
var Src, Dest : PChar;
begin
Src := StrAlloc(Length(SourceFile)+1);
Dest := StrAlloc(Length(DestFile)+1);
try
StrPCopy(Src,SourceFile);
StrPCopy(Dest,DestFile);
result := (CopyFile(Src,Dest,FALSE));
finally
StrDispose(Src);
StrDispose(Dest);
end;
end;
Any help would be much appreciated, Thanks.
CopyFile is a Windows API function that is declared in the Windows unit. You need to add Windows to your uses clause. Or, if you are using fully qualified namespaces, add Winapi.Windows.
The code should also avoid performing heap allocations and string copies that are in fact not necessary. You can replace the code in the question with this:
uses
Windows; // or Winapi.Windows
....
function TdmData.CopyAFile(const SourceFile, DestFile: string): Boolean;
begin
Result := CopyFile(PChar(SourceFile), PChar(DestFile), False);
end;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With