Pass and receive command line parameters between C ++ and C # programs

Source: Internet
Author: User

Recently, a program needs to be developed from C ++ to start a C # program and pass parameters.

The specific method is: Call CreateProcess () on the C ++ side to start the C # side and pass relevant parameters. On the C # side, use main (string [] ARGs) to receive command line parameters.

C ++Sending code:

Int main (INT argc, char * argv [])

{

Char * filename = "C: \ csharp.exe ";

Char * Params = "parameter 1 parameter 2 parameter 3"; // pass three parameters

Process_information PII;

Startupinfo ⅱ;

Memset (& ⅱ, 0, sizeof (ⅱ ));

Ii. cb = sizeof (II );

Ii. wshowwindow = sw_show;

Ii. dwflags = startf_useshowwindow;

CreateProcess (filename, Params, null, false, null, & ⅱ, & PII );

Return 0;

}

C #Acceptor code:

Static void main (string [] ARGs)

{

String MSG = "";

If (ARGs. length> 0)

{

Console. writeline ("parameter quantity:" + args. Length );

For (INT I = 0; I <args. length; I ++)

{

MSG = ARGs [I];

Console. writeline ("parameter" + I + ":" + MSG );

}

}

Else

{

Console. writeline ("command line parameters not obtained ");

}

Console. readkey ();

}

 

The result is as follows:

If you change Params in C ++:

Char * Params = ""; // pass 0 Parameters

Or

Char * Params = "parameter 1"; // pass 1 parameter

If you change Params in C ++:

Char * Params = "parameter 1 parameter 2"; // pass two parameters

Analysis: The parameter received on the CSHARP side is one fewer than the parameter sent by the VC side, that is, the first parameter sent by the VC side is not received on the CSHARP side.

 

I think it is interesting to see this result. I think what will happen if I send the result from the VC client in CSHARP?

CSHARP Sender:

PROCESS p = new process ();

P. startinfo. filename = "C: \ vc.exe"; // program name

P. startinfo. Arguments = "parameter 1 parameter 2 parameter 3"; // program execution Parameter

P. Start (); // start

C ++ receiving end:

Int main (INT argc, char * argv [])

{

If (argc> 0)

{

Cout <"the number of parameters is" <argc <Endl;

For (INT I = 0; I <argc; I ++)

{

Char * MSG = argv [I];

Cout <"parameter" <I <":" <MSG <Endl;

}

}

Else

Cout <"command line parameters not obtained" <Endl;

Char II;

Cin> II;

Return 0;

}

Execution result:

Analysis: only three parameters "parameter 1 parameter 2 parameter 3" are clearly sent on the CSHARP side, but four parameters are received on the C ++ receiving end, the first parameter is the name of the VC program.

 

The reason is that the main (INT argc, char * argv []) entry function of C/C ++ differs from main (string [] argv) of C # entry function. C/C ++ receives the program name as the first parameter, but C # does not. As follows:

 

VC end:

Int main (INT argc, char * argv [])

{

If (argc> 0)

{

Cout <"the number of parameters is" <argc <Endl;

For (INT I = 0; I <argc; I ++)

{

Char * MSG = argv [I];

Cout <"parameter" <I <":" <MSG <Endl;

}

}

Else

Cout <"command line parameters not obtained" <Endl;

Return 0;

}

Run: C: \ vc.exe 1 2 3

Result:

Analysis: In the command line, there are four parameters: "C: \ vc.exe", "1", "2", and "3". The first "C: \ vc.exe" is the program name, the other three "1", "2", and "3" are the real parameters, but the four parameters are received and displayed, c ++ regards the program name as the first argument of the command line.

CSHARP end:

Static void main (string [] ARGs)

{

String MSG = "";

If (ARGs. length> 0)

{

Console. writeline ("parameter quantity:" + args. Length );

For (INT I = 0; I <args. length; I ++)

{

MSG = ARGs [I];

Console. writeline ("parameter" + I + ":" + MSG );

}

}

Else

{

Console. writeline ("command line parameters not obtained ");

}

Console. readkey ();

}

Run: C: \ csharp.exe 1 2 3

Result:

Analysis: There are four parameters in the command line: "C: \ csharp.exe" "1" "2" "3", and the first "C: \ csharp.exe" is the program name, the other three are the real parameters. However, the program name is not displayed in the running result, which is visible. Unlike C ++, the program name is not considered as the first real argument of the command line in C.

Therefore, when passing command line parameters from C/C ++ to C #, you must add a useless parameter before these parameters. For example, if the parameter to be accepted by C # is "1 2 3", the command line parameter in C/C ++ is "*** 1 2 3 ". On the contrary, when C # transmits command line parameters to C/C ++, the parameters received by C/C ++ should be counted from the second one, because the first parameter is the program name.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.