CopyFile标记为未声明的标识符

时间:2015-06-02 08:44:57

标签: delphi delphi-xe2

创建了一个复制函数,当尝试在其中使用“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;

非常感谢任何帮助, 感谢。

1 个答案:

答案 0 :(得分:7)

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;