How to run Windows commands in a C++ program? - c++

Is there a way to put windows commands in C++ program.
I am a newbie so your help will be very much appreciated.
Thank You

You can try system function from cstdlib.
http://www.kev.pulo.com.au/pp/RESOURCES/cplusplus/ref/cstdlib/system.html

You should be able to use the system() function from <cstdlib>7. Although be aware that it will probably create a cmd.exe window. If you don't want that then you can use the windows specific CreateProcess() function.

Related

How to Redirect to Another Application in C++?

I've researched it all over StackOverflow and Google but only get results about C# and irrelevant topics, so now I have to ask. I want after 20 seconds of the application being open to close itself and open another application that is in the same folder. After I have debugged, I copy the application to the folder on my Desktop that I will eventually ZIP up. But I want the app to open another application in the folder. Say for instance ApplicationA.exe Opens and after 20 seconds it closes and ApplicationB.exe opens.
You can use the CreateProcess() winapi function which acts like fork()+exec() and then exit() the ApplicationA. Also you can use Sleep(miliseconds) for the delay.
Here is the information about CreateProcess():
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx
You can open it with system("ApplicationB.exe"); and then exit. This will start ApplicationB.exe and exit ApplicationA.exe.
Check out: http://www.dreamincode.net/forums/topic/18057-run-external-executable/
I believe C++ is not really the best way to go for system calls like the ones you need to do here. Maybe shell scripting would be a better option.
Anyway, the easiest way is probably writing a third program that opens ApplicationA.exe, closes it and starts ApplicaionB.exe.
I believe in C++ you'd have to use system, which you find in <cstdlib>. Yes, it is a C feature, not a C++ one, and its use is not encouraged, to put it mildly.
I would recommend using the ShellExecute function. http://msdn.microsoft.com/en-us/library/windows/desktop/bb762153(v=vs.85).aspx

C++ Can't call tskill inside with system call

I can't call taskkill or tskill inside VC++ with system i don't know any alternatives, i have tried writing a .bat file and trying calling tskill, that dind't work either.
system("tskill example");
system("dins.bat");
they don't work.
thanks
Use TerminateProcess instead. I also recommend reading this.

Hooking output from the console

I was wondering if there is any way to read the output of a console command, from executing it in code. OK that's probably not the clearest way I could have put that, so let's have an example:
My project PingSweepr is, as the name implies, a simple network ping sweeper that uses the C++ system() command to automate ping sweeping with the Windows shell ping command.
The only problem is, there is no way to sort the results (btw, this would be used in more than just that program, in case you were wondering), which would involve parsing the command-line output of the ping program. So basically my question is: is there any way to read the output from the shell into the program? Maybe through a system message hook or something, or is it just not possible?
Thanks!
Have you tried looking at the popen function? This older question has some discussion:
Capturing stdout from a system() command optimally
Here is the answer: How to execute a command and get output of command within C++ using POSIX?

How to pause FFmpeg from C++ code?

I'm writing a VisualC++ program which have code invoke ffmpeg.exe to convert video file.
I wonder is it possible to pause/resume ffmpeg converting thread from C++ code?
LR.
All you need to do is suspend and resume the ffmpeg child process itself.
The main problem is: there is no SuspendProcess API function. And there is no documented or safe way of doing this.
The only simple way of doing this is via SuspendThread/ResumeThread.
See this article on codeproject about how to do it.
There are no ways to controls the conversion using ffmpeg.
However, if you switch you mencoder, you will be able to do it.
This is an equivalent for Unix based command:
mike#krusty:~$ pidof ffmpeg
22730
mike#krusty:~$ sudo kill -STOP 22730
[sudo] password for mike:
mike#krusty:~$ sudo kill -CONT 22730
No. This is a command-line tool, and once it is started, you cannot pause it.
Microsoft API ::SuspendThread function

c++ file handling problem

hi everybody out there i need help from u in file handling
I'm using Ubuntu as my operating system
now my c++ code has a line remove("/home/manish.yadav/Desktop/ram.txt");
last time i ran this command in C code it remove my operating system
now I'm not sure to use it or not ?
can anyone tell me proper syntax of this command so that i can use it in my code ?
is there any other way to remove files in c++ by using programs? how to do this?
how delete file using c++ program ?
try it !
unlink("/home/manish.yadav/Desktop/ram.txt");
Take a look into the reference page for std::remove on how to use it to remove files.
Concerning your OS, std::remove doesn't randomly kill OSs. Last time I tried it, the function worked as expected.
"last time i ran this command in C code it remove my operating system".
No, it didn't. Something else did, and we have no crystal ball to guess what that was.