You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决Inno Setup生成的EXE安装打印机时驱动未安装问题?

问题分析与解决方案

核心问题

Inno Setup可成功触发PowerShell脚本并创建打印机端口,但驱动始终安装失败,手动运行脚本则正常。问题根源在于脚本执行时的工作目录与相对路径不匹配,以及64位系统下的文件系统重定向问题。


原ISS脚本

[Setup]
AppName=Printer Installer
AppVersion=1.0
DefaultDirName={tmp}
DisableDirPage=yes
DisableProgramGroupPage=yes
Uninstallable=no
AlwaysRestart=no
ArchitecturesAllowed=x64compatible
ArchitecturesInstallIn64BitMode=x64compatible
PrivilegesRequired=admin
Compression=lzma2/ultra64
SolidCompression=yes
LZMADictionarySize=1048576
LZMANumBlockThreads=4
LZMAUseSeparateProcess=yes
WizardStyle=modern
OutputDir=output
OutputBaseFilename=Home_Canon_Color_imageCLASS_MF634Cdw

[Messages]
ReadyLabel1=Setup is now ready to begin installing the printer.
InstallingLabel=The print drivers are being unpacked and installed.  The printer will be configured shortly.  Grab yourself a Snickers; this could take a hot minute.

[Files]
Source: "Drivers\*"; \
DestDir: "{tmp}\Drivers"; \
Flags: recursesubdirs createallsubdirs ignoreversion deleteafterinstall

[Code]
procedure CurStepChanged(CurStep: TSetupStep);
var
  ResultCode: Integer;
begin
  if CurStep = ssPostInstall then
  begin
    Exec(ExpandConstant('{sys}\WindowsPowerShell\v1.0\powershell.exe'),
      '-NoProfile -ExecutionPolicy Bypass -File "' + ExpandConstant('{tmp}\Drivers\Printer_Install.ps1') + '"', 
      '', SW_HIDE, ewWaitUntilTerminated, ResultCode);
  end;
end;

原PowerShell脚本

$ErrorActionPreference = "SilentlyContinue"
$PrintDriverPath = "./Driver/CNLB0MA64.INF"
$PrintDriverName = "Canon Generic Plus UFR II"
$PrinterPort = "192.168.20.118"
$PrinterName = "Canon Color imageCLASS MF634Cdw"
$PrinterLocale = "Loft"

$StartProcessParams = @{
  FilePath = "C:\Windows\System32\pnputil.exe"
  ArgumentList = "/Add-Driver `"$PrintDriverPath`" /install"
  Wait = $True
  NoNewWindow = $True
}

# Install the driver into the Windows Driver Store.
Start-Process @StartProcessParams

# Add the TCP/IP printer.
Add-PrinterDriver "$PrintDriverName"
Add-PrinterPort -Name "$PrinterPort" -PrinterHostAddress "$PrinterPort"
Add-Printer -Name "$PrinterName" -DriverName "$PrintDriverName" -PortName "$PrinterPort" -Location "$PrinterLocale"

具体修复步骤

1. 修正PowerShell脚本中的相对路径问题

Inno Setup执行PowerShell时,默认工作目录是系统盘根目录(比如C:\),不是脚本所在的{tmp}\Drivers,所以相对路径./Driver/CNLB0MA64.INF会找不到文件。需改为绝对路径:

# 替换原路径定义
$PrintDriverPath = Join-Path -Path $PSScriptRoot -ChildPath "./Driver/CNLB0MA64.INF"

2. 确保64位pnputil的正确调用

即使Inno Setup设置了64位模式,32位进程调用System32下的文件会被重定向到SysWOW64,需强制调用64位版本:

# 确定正确的pnputil路径
if ([Environment]::Is64BitOperatingSystem -and !([Environment]::Is64BitProcess)) {
    $pnputilPath = "C:\Windows\SysNative\pnputil.exe"
} else {
    $pnputilPath = "C:\Windows\System32\pnputil.exe"
}

$StartProcessParams = @{
  FilePath = $pnputilPath
  ArgumentList = "/Add-Driver `"$PrintDriverPath`" /install"
  Wait = $True
  NoNewWindow = $True
  Verb = "runas" # 双重确保管理员权限
}

