-2

How can I insert parameters into an EXE file after it is executed using the code and not manually?

I've tried to insert inside the arguments of Process.StartInfo.Arguments and not worked. Tried to look in the site and not found something usefull.

[P1.StartInfo.WorkingDirectory = @"D:\try";
            P1.StartInfo.FileName = "CMD.exe";
            P1.StartInfo.Arguments = "/k cmbs_tcx.exe -han";
            P1.StartInfo.UseShellExecute = true;
            P1.StartInfo.CreateNoWindow = true;
            P2.StartInfo.WorkingDirectory = P1.StartInfo.WorkingDirectory;
            P2.StartInfo.FileName = P1.StartInfo.FileName;
            P2.StartInfo.Arguments = "/k han_client.exe 127.0.0.1 & 1 6 0 2 1 3 0 0 0 1 1 32534 1 4 0 2 0 2";
            P2.StartInfo.UseShellExecute = true;
            P2.StartInfo.CreateNoWindow = true;
            P1.Start();
            P2.Start();][1]

I look forward to seeing the EXE running after getting the latest arguments. The link includes pictures which describes what you see after opening the file and I want to give parameters to activate the menu.

enter image description here

enter image description here

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
  • 1
    Sharing images via google drive is not an acceptable way to share images. Please edit to use the standard method – Joe Phillips May 19 '19 at 13:22
  • Also what data type is P1? It would be good to see how that gets instantiated – Joe Phillips May 19 '19 at 13:23
  • 2
    That code is not C#. – Thomas Weller May 19 '19 at 13:23
  • If you want to "see" the exe, maybe you should set `CreateNoWindow = false`. Why do you run those executables through `cmd`? If they are console applications, you can run them directly. Usually works much better. – Thomas Weller May 19 '19 at 13:25
  • These application are doing some actions that complicated for me to rewrite them on my code so i want to operate them but my only option to activate them is through the cmd because i need to give starting parameters as you can see. "cmbs_tcx.exe -han" and "han_client.exe 127.0.0.1" – Sasha777 May 19 '19 at 13:33
  • 1
    Use ProcessStartInfo.RedirectStandardInput. – Hans Passant May 19 '19 at 14:00
  • What Hans said. See [this sample code](https://stackoverflow.com/a/51682585/7444103) (+ small project). – Jimi May 19 '19 at 14:39

1 Answers1

0

In order to pass your parameters as command-line arguments, the target application needs to explicitly support that and parse its command line - my guess is that it's not the case.

If you're lucky, the application is using standard input to read values - if so, all you need to do is set P2.StartInfo.RedirectStandardInput = true before launching and then write your values to the program using P2.StandardInput.WriteLine.

If you're less than lucky and the application uses some other way to read keyboard input, you'll need to bring the window to the foreground using:

[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);

passing it the P2.Handle value after the application starts, and then use SendKeys.Send or SendKeys.SendWait to send keystrokes to it. I would not recommend using it in any sort of production code, however.

Maciej Stachowski
  • 1,708
  • 10
  • 19