Inno Setup驱动程序安装API

时间:2013-03-14 18:37:07

标签: windows driver inno-setup

我在调用几个Windows API来安装驱动程序时遇到了麻烦。具体做法是:
SetupCopyOEMInfDriverPackageInstall

我使用的原型似乎没有用,可能是由于Unicode字符串或使用指针。注意:我正在使用Inno Setup的Unicode版本。某些参数可能为NULL,但我不知道如何在“代码”部分中指定NULL。

以下是我尝试的原型:

function SetupCopyOEMInf(SourceInfFileName: String;
  OEMSourceMediaLocation: String; OEMSourceMediaType: Longword;
  CopyStyle: Longword; DestinationInfFileName: String;
  DestinationInfFileNameSize: Longword; var RequiredSize: Longword;
  DestinationInfFileNameComponent: String): Longword;
external 'SetupCopyOEMInfW@setupapi.dll stdcall setuponly';

function GetLastError(): Longword;
external 'GetLastError@kernel32.dll stdcall setuponly';

type
InstallerInfo = record
  pApplicationId: String;
  pDisplayName: String;
  pProductName: String;
  pMfgName: String;
end;

function DriverPackageInstall(DriverPackageInfPath: String;
  Flags: Longword; pInstallerInfo: InstallerInfo;
  var pNeedReboot: Longword): Longword;
external 'DriverPackageInstall@files:difxapi.dll stdcall setuponly';

原型可能是正确的,我遇到了一个不同的错误,我不知道。我知道有些错误,因为调用失败(返回失败代码)并且从C程序调用相同的函数。

更新1:
DriverPackageInstall可能不会立即有用。 DLL必须在使用前注册。不是说这是不可能的,只需要更多的工作而不是适合这个问题。

更新2,3:
用法示例:

const
  MAX_PATH = 260;
  SPOST_PATH = 1;
  SP_COPY_DELETESOURCE = $0000001;

procedure InstallUsbDriver();
var
  RequiredSize: DWORD;
  DestinationInfFileName: String;
  DestinationInfFileNameComponent: String;
begin
  SetLength(DestinationInfFileName, MAX_PATH);
  SetLength(DestinationInfFileNameComponent, MAX_PATH);
  if not SetupCopyOEMInf(ExpandConstant('{app}\driver.inf'),
    ExpandConstant('{app}'), SPOST_PATH, SP_COPY_DELETESOURCE,
    DestinationInfFileName, MAX_PATH, RequiredSize,
    DestinationInfFileNameComponent) then begin
    MsgBox('Error installing USB driver: ' + SysErrorMessage(DLLGetLastError),
      mbError, MB_OK);
    CancelWithoutPrompt := True;
    WizardForm.Close;
  end;
end;

1 个答案:

答案 0 :(得分:0)

由于我花时间弄清楚为什么给定和验证的答案不起作用,我在这里发布我的案例的正确答案。我在ANSI模式下使用InnoSetup 5.5.3。当使用var修饰符为字符串声明外部方法时,我有一个访问冲突错误。你不应该添加这个修饰符。

轻松复制/粘贴答案:

#ifdef UNICODE
  #define AW "W"
#else
  #define AW "A"
#endif

function GetLastError: DWORD;
  external 'GetLastError@kernel32.dll stdcall setuponly';

function SetupCopyOEMInf(SourceInfFileName, OEMSourceMediaLocation: String;
  OEMSourceMediaType, CopyStyle: DWORD; DestinationInfFileName: String;
  DestinationInfFileNameSize: DWORD; var RequiredSize: DWORD;
  DestinationInfFileNameComponent: String): BOOL;
  external 'SetupCopyOEMInf{#AW}@setupapi.dll stdcall setuponly';

const
  MAX_PATH = 260;

  SPOST_NONE = 0;
  SPOST_PATH = 1;
  SPOST_URL = 2;

  SP_COPY_DELETESOURCE = $0000001;
  SP_COPY_REPLACEONLY = $0000002;
  SP_COPY_NOOVERWRITE = $0000008;
  SP_COPY_OEMINF_CATALOG_ONLY = $0040000;

function InstallDriver(PathLocation, FileName : String) : Boolean;
var
  RequiredSize: DWORD;
  DestinationInfFileName: String;
  DestinationInfFileNameComponent: String;
begin
  Result := False;
  if FileExists(PathLocation+'\'+FileName) then begin
    SetLength(DestinationInfFileName, MAX_PATH);
    SetLength(DestinationInfFileNameComponent, MAX_PATH);
    Result := SetupCopyOEMInf(PathLocation+'\'+FileName,
                              PathLocation, SPOST_PATH, SP_COPY_DELETESOURCE,
                              DestinationInfFileName, MAX_PATH, RequiredSize,
                              DestinationInfFileNameComponent);
  end;
end;

像这样使用:

 if not InstallDriver(ExpandConstant('{app}\drivers'), 'mydriver.inf') then
 begin
  MsgBox('Error: '+SysErrorMessage(GetLastError), mbError, MB_OK);
 end