[Inno/Code] Run WPI or file from USB (removable device)...

Inno Setup Discussion
Post Reply
User avatar
-=[4lfre1re]=-
Posts: 183
Joined: Sun Jan 10, 2016 10:38 am

[Inno/Code] Run WPI or file from USB (removable device)...

Post by -=[4lfre1re]=- » Mon Feb 22, 2016 5:36 pm

[Inno/Code] Run WPI or file from USB (removable device)...

[Source
]
[Author]
To run a executable file from a removable device in Inno Setup use this code:
;Run WPI (or file) from USB (removable or optical device)
[Run]
Filename: "{code:GetFileSource}";

Code: Select all

[/b]
#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;

function GetLogicalDrives(var ADrives: array of string; 
  AFilter: TDriveTypes): Integer;
var
  S: string;
  I: Integer;
  DriveRoot: string;
begin
  Result := 0;
  SetArrayLength(ADrives, 0);
  SetLength(S, 65);
  I := GetLogicalDriveStrings(64, S);
  if I <> 0 then
  begin
    SetLength(S, I);  
    I := Pos(#0, S);
    while I > 0 do
    begin
      DriveRoot := Copy(S, 1, I - 1);
      if (AFilter = []) or 
        (TDriveType(GetDriveType(DriveRoot)) in AFilter) then
      begin
        SetArrayLength(ADrives, GetArrayLength(ADrives) + 1);
        ADrives[High(ADrives)] := DriveRoot;
      end;
      Delete(S, 1, I);
      I := Pos(#0, S);
    end;
    Result := GetArrayLength(ADrives);
  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 Removable
  // drives and returns the count of found drives
  DriveCount := GetLogicalDrives(DriveArray, [dtRemovable]);
  // 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] + '[color=#FF0000]WPI\WPI.exe[/color]') then
    begin
      FileSource := DriveArray[I] + '[color=#FF0000]WPI\WPI.exe[/color]';
      Break;
    end;
  end;
  //MsgBox('File was found on path: ' + FileSource, mbInformation, MB_OK);
end;[/quote]

[b]To copy file use:[/b]
[quote][b][Files][/b]
Source: {code:GetFileSource}; DestDir: {app}; flags:external;[/quote]

Folder in USB (removable device) = [color=#FF0000]WPI[/color]
Executable file inside WPI Folder = [color=#FF0000]WPI.exe[/color]

Cheers... ;-)
Image

User avatar
niTe_RiDeR_Pr0
Posts: 62
Joined: Wed Apr 13, 2016 9:15 am
Location: $omewhere in the Un!verse

Re: [Inno/Code] Run WPI or file from USB (removable device)...

Post by niTe_RiDeR_Pr0 » Mon Apr 18, 2016 6:29 am

Using inno setup everything is easy & possible... :)
Thanks for sharing that code, buddy... :)

Cheers ;)
Image Image

Post Reply