Click to See Complete Forum and Search --> : Accessing the windows command prompt
cejerik
December 8th, 2004, 07:12 AM
Is it possible to send commands to the command prompt from a C or C++ program and if so how do you do it? For example, can I change the attributes of a file to read-only(attrib) or list all files in a directory (dir) from my C program? Thanks!
Marc G
December 8th, 2004, 10:35 AM
You don't need a command prompt for this.
Use SetFileAttributes(...) to change attributes of files.
sudhakarm
December 8th, 2004, 10:46 AM
Yes you can do it by using a function called
system("COMMAND");
ex : -
system("dir");
system("del <filename>");
system("cls");
etc...
JMS
December 8th, 2004, 12:45 PM
Is it possible to send commands to the command prompt from a C or C++ program and if so how do you do it? For example, can I change the attributes of a file to read-only(attrib) or list all files in a directory (dir) from my C program? Thanks!
There are standard/and platform specific library commands which allow your program to interact with the OS for the tasks you wish to perform...
Change the attributes of a file to read-only... --->>> c/c++ chmod() fuction
List all files in a directory ... getfirst() getnext() or getfilefirst() getfilenext()
That is how a c/c++ programmer would typically perform those tasks..
Now if you wish to execute another application like the command task allows you to do you can use
system()
but system typically sucks because it brings up the anoying command session which flashes up on the screen and then disapeers before you can read what was executed.. To do away with the flash you can use
winexec( "blah.exe", SW_HIDE );
That does away with the annoying flash.. Now I have to add here before one of the guru's jump down my throat.. winexec is a frowned upon and supposedly shortly disabled function due to the fact that it's so easy to understand and helpful Microsoft has decided to try to mandate everybody uses
CreateProcess() which takes like 60 paramerters and a phd to grasp and does the exact same thing as winexec wich if you check calls createprocess.
Lastly.. if you really really want use the command shell and somehow collect the answer returned on the command line within your C/C++ program you can use the function ppipe... which will execute the command and return the entire command session to you in the form of a character buffer which you can then parse for your information.. pain in the butt but it works and can be helpful to know in rare cases when what you want to accomplish isn't already implemented in a library command.
Marc G
December 8th, 2004, 01:01 PM
winexec( "blah.exe", SW_HIDE );
That does away with the annoying flash.. Now I have to add here before one of the guru's jump down my throat.. winexec is a frowned upon and supposedly shortly disabled function due to the fact that it's so easy to understand and helpful Microsoft has decided to try to mandate everybody uses
Ever thought of using ShellExecute? As in:
ShellExecute(hWnd, "open", "blah.exe", NULL, NULL, SW_HIDE); :wave:
Axter
December 8th, 2004, 02:41 PM
There are several methods for launching another program from one program.
You can use _spawn(), _exec(), system(), WinExec(), ShellExecute(), ShellExecuteEx(), and CreateProcess().
The system() function is a more portable function.
WinExec(), ShellExecute(), ShellExecuteEx(), and CreateProcess() are unique to Windows programming. All four windows functions allow for launching an application in the background (window-hidden).
The WinExec() is the easiest of the four to use. It only takes two arguments, however MS recommends using the CreateProcess() instead of WinExec on all 32-bit programs.
Although you can launch a dir command using system or WinExec, it usally will not do the application too much good, since the information is being displayed on the screen.
If you need directory file list information, you can use FindFirstFile windows API functions, or you can use _findfirst VC++ extended functions.
You can also use the code in the following link which is OS independent:
http://code.axter.com/findfilecontainer.h
Marc G
December 9th, 2004, 03:06 AM
Or use Boost filesystem (http://www.boost.org/libs/filesystem/doc/index.htm) for platform independent filesystem stuff.
eswar_illuri
December 16th, 2004, 05:39 AM
Yes, it's possible, To invoke the command prompt from C or C++ use the command system(), give the parameter as string like commands at command prompt, i.e
system("dir");
regards,
eswar
Marc G
December 16th, 2004, 08:35 AM
Yes, it's possible, To invoke the command prompt from C or C++ use the command system(), give the parameter as string like commands at command prompt, i.e
system("dir");
regards,
eswar
This won't work, because output will be to standard output. If you want to use system(...) you'll also need to redirect the standard output.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.