windows - Inno Setup 驱动安装 API

标签 windows driver inno-setup

我在调用几个 Windows API 来安装驱动程序时遇到问题。具体来说:
SetupCopyOEMInfDriverPackageInstall

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

以下是我尝试过的原型(prototype):

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';

原型(prototype)可能是正确的,我遇到了不同的错误,我不知道。我知道出了点问题,因为调用失败(返回失败代码)并且从 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;

最佳答案

由于我花了一些时间来弄清楚为什么给定的和经过验证的答案不起作用,所以我在这里发布了我的案例的正确答案。我在 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

关于windows - Inno Setup 驱动安装 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15417503/

相关文章:

parameters - Inno Setup 中的/SL5、/SPAWNWND、/NOTIFYWND 和/DEBUGWND 参数是什么意思?

c++ - move 语义和窗口句柄。 DeleteObject 安全句柄?

c - 我在 DbgView 中看不到日志,但在 DeviceTree 中可以看到过滤器

PHP (WAMP) OCI8 驱动设置问题

linux - 环形振荡器设备驱动程序

inno-setup - 当 WizardForm 最小化并恢复时 Inno Setup 检测(收到通知)

zip - 从 Inno Setup 中创建 ZIP 文件

windows - 在批处理脚本中运行 rmdir 命令时如何解决 "The directory is not empty"错误?

windows - Windows 服务的 DirectX 访问

c++ - CMake 不会为简单的测试程序生成 vc pdb