delphi - CopyFile 标记为未声明的标识符

标签 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;

任何帮助将不胜感激, 谢谢。

最佳答案

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;

关于delphi - CopyFile 标记为未声明的标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30591327/

相关文章:

delphi - Delphi-NX库中的任意长整数?

delphi - RTTI - 为什么在某些情况下 TTypedData.CompType 为零?

德尔福XE2 : Jumping to an anchor in CHM?

delphi - 在 Delphi 中用泛型替换重载?

multithreading - 全线程 : Create a task wrapper/modify a task that adds some extra pre- and post processing to an alredy existing task

Delphi XE4 我必须包含什么才能使用函数 FmxHandleToObjC

delphi - 匿名方法 - 变量捕获与值捕获

delphi - 在 Delphi 中存储一组值

delphi - Indy 正在更改我的 URL 中的二进制数据

delphi - 当我的窗体大于屏幕时,为什么我的 Delphi 窗体控件会被裁剪?