【问题标题】:Automaticalli ignore error during NSIS installer and log the error messageNSIS 安装程序期间自动忽略错误并记录错误消息
【发布时间】:2018-01-05 10:44:15
【问题描述】:

我有一个用 NSIS 编写的安装程序,其中一部分尝试安装一些字体,但失败了:

代码部分如下所示:

SetOverwrite ifnewer
File ".\target\fonts\*.*"

WriteRegStr HKEY_LOCAL_MACHINE "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts" "Arial Narrow (TrueType)" "arialn.ttf"
WriteRegStr HKEY_LOCAL_MACHINE "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts" "Arial Narrow Bold (TrueType)" "arialnb.ttf"
WriteRegStr HKEY_LOCAL_MACHINE "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts" "Arial Narrow Bold Italic (TrueType)" "arialnbi.ttf"
WriteRegStr HKEY_LOCAL_MACHINE "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts" "Arial Narrow Italic (TrueType)" "arialni.ttf"

GetTempFileName $R0
File /oname=$R0 ".\target\additions\fonts.dll"

Push "arialn.ttf"
CallInstDLL $R0 registerFont
Push "arialnb.ttf"
CallInstDLL $R0 registerFont     
Push "arialnbi.ttf"
CallInstDLL $R0 registerFont     
Push "arialni.ttf"
CallInstDLL $R0 registerFont
SetOverwrite on

DetailPrint "Fonts Installed"

这个错误信息对我来说不是什么大问题,所以我总是可以忽略它。

我想知道,我怎么能自动忽略这些字体安装部分的错误消息弹出但记录(DetailPrint)错误消息?

【问题讨论】:

  • 我假设安装程序以提升的管理员身份运行?
  • 当然,我以管理员身份运行它

标签: dll fonts installation nsis


【解决方案1】:

该对话框由SetOverwrite 控制,但无法立即获得您想要的内容。

您需要放弃File * 命令,而是手动处理每个文件。

如果您不在乎新文件是否较旧,则可以使用SetOverwrite try

!include LogicLib.nsh
!macro TryExtractWithDetailPrint file
SetOverwrite try
ClearErrors
File "${file}"
${If} ${Errors}
    DetailPrint 'Could not extract "${file}", ignoring error...'
${EndIf}
SetOverwrite lastused
!macroend

Section
SetOutPath $Fonts
!insertmacro TryExtractWithDetailPrint "c:\myfonts\font1.ttf"
!insertmacro TryExtractWithDetailPrint "c:\myfonts\font2.ttf"
SectionEnd

如果您想考虑日期戳,则需要将新文件提取到临时位置,然后通过比较新旧文件来决定是否需要尝试覆盖:

!macro Make64 high low result
System::Int64Op "${high}" << 32
IntFmt ${result} "%#x" "${low}" ; Must reformat as hex because we don't want negative numbers to be sign extended
System::Int64Op ${result} |
Pop ${result}
!macroend

!macro CompareFilesLastWrite oldfile newfile result
Push "${newfile}"
Push "${oldfile}"
Call CompareFilesLastWrite
Pop ${result}
!macroend
Function CompareFilesLastWrite
System::Store S
Pop $1 ; oldpath
Pop $2 ; newpath
GetFileTime $2 $R2 $R1
IntOp $R1 $R1 & 0xfc000000 ; Chop off the bottom because of FAT
GetFileTime $1 $2 $1
IntOp $1 $1 & 0xfc000000 ; Chop off the bottom because of FAT
!insertmacro Make64 $2 $1 $1 ; old
!insertmacro Make64 $R2 $R1 $2 ; new
System::Call 'KERNEL32::CompareFileTime(*lr1,*lr2)i.s'
System::Store L
FunctionEnd

!macro TryExtractIfNewer src dst
InitPluginsDir
Push $0
SetDateSave on ; Must be on for this to work
File "/oname=$PluginsDir\temp.tmp" "${src}"
!insertmacro CompareFilesLastWrite "${dst}" "$PluginsDir\temp.tmp" $0
${If} $0 < 0
    SetOverwrite try
    ClearErrors
    File "/oname=${dst}" "${src}"
    ${If} ${Errors}
        DetailPrint 'Could not extract "${dst}", ignoring error...'
    ${EndIf}
    SetOverwrite lastused
${Else}
    DetailPrint "Existing file is newer, skipping"
${EndIf}
Pop $0
!macroend

Section
!insertmacro TryExtractIfNewer "myfiles\myfont.ttf" "$fonts\myfont.ttf"
SectionEnd

【讨论】:

  • 谢谢,我会试试的。但是,如果我想尝试覆盖但只有在更新的情况下怎么办?所以我会使用类似 SetOverwrite try ifnewer 但当然这是不可能的。那怎么办?
  • 您必须自己在宏中比较文件日期。
  • 啊,是的,你提到它很抱歉。您有比较日期的简单示例吗?
  • NSIS wiki 上有一些日期的东西,但我不知道它是否正是你所追求的。
  • NSIS 确实有 GetFileTime 指令,但您需要稍微调整一下数字。
猜你喜欢
  • 2012-04-13
  • 1970-01-01
  • 2016-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多