Search code examples
inno-setuppascalscript

Inno Setup invoke or replicate native Windows file copy operation


I know that the FileCopy function can be used to copy files in the [Code] section and this works fine for most purposes. However, is there any way to invoke the native Windows file copy operation, so that the standard Windows file copy dialog with progress, time remaining etc is shown (i.e. the same as doing Ctrl+C, followed by Ctrl+V), which will also allow the user to cancel or pause the copy operation mid-process? Or, better still, is there a way to replicate similar functionality directly in the [Code] section?


Solution

  • Use SHFileOperation with FO_COPY:

    type
      TSHFileOpStruct = record
        hwnd: HWND;
        wFunc: UINT;
        pFrom: string;
        pTo: string;
        fFlags: Word;
        fAnyOperationsAborted: BOOL; 
        hNameMappings: HWND;
        lpszProgressTitle: string;
      end; 
    
    const
      FO_COPY            = $0002;
      FOF_NOCONFIRMATION = $0010;
    
    function SHFileOperation(lpFileOp: TSHFileOpStruct): Integer;
      external 'SHFileOperationW@shell32.dll stdcall';
    
    procedure ShellCopyFile;
    var
      FromPath: string;
      ToPath: string;
      FileOp: TSHFileOpStruct;
    begin
      FromPath :=
        ExpandConstant('{src}\data1.dat') + #0 +
        ExpandConstant('{src}\data2.dat') + #0;
      ToPath := ExpandConstant('{app}') + #0;
    
      FileOp.hwnd := WizardForm.Handle;
      FileOp.wFunc := FO_COPY;
      FileOp.pFrom := FromPath;
      FileOp.pTo := ToPath;
      FileOp.fFlags := FOF_NOCONFIRMATION;
    
      if SHFileOperation(FileOp) <> 0 then
      begin
        MsgBox('Copying failed.', mbError, MB_OK);
      end;  
    end;
    
    procedure CurStepChanged(CurStep: TSetupStep);
    begin
      if CurStep = ssInstall then
      begin
        ShellCopyFile;
      end;
    end;
    

    The code is for Unicode version of Inno Setup (the only version as of Inno Setup 6).

    enter image description here