Inno Setup invoke or replicate native Windows file copy operation

心不动则不痛 提交于 2019-12-08 03:31:12

问题


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?


回答1:


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).



来源:https://stackoverflow.com/questions/45244210/inno-setup-invoke-or-replicate-native-windows-file-copy-operation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!