CopyFile 标记为未声明的标识符

CopyFile flagged as undeclared identifier

创建了一个复制函数,当试图在其中使用 'CopyFile' 时,在编译时,Delphi 将其标记为未声明的标识符。

我是不是做错了什么?

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;

如有任何帮助,我们将不胜感激, 谢谢

CopyFile 是在 Windows 单元中声明的 Windows API 函数。您需要将 Windows 添加到您的 uses 子句中。或者,如果您使用完全限定的名称空间,请添加 Winapi.Windows.

代码还应避免执行实际上不必要的堆分配和字符串复制。您可以将问题中的代码替换为:

uses
  Windows; // or Winapi.Windows

....

function TdmData.CopyAFile(const SourceFile, DestFile: string): Boolean;
begin
  Result := CopyFile(PChar(SourceFile), PChar(DestFile), False);
end;