3. 添加错误日志排查

去掉$ErrorActionPreference = "SilentlyContinue",改为捕获错误并写入日志,方便定位问题:

$ErrorActionPreference = "Stop"
$logPath = Join-Path -Path $PSScriptRoot -ChildPath "PrinterInstall.log"

try {
    # 安装驱动
    Start-Process @StartProcessParams -RedirectStandardOutput $logPath -RedirectStandardError $logPath
    
    # 后续打印机配置步骤
    Add-PrinterDriver "$PrintDriverName"
    Add-PrinterPort -Name "$PrinterPort" -PrinterHostAddress "$PrinterPort"
    Add-Printer -Name "$PrinterName" -DriverName "$PrintDriverName" -PortName "$PrinterPort" -Location "$PrinterLocale"
} catch {
    $_ | Out-File -FilePath $logPath -Append
    throw $_
}

4. 调整Inno Setup的工作目录

在调用PowerShell时,指定工作目录为脚本所在的{tmp}\Drivers,从根源避免路径问题:

procedure CurStepChanged(CurStep: TSetupStep);
var
  ResultCode: Integer;
  ScriptPath: String;
  WorkingDir: String;
begin
  if CurStep = ssPostInstall then
  begin
    ScriptPath := ExpandConstant('{tmp}\Drivers\Printer_Install.ps1');
    WorkingDir := ExpandConstant('{tmp}\Drivers');
    Exec(ExpandConstant('{sys}\WindowsPowerShell\v1.0\powershell.exe'),
      '-NoProfile -ExecutionPolicy Bypass -File "' + ScriptPath + '"', 
      WorkingDir, SW_HIDE, ewWaitUntilTerminated, ResultCode);
  end;
end;

最终修复后的PowerShell脚本

$ErrorActionPreference = "Stop"
$logPath = Join-Path -Path $PSScriptRoot -ChildPath "PrinterInstall.log"

$PrintDriverPath = Join-Path -Path $PSScriptRoot -ChildPath "./Driver/CNLB0MA64.INF"
$PrintDriverName = "Canon Generic Plus UFR II"
$PrinterPort = "192.168.20.118"
$PrinterName = "Canon Color imageCLASS MF634Cdw"
$PrinterLocale = "Loft"

# 确定正确的pnputil路径
if ([Environment]::Is64BitOperatingSystem -and !([Environment]::Is64BitProcess)) {
    $pnputilPath = "C:\Windows\SysNative\pnputil.exe"
} else {
    $pnputilPath = "C:\Windows\System32\pnputil.exe"
}

$StartProcessParams = @{
  FilePath = $pnputilPath
  ArgumentList = "/Add-Driver `"$PrintDriverPath`" /install"
  Wait = $True
  NoNewWindow = $True
  Verb = "runas"
  RedirectStandardOutput = $logPath
  RedirectStandardError = $logPath
}

try {
    # 安装驱动到驱动商店
    Start-Process @StartProcessParams

    # 等待驱动注册完成(短暂延迟避免驱动未就绪)
    Start-Sleep -Seconds 5

    # 添加打印机驱动、端口和打印机
    Add-PrinterDriver "$PrintDriverName"
    Add-PrinterPort -Name "$PrinterPort" -PrinterHostAddress "$PrinterPort"
    Add-Printer -Name "$PrinterName" -DriverName "$PrintDriverName" -PortName "$PrinterPort" -Location "$PrinterLocale"

    Write-Host "打印机安装完成" | Out-File -FilePath $logPath -Append
} catch {
    $errorMsg = "安装失败: $_"
    Write-Host $errorMsg
    $errorMsg | Out-File -FilePath $logPath -Append
    throw $errorMsg
}

内容的提问来源于stack exchange,提问作者RKillcrazy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.02 06:37:28