CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jul 2003
    Posts
    4

    Accessing the windows command prompt

    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!

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Accessing the windows command prompt

    You don't need a command prompt for this.
    Use SetFileAttributes(...) to change attributes of files.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  3. #3
    Join Date
    May 2004
    Posts
    81

    Re: Accessing the windows command prompt

    Yes you can do it by using a function called

    system("COMMAND");

    ex : -

    system("dir");
    system("del <filename>");
    system("cls");

    etc...

  4. #4
    Join Date
    May 2000
    Location
    Washington DC, USA
    Posts
    715

    Re: Accessing the windows command prompt

    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.

  5. #5
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Accessing the windows command prompt

    Quote Originally Posted by JMS
    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:
    Code:
    ShellExecute(hWnd, "open", "blah.exe", NULL, NULL, SW_HIDE);
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  6. #6
    Join Date
    Aug 2000
    Location
    New Jersey
    Posts
    968

    Re: Accessing the windows command prompt

    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
    David Maisonave
    Author of Policy Based Synchronized Smart Pointer
    http://axter.com/smartptr


    Top ten member of C++ Expert Exchange.
    C++ Topic Area

  7. #7
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Accessing the windows command prompt

    Or use Boost filesystem for platform independent filesystem stuff.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  8. #8
    Join Date
    Nov 2004
    Posts
    19

    Re: Accessing the windows command prompt

    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

  9. #9
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Accessing the windows command prompt

    Quote Originally Posted by eswar_illuri
    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.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured