dev_O
dev_O

Reputation: 163

How to parse cmd.exe values ​in NSIS?

In cmd.exe, type Generator.exe param1 param2 param3 and tell me how to parse the received value.

Generator is an exe created in C #.

Result class and main part were edited slightly. I want to do NSIS string parsing on Result value.

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

As shown in the figure, in NSIS, I want to put a message box in the parsing syntax called False in the IF statement.

Upvotes: 1

Views: 414

Answers (2)

Anders
Anders

Reputation: 101736

Use nsExec (or one of the other exec plug-ins) if you need to capture the output written to stdout by a console program:

!include LogicLib.nsh

Section
nsExec::ExecToStack '"$InstDir\Generator.exe" param1 param2 param3'
Pop $0 ; Exit code
Pop $1 ; Output
${If} $1 == "string goes here"
  MessageBox mb_Ok "Message text!"
  Abort
${EndIf}
SectionEnd

If the output contains newlines you can include those in your string check:

Section
nsExec::ExecToStack '"cmd.exe" /c echo.Hello'
Pop $0 ; Exit code
Pop $1 ; Output

${If} $1 = "Hello$\r$\n"
    MessageBox mb_OK "Hi"
${EndIf}
SectionEnd

or you can remove the newlines:

!include StrFunc.nsh
${StrTrimNewLines}

Section
nsExec::ExecToStack '"cmd.exe" /c echo.Hello'
Pop $0 ; Exit code
Pop $1 ; Output

${StrTrimNewLines} $1 $1
${If} $1 = "Hello"
    MessageBox mb_OK "Hi"
${EndIf}
SectionEnd

Upvotes: 1

dev_O
dev_O

Reputation: 163

I tried to use the String value received from the console, but I could not use it. I checked for whitespace and newline characters. I do not know why. But It was used normally when I put in the portion of Trim processing in 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

Upvotes: 0

Related Questions