function - 整数函数结果 - "Value assigned to ' [函数名称 ]' never used"

标签 function delphi delphi-xe2

我找到了a solution通过使用 Ole 创建 DLL 来提取 zip 文件的内容。我对这个函数进行了自己的修改,但由于某种原因,编译器提示该函数的结果从未使用过......

library unzipper;

{
  title     : UnZip for InnoSetup
  version   : 1.0
  author    : Daniel P. Stasinski
  email     : daniel@genericinbox.com
  begin     : Fri Nov 22 17:31:33 MST 2013
  license   : None
}

uses
  Windows,
  SysUtils,
  ComObj;

const
  SHCONTCH_NOPROGRESSBOX = 4;
  SHCONTCH_AUTORENAME = 8;
  SHCONTCH_RESPONDYESTOALL = 16;
  SHCONTF_INCLUDEHIDDEN = 128;
  SHCONTF_FOLDERS = 32;
  SHCONTF_NONFOLDERS = 64;

  UNZIP_SUCCESS = 0;
  UNZIP_FAIL = -1;

function UnzipFile(ZipFile, TargetFolder: WideString): Integer; stdcall;
var
  shellobj: variant;
  ZipFileV, SrcFile: variant;
  TargetFolderV, DestFolder: variant;
  shellfldritems: variant;
begin
  Result:= UNZIP_FAIL;
  try
    shellobj := CreateOleObject('Shell.Application');
    ZipFileV := string(ZipFile);
    TargetFolderV := string(TargetFolder);
    SrcFile := shellobj.NameSpace(ZipFileV);
    DestFolder := shellobj.NameSpace(TargetFolderV);
    shellfldritems := SrcFile.Items;
    DestFolder.CopyHere(shellfldritems, SHCONTCH_NOPROGRESSBOX or SHCONTCH_RESPONDYESTOALL);
    Result:= UNZIP_SUCCESS;
  except
    on e: exception do begin
      Result:= GetLastError;
    end;
  end;
end;

exports
  UnzipFile;

begin

end.

它给了我信息...

[DCC Hint] Unzipper.dpr(35): H2077 Value assigned to 'UnzipFile' never used



这来自函数中的第一行代码,我将其初始化为常量 -1 - 如果整个功能失败,这是我自己的错误代码。我不相信编译器应该提示这个,但我可能是错的。我总是消除所有编译器提示和警告,但在这种情况下,编译器更像是一个提示者。

这是编译器中的侥幸,还是代码中有问题?

最佳答案

编译器是正确的,代码有问题。 :-)

该函数将返回 UNZIP_SUCCESS如果它有效,或者 GetLastError 的结果如果引发异常。因此,第一个赋值给 Result是不必要的 - 没有会导致 UNZIP_FAIL 的执行路径被退回。

关于function - 整数函数结果 - "Value assigned to ' [函数名称 ]' never used",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21505417/

相关文章:

javascript - 循环遍历 div 数组以确定每个元素是否都有子元素

C++ 是否可以使用对变量的引用来简化这种嵌套循环模式?

c++ - C++函数/方法设计的良好实践

delphi - 在Delphi中实现Mediator模式而不会出现循环引用

sql - 如何在没有安装 SQL native 客户端 2008 的情况下部署 dbExpress 数据库应用程序?

r - 将函数调用转换为 R 中的字符

arrays - 引用类(class)流程

delphi - Chromium 嵌入式框架 : Creating an object fails when using "ExecuteFunctionWithContext"

delphi - 为什么包内缺少 x.dfm?

delphi - Delphi XE2 中的命名空间