Question

I found a solution to extract the contents of a zip file by creating a DLL using Ole. I put my own touch on this function, but for some reason, the compiler complains that the function's result is never used...

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.

It gives me the message...

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

This is coming from the first line of code in the function, which I'm initializing to a constant of -1 - which is my own error code if the entire function fails. I don't believe the compiler should be complaining about this, but I could be wrong. I always exterminate all compiler hints and warnings, but in this case, the compiler is more of a complainer.

Is this a fluke in the compiler, or is something wrong in the code?

Was it helpful?

Solution

The compiler is correct, and there's something wrong in the code. :-)

The function will either return UNZIP_SUCCESS if it works, or the result of GetLastError if an exception is raised. Therefore, the first assignment to Result is unnecessary - there is no path of execution that would cause UNZIP_FAIL to be returned.

OTHER TIPS

If you remove the first line result assignment, there is no execution path that leaves result unassigned. Therefore, UNZIP_FAIL value will never be returned.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top