gpt4 book ai didi

windows - Inno Setup 驱动安装 API

转载 作者:可可西里 更新时间:2023-11-01 10:48:08 30 4
gpt4 key购买 nike

我在调用几个 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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com