2

我想为 2 张 CD 上的旧程序制作安装程序,我想直接从光盘安装文件。

启动时,安装程​​序应检查是否存在某个文件,这意味着第一张 CD 已插入 cd rom 驱动器。这是该任务的代码:

[Files]
Source: {code: ??? }; Destination: {app}; flags:external;

[Code]
procedure InitializeWizard();
begin
  if not FileExists('A:\Resource\CD1.GOB') xor
         FileExists('B:\Resource\CD1.GOB') xor
         // and so on, for every drive letter...
         FileExists('Z:\Resource\CD1.GOB') then
         Repeat
           if MsgBox('Insert the first CD!', mbInformation, MB_OKCANCEL) = IDCANCEL then
           ExitProcess(0);
         Until  FileExists('A:\Resource\CD1.GOB') or
                FileExists('B:\Resource\CD1.GOB') or
                // going through all letters again...
                FileExists('Z:\Resource\CD1.GOB') = true;

所以这按预期工作。如果未插入 CD,因此找不到文件,将显示一条消息,要求用户插入 CD。

但我想知道是否有更好的方法来增加驱动器号,因为这是一团糟。

其次,如何保存完整的文件路径并将其传递给 [Files] 部分?

我希望你能帮我解决这个问题!

更新:

我再次尝试并想出了这个:

procedure CurPageChanged(CurPageID: Integer);
begin
  if CurPageId = wpWelcome then
  begin
    WizardForm.NextButton.Enabled := False; 
    repeat
      for i:=0 to 31 do
        dstr := (Chr(Ord('A') + i) + ':\Resource\CD1.gob');
    until FileExists(dstr);
    WizardForm.NextButton.Enabled := True; 
  end;
end;

但是使用此代码安装程序会在开始时冻结并且即使已插入 CD 也不会响应。

4

1 回答 1

3

像这样的东西应该做你需要的:

[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program

[Files]
Source: {code:GetFileSource}; DestDir: {app}; flags:external;

[Code]
#ifdef UNICODE
  #define AW "W"
#else
  #define AW "A"
#endif
type
  TDriveType = (
    dtUnknown,
    dtNoRootDir,
    dtRemovable,
    dtFixed,
    dtRemote,
    dtCDROM,
    dtRAMDisk
  );
  TDriveTypes = set of TDriveType;
function GetDriveType(lpRootPathName: string): UINT;
  external 'GetDriveType{#AW}@kernel32.dll stdcall';
function GetLogicalDriveStrings(nBufferLength: DWORD; lpBuffer: string): DWORD;
  external 'GetLogicalDriveStrings{#AW}@kernel32.dll stdcall';

var
  FileSource: string;

#ifndef UNICODE
function IntToDriveType(Value: UINT): TDriveType;
begin
  Result := dtUnknown;
  case Value of
    1: Result := dtNoRootDir;
    2: Result := dtRemovable;
    3: Result := dtFixed;
    4: Result := dtRemote;
    5: Result := dtCDROM;
    6: Result := dtRAMDisk;
  end;
end;
#endif

function GetLogicalDrives(var ADrives: array of string; 
  AFilter: TDriveTypes): Integer;
var
  S: string;
  I: Integer;
  DriveRoot: string;
begin
  Result := 0;
  SetArrayLength(ADrives, 0);

  I := GetLogicalDriveStrings(0, #0);
  if I > 0 then
  begin
    SetLength(S, I);
    if GetLogicalDriveStrings(Length(S), S) > 0 then
    begin
      S := TrimRight(S);
      I := Pos(#0, S);
      while I > 0 do
      begin
        DriveRoot := Copy(S, 1, I - 1);
        #ifdef UNICODE
        if (AFilter = []) or
          (TDriveType(GetDriveType(DriveRoot)) in AFilter) then
        #else
        if (AFilter = []) or
          (IntToDriveType(GetDriveType(DriveRoot)) in AFilter) then
        #endif
        begin
          SetArrayLength(ADrives, GetArrayLength(ADrives) + 1);
          #ifdef UNICODE
          ADrives[High(ADrives)] := DriveRoot;
          #else
          ADrives[GetArrayLength(ADrives) - 1] := DriveRoot;
          #endif
        end;
        Delete(S, 1, I);
        I := Pos(#0, S);
      end;
      Result := GetArrayLength(ADrives);
    end;
  end;
end;

function GetFileSource(Value: string): string;
begin
  // file source path passed to the [Files] section
  Result := FileSource;
end;

procedure InitializeWizard;
var
  I: Integer;
  DriveCount: Integer;
  DriveArray: array of string;
begin
  // the function will fill the DriveArray only with CDROM
  // drives and returns the count of found drives
  DriveCount := GetLogicalDrives(DriveArray, [dtCDROM]);
  // here you have an array of CD-ROM drives so iterate it
  // search for a file you need and when you find it, pass
  // the path to the FileSource variable, which will later
  // be queried to get the source to the file in [Files]
  for I := 0 to DriveCount - 1 do
  begin
    if FileExists(DriveArray[I] + 'Resource\CD1.GOB') then
    begin
      FileSource := DriveArray[I] + 'Resource\CD1.GOB';
      Break;
    end;
  end;
  MsgBox('File was found on path: ' + FileSource, mbInformation, MB_OK);
end;
于 2012-09-12T00:30:22.083 回答