Click here to Skip to main content
15,920,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I'm looking for a way to call system commands with variable parameters
within a Window environment.
Sort of like system() but i need to be
able to pass variable parameters.
I tried this:
system("command -%s -%s", parameter1, parameter2);

and this

C#
char buffer[200];
    string p="192.168.1.100";
    sprintf(buffer,"ping %s",p);

    system(buffer);
    system("pause");


However, i still get error.
Can someone direct me, what i am doing wrong?

Best Regards,
Victor
Posted

The problem with your code is that you're passing a std::string to sprintf, which wouldn't know what a string was if one came up and twatted it with a cricket bat. I'd suggest using a stringstream to format the string rather than sprintf - there's no chance of a buffer overflow.

Once you've got the command to execute the next fun-part is getting the results back...
 
Share this answer
 
Comments
CPallini 28-Oct-13 13:50pm    
5.
Your example code is OK and will execute the ping command. But if your parameters contain spaces, quotes or specific reserved characters, you must enclose these parameters by quotes. To be on the save side, you should also enclose the complete command by quotes. Your ping example would then look like this:
sprintf(buffer,"\"ping \"%s\"\"",p);

To make it more complicated, there is another special case when a parameter itself is enclosed by double quote characters. Then it must be enclosed by another pair of quotes.

The system() functions runs the command as 'cmd /c command'. You can open a command shell and execute 'help cmd' to get some information on quoting.
 
Share this answer
 
Hello,
First of all, thank you for your help and quick response.
Secondary, Jochen Arndt I did as you told me but I am getting the error below:
Ping request could not find host d_↕. Please check the name and try again.
Press any key to continue . . .


Best Regards,
Victor
 
Share this answer
 
Comments
CPallini 29-Oct-13 2:53am    
That has nothing to do with your original problem. This time, the command your program correctly issued, failed.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900