2

我无法调用几个 Windows API 来安装驱动程序。具体来说:
SetupCopyOEMInfDriverPackageInstall

我使用的原型似乎不起作用,可能是由于 Unicode 字符串或使用了指针。注意:我使用的是 Inno Setup 的 Unicode 版本。有些参数可能是NULL,但我不知道如何在Code部分指定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;
4

1 回答 1

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
于 2014-04-17T14:39:05.627 回答