Call an Executable and Pass Parameters

How to pass parameters to an exe?

Hope the below code may help.

Code from first .exe:

Process p= new Process();
p.StartInfo.FileName = "demo.exe";
p.StartInfo.Arguments = "param1 param2";
p.Start();
p.WaitForExit();

or

Process.Start("demo.exe", "param1 param2");

Code in demo.exe:

static void Main (string [] args)
{
Console.WriteLine(args[0]);
Console.WriteLine(args[1]);
}

Call an executable and pass parameters

Pass your arguments in constructor itself.

Process process = new ProcessBuilder("C:\\PathToExe\\MyExe.exe","param1","param2").start();

Batch one line to call an executable using arguments from file

Thanks to the comments under my question, I was pushed in the right direction.

The problem was my understanding of <. It literally means "Read file to STDIN" (as mentionned here). Many other documentation sites give vague definitions like (as mentionned here)

command < filename : Type a text file and pass the text to command

I need to parse the input correctly, since stdin isn't available in argc or argv, but through std::cin.

My batch code and text file remain unchanged, and I want to maintain the same form of parsing to avoid rewriting multiple projects, so I split the input string using the Solution 1.3 from here (slightly modified) and created a new_argv.

std::vector<char*> split(const std::string& s, char delimiter)
{
std::vector<char*> tokens;
std::string token;
std::istringstream tokenStream(s);
while (std::getline(tokenStream, token, delimiter))
{
tokens.push_back(_strdup(token.c_str()));
}
return tokens;
}

int main(int argc, char* argv[])
{
std::string extra_input; // Variable to store the contents of test.txt
std::getline(std::cin, extra_input); // Recuperate the contents of test.txt
std::vector<char*> new_argv = split(extra_input, ' '); // Split the args
for(int i = argc - 1; i >= 0; i--)
new_argv.insert(new_argv.begin(), argv[i]); // Add the original args to the beginning
const size_t new_argc = new_argv.size(); // Create the new argc based on the final argument list (vector)

if(new_argc >= 2)
{
if(strcmp("-help", new_argv[1]) ==0)
{
show_help();
return 0;
}
for(int i=1; i < new_argc; i++)
{
if(strcmp("-framerate", new_argv[i]) ==0)
{
i++;
if(i < new_argc)
{
FrameRate = (float)atof(new_argv[i]);
}
else
{
std::cerr << "Parameters error" << std::endl;
return 0;
}
} else if ...
{
...
}
}
}
// Important, don't forget to free the memory used by the _strdup
for(int i=1; i < new_argc; i++)
{
if(i >= argc)
free(new_argv[i]);
}
}

test.bat

call test.exe < test.txt

test.txt

-arg1 param1 -arg2 param2 ...

Of course, I need to add some checks to make it properly handle whitespace, but that's the gist of it. Thank you for your help and external point of view.

Edit : Fixed a mistake in the code.

Run exe form another exe and pass parameters

How about: std::system( "dosbox -c myCommand" ); (assuming dosbox.exe and your custom myCommand.exe are in your path)?

To start two in the background, do:

std::system( "start dosbox -c myCommand1" );
std::system( "start dosbox -c myCommand2" );
// Program has launched these in the background
// and continues execution here.

Alternately, you could spin up a thread for each std::system() call:

auto cmd1 = std::async( [] { std::system( "dosbox -c myCommand1" ); } );
auto cmd2 = std::async( [] { std::system( "dosbox -c myCommand2" ); } );
// Program is launching these in the background
// and continues execution here.

You may also want to check the return value for each std::system() call to make sure it succeeded.


Update: You ask how to run two commands in the foreground in a single dosbox, which is located in another folder. You can embed the full path like this:

std::system( "c:\\MyDosBox\\dosbox.exe -c c:\\My\\Progams\\myCommand1.exe p1 p2 && c:\\Other\\myCommand2.exe p3 p4" );`

Running exe with parameters in bash and passing it's output to another program

This really isn't a bash question. This is a C++ question really. prog2 should be reading from stdin as jordanm suggested in the comments. Something like this will read from stdin line by line:

for (std::string line; std::getline(std::cin, line);) {
std::cout << line << std::endl;
}

Change the cout to do whatever you want with it on each line.

Running an .exe file through a batch file and passing parameters

try this, it works with AVI as the main extension, you may change this:


@echo off &setlocal enabledelayedexpansion
for %%i in (*.avi) do (
set "line="
for %%j in ("%%~ni.*") do set line=!line! -"%%~j"
start "" test_app.exe !line!
)

Create a batch file to run an .exe with an additional parameter

in batch file abc.bat

cd c:\user\ben_dchost\documents\
executible.exe -flag1 -flag2 -flag3

I am assuming that your executible.exe is present in c:\user\ben_dchost\documents\
I am also assuming that the parameters it takes are -flag1 -flag2 -flag3

Edited:

For the command you say you want to execute, do:

cd C:\Users\Ben\Desktop\BGInfo\
bginfo.exe dc_bginfo.bgi
pause

Hope this helps



Related Topics



Leave a reply



Submit