我正在编写一个NSIS安装程序,它记录了一些外部命令的输出(参见How can I make this function actually work in NSIS?。)不幸的是,当nsis存储输出时,它会占用文字换行符,大概是因为它正在查找文字$ \ n(我实际上并不知道。)
有人可以告诉我如何在以下代码段中正确转换$ 0中的换行符吗?
nsExec::ExecToStack '...'
Pop $0
我将把存储的字符串写入文件和DetailPrint。
答案 0 :(得分:2)
我不相信ExecToStack会占用换行符,但仅限于$ {NSIS_MAX_STRLEN}的输出。
ExecDos plug-in有一个/ TOFUNC参数,可以为每一行调用NSIS函数。
或者你可以自己解析它:
Function nsExecLine
Pop $0
DetailPrint Line|$0|
; FileWrite ...
FunctionEnd
!include LogicLib.nsh
nsExec::ExecToStack 'ping -n 2 localhost'
Pop $0
${If} $0 != "error"
Pop $0
StrCpy $1 -1
StrCpy $3 "" ; \r \n merge
more:
IntOp $1 $1 + 1
StrCpy $2 $0 1 $1
StrCmp $2 "" done ; Can also be printlast if desired
StrCmp $2 "$\n" +2
StrCmp $2 "$\r" +1 more
StrCpy $2 $0 $1
StrCpy $4 $0 1 $1
StrCmp $3 "" +2
StrCmp $3 $4 0 more
StrCpy $3 $4
IntOp $1 $1 + 1
;printlast:
StrCpy $0 $0 "" $1
Push $0
Push $3
Push $2
Call nsExecLine
Pop $3
Pop $0
StrCpy $1 -1
StrCmp $0 "" +1 more
done:
${EndIf}
...或者作为一个黑客,使用ExecToLog:
!include LogicLib.nsh
!include WinMessages.nsh
!ifndef LVM_GETITEM
!ifndef LVM_FIRST
!define LVM_FIRST 0x00001000
!endif
!define /math LVM_GETITEMCOUNT ${LVM_FIRST} + 4
!ifndef NSIS_UNICODE
!define /math LVM_GETITEM ${LVM_FIRST} + 5
!else
!define /math LVM_GETITEM ${LVM_FIRST} + 75
!endif
!endif
FindWindow $9 "#32770" "" $HWNDPARENT
GetDlgItem $9 $9 0x3F8
SendMessage $9 ${LVM_GETITEMCOUNT} 0 0 $7
nsExec::ExecToLog 'ping -n 2 localhost'
Pop $0
${If} $0 != "error"
!define MAXEXECLINE 5000
System::Call '*(i,i,i,i,i,i,i,i,i,&t${MAXEXECLINE})i.r6'
IntOp $8 $6 + 36
System::Call '*$6(i,i,i,i,i,ir8,i${MAXEXECLINE},i,i)'
SendMessage $9 ${LVM_GETITEMCOUNT} 0 0 $8
${DoWhile} $7 < $8
Push $6
Push $7
Push $8
Push $9
System::Call '*$6(i1,i$7,i0)'
SendMessage $9 ${LVM_GETITEM} 0 $6
System::Call '*$6(i,i,i,i,i,t.r7)' ; (i,i,i,i,i,i,i,i,i,&t${MAXEXECLINE} .r7)'
Push $7
Call nsExecLine ; nsExecLine should not DetailPrint in this case...
Pop $9
Pop $8
Pop $7
Pop $6
IntOp $7 $7 + 1
${Loop}
System::Free $6
${EndIf}