如何在 NSIS 中解析 cmd.exe 值?
How to parse cmd.exe values in NSIS?
在 cmd.exe 中输入 Generator.exe param1 param2 param3 并告诉我如何解析接收到的值。
Generator 是用 C# 创建的 exe。
结果 class 和主要部分稍作编辑。我想对结果值进行 NSIS 字符串解析。
static void Main(string[] param)
{
// ip, key, productName
if (param.Length != 3)
{
CResult result = new CResult();
result.Result = false;
result.Msg = "IP or Key NULL";
Console.WriteLine(result.Result.ToString() + ',' + result.Msg);
return;
}
}
public class CResult
{
private bool _result = false;
public bool Result
{
get { return _result; }
set { _result = value; }
}
private string _msg = "WORK_FAIL";
public string Msg
{
get { return _msg; }
set { _msg = value; }
}
public int nRet { get; set; }
}
enter image description here
如图所示,在NSIS中,我想在IF语句中的解析语法中放一个消息框叫False
如果需要捕获控制台程序写入标准输出的输出,请使用 nsExec(或其他 exec 插件之一):
!include LogicLib.nsh
Section
nsExec::ExecToStack '"$InstDir\Generator.exe" param1 param2 param3'
Pop [=10=] ; Exit code
Pop ; Output
${If} == "string goes here"
MessageBox mb_Ok "Message text!"
Abort
${EndIf}
SectionEnd
如果输出包含换行符,您可以将它们包含在字符串检查中:
Section
nsExec::ExecToStack '"cmd.exe" /c echo.Hello'
Pop [=11=] ; Exit code
Pop ; Output
${If} = "Hello$\r$\n"
MessageBox mb_OK "Hi"
${EndIf}
SectionEnd
或者您可以删除换行符:
!include StrFunc.nsh
${StrTrimNewLines}
Section
nsExec::ExecToStack '"cmd.exe" /c echo.Hello'
Pop [=12=] ; Exit code
Pop ; Output
${StrTrimNewLines}
${If} = "Hello"
MessageBox mb_OK "Hi"
${EndIf}
SectionEnd
我尝试使用从控制台接收到的字符串值,但无法使用。
我检查了空格和换行符。
我不知道为什么。但是我把Trim处理的部分放到NSIS里就可以正常使用了
Function Trim
Exch $R1 ; Original string
Push $R2
Loop:
StrCpy $R2 "$R1" 1
StrCmp "$R2" " " TrimLeft
StrCmp "$R2" "$\r" TrimLeft
StrCmp "$R2" "$\n" TrimLeft
StrCmp "$R2" "$\t" TrimLeft
GoTo Loop2
TrimLeft:
StrCpy $R1 "$R1" "" 1
Goto Loop
Loop2:
StrCpy $R2 "$R1" 1 -1
StrCmp "$R2" " " TrimRight
StrCmp "$R2" "$\r" TrimRight
StrCmp "$R2" "$\n" TrimRight
StrCmp "$R2" "$\t" TrimRight
GoTo Done
TrimRight:
StrCpy $R1 "$R1" -1
Goto Loop2
Done:
Pop $R2
Exch $R1
FunctionEnd
在 cmd.exe 中输入 Generator.exe param1 param2 param3 并告诉我如何解析接收到的值。
Generator 是用 C# 创建的 exe。
结果 class 和主要部分稍作编辑。我想对结果值进行 NSIS 字符串解析。
static void Main(string[] param)
{
// ip, key, productName
if (param.Length != 3)
{
CResult result = new CResult();
result.Result = false;
result.Msg = "IP or Key NULL";
Console.WriteLine(result.Result.ToString() + ',' + result.Msg);
return;
}
}
public class CResult
{
private bool _result = false;
public bool Result
{
get { return _result; }
set { _result = value; }
}
private string _msg = "WORK_FAIL";
public string Msg
{
get { return _msg; }
set { _msg = value; }
}
public int nRet { get; set; }
}
enter image description here
如图所示,在NSIS中,我想在IF语句中的解析语法中放一个消息框叫False
如果需要捕获控制台程序写入标准输出的输出,请使用 nsExec(或其他 exec 插件之一):
!include LogicLib.nsh
Section
nsExec::ExecToStack '"$InstDir\Generator.exe" param1 param2 param3'
Pop [=10=] ; Exit code
Pop ; Output
${If} == "string goes here"
MessageBox mb_Ok "Message text!"
Abort
${EndIf}
SectionEnd
如果输出包含换行符,您可以将它们包含在字符串检查中:
Section
nsExec::ExecToStack '"cmd.exe" /c echo.Hello'
Pop [=11=] ; Exit code
Pop ; Output
${If} = "Hello$\r$\n"
MessageBox mb_OK "Hi"
${EndIf}
SectionEnd
或者您可以删除换行符:
!include StrFunc.nsh
${StrTrimNewLines}
Section
nsExec::ExecToStack '"cmd.exe" /c echo.Hello'
Pop [=12=] ; Exit code
Pop ; Output
${StrTrimNewLines}
${If} = "Hello"
MessageBox mb_OK "Hi"
${EndIf}
SectionEnd
我尝试使用从控制台接收到的字符串值,但无法使用。 我检查了空格和换行符。 我不知道为什么。但是我把Trim处理的部分放到NSIS里就可以正常使用了
Function Trim
Exch $R1 ; Original string
Push $R2
Loop:
StrCpy $R2 "$R1" 1
StrCmp "$R2" " " TrimLeft
StrCmp "$R2" "$\r" TrimLeft
StrCmp "$R2" "$\n" TrimLeft
StrCmp "$R2" "$\t" TrimLeft
GoTo Loop2
TrimLeft:
StrCpy $R1 "$R1" "" 1
Goto Loop
Loop2:
StrCpy $R2 "$R1" 1 -1
StrCmp "$R2" " " TrimRight
StrCmp "$R2" "$\r" TrimRight
StrCmp "$R2" "$\n" TrimRight
StrCmp "$R2" "$\t" TrimRight
GoTo Done
TrimRight:
StrCpy $R1 "$R1" -1
Goto Loop2
Done:
Pop $R2
Exch $R1
FunctionEnd