0

I am launching a C# windows application “Test.exe” from a button click event of C++ as below.

CreateProcess("D:\\Test\\Test.exe",
        “Passing Data to C#”,
        NULL,
        NULL,
        FALSE,
       CREATE_DEFAULT_ERROR_MODE,
       NULL,
       NULL,
       &siStartupInfo,
       &piProcessInfo);

Please let me know how to receive the parameter “Passing Data to C#” from C# windows application i.e. Test.exe

Amitav
  • 181
  • 1
  • 1
  • 7

2 Answers2

1

Your main method contains an args string array. You can access these arguments as you would in any other array. For example if you run Test.exe from the command prompt as "text.exe a b c d e" you could run this code in the main method to access each element.

for (int i = 0; i < args.Length; i++)
    {
        System.Console.WriteLine(args[i]);
    }

String args[] is common but depending on what you're using the actual passed arguments may start at args[1].

PeskyToaster
  • 283
  • 6
  • 16
0

You can read args of C# app's entry method which is usually Main(string[] args), or use System.Environment.GetCommandLineArgs().

If your Main method do not have string[] args, add it manually.

While Main(string[] args) can be used only in entrance method, the System.Environment.GetCommandLineArgs() can be used everywhere in your code to get parameters passed to your C# app. This is useful if you are writing a C# wpf app, the wpf app's Main method is compiler generated by default.

qaqz111
  • 181
  • 2
  • 7