I will use some CMD commands in my program and these commands might throw some exceptions. And as you know, when an exception accours, CMD writes its own error message the screen. But, I want to write my own error message.
My question is this: Is there a way to block CMD messages and write only my own error messages?
P.S. This is not a complex program. It executes CMD commands using System().
Example:
Let’s say, the user can rename and copy any files in the program. As you know, if the user does not enter file’s path properly, an error message is showed on the screen. And I want that this error message never appears on the screen. Only my own error message is showed.
Thank you!
It depends on your platform and the commands you are going to use. The usage of
system()for calling console commands is by the way strongly discouraged by most people (it’s way to heavy for most purposes).I would suggest to you using
CreateProcess()with the CREATE_NO_WINDOW flag and waiting for the process to exit with a call toWaitForSingleObject()andGetExitCodeProcess().This approach utilizes the fact, that most CMD command are executables, located somewhere in
C:/Windows/....If you want to retrieve the output of the command you could also provide
hStdOutput,hStdErrorin theSTARTUPINFOstructure and setSTARTF_USESTDHANDLESinSTARTUPINFO.dwFlags.You can even do other things in your own program while the command is executing (especially as you mentioned file copy). This one is done the C++ way: