Hi, I've been looking around for how to execute another application from a c++ app.

I remember in VB I could have my app launch the browser or other app and could even have it launch command line with the commands I wanted the system to run.

I have found several post at other places that say c++ can't do that without the use of some non c++ libs.

I find it hard to believe and think that would be a major disfunction in c++ if it can't execute or send commands to the system shell. BTW I was also reading that system() was something implemented in my c++; not actually my system.

Is all of this true? Is there any way to write a c++ program that can execute other apps or send commands directly to the shell? In my case this would be bash in linux but how to execute command.exe in windows would be helpful.

If it could be done with system() how can I put commands from user input into the system command? I tried it with a variable like system(commands); with no luck.

Any info you can provide on this topic is helpful for me to understand it.

Thanks

system() takes a char array (pointer) as an argument, if you want to run an application you could use this simple function.

void Run(char *app) {
   std::string command = "start ";
   command += app;
   system(command.c_str());
}

Now to call that function it should look something like this.

Run("notepad.exe");

and notepad should open.

This may not be the best and most portable method though.

Hope this helps.

Thank you williamhemswort. My internet has been down so I couldn't be online for a while.

I can definitely start there. I'll have to figure out how to translate that into running apps on linux which I think I can figure out, but with that I should be able to run exe's, msi's or anything that's executable on Windows I'm guessing.

It's been so long since I did it in VB that I can't remember the code. Basically I would call the exe and pass parameters to it like passing a url to explorer.exe to open a web page but it was done through visual studio so I didn't really learn what was going on behind the scenes.

If anyone knows any other methods I appreciate the help.

Or you could just say system("notepad file.ext"); instead of passing arguments to a function that does nothing more than that anyway. Or if you want the command in a string, just use:

string command = "notepad file.ext ";
   ...
   system(command.c_str());

if you wanna to execute a vbs file ,
just you can use execute the wscript.exe .

Well I'm trying to learn all methods available for executing directly from a program I write in c++. I was just mentioning the VB way because I'm looking for something similar in c++.

The VB equivalent would be to run the script and have the script actually execute the other app.

I want to write an app in c++ and use glade to make a gtk gui interface so when I click on the button labeled "mount drive" for example, it will execute the command on the system. (sudo mount /dev/sdh1). That's for linux. I also want to be able to open other programs like if I made my own gui that asks for a word and then takes that word, concantenates it with a url in the right format and then opens IE or firefox to the url for that word at dictionary.com.

Like I said, I did this in VB but don't remember how. I'll have to go back and figure that out again. Maybe it'll give me a clue about c++. Anyway I'll see what all I can do with the system(command.c_str()) method for now. I'm sure there's all kinds of ways I can use that.

Thanks guys.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.