Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CopyFile flagged as undeclared identifier

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.

like image 245
Sharpie Avatar asked Jan 17 '26 06:01

Sharpie


1 Answers

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;
like image 163
David Heffernan Avatar answered Jan 19 '26 19:01

David Heffernan