Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Dos command from mfc app

Dos command from mfc app

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
8 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Hi, I want to execute dos commands from my app, such as "dir", or "copy". Sorry if this is a "stupid" question...and yes, I did RTFM. **FOLLOW UP AND ANSWER** Found the best way to do this from a Win32 app is: WinExec ( "cmd.exe /c doscommand /switch parameters", SW_HIDE ) By doing cmd.exe the next command is executed in a new process and the cmd.exe window will close after execution. Also, by using WinExec, you get the added benefit of hiding the MS-DOS window. Thanks for everyone's suggestions and help!

    I T M 3 Replies Last reply
    0
    • L Lost User

      Hi, I want to execute dos commands from my app, such as "dir", or "copy". Sorry if this is a "stupid" question...and yes, I did RTFM. **FOLLOW UP AND ANSWER** Found the best way to do this from a Win32 app is: WinExec ( "cmd.exe /c doscommand /switch parameters", SW_HIDE ) By doing cmd.exe the next command is executed in a new process and the cmd.exe window will close after execution. Also, by using WinExec, you get the added benefit of hiding the MS-DOS window. Thanks for everyone's suggestions and help!

      I Offline
      I Offline
      imsniper
      wrote on last edited by
      #2

      One Way is This UINT WinExec( LPCSTR lpCmdLine, // address of command line UINT uCmdShow // window style for new application );

      L 1 Reply Last reply
      0
      • I imsniper

        One Way is This UINT WinExec( LPCSTR lpCmdLine, // address of command line UINT uCmdShow // window style for new application );

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Unfortunately, I've tried this and it doesn't expose the dos commands available at the Ms-dos prompt, such as dir, copy, etc. Thanks for your reply.

        P 1 Reply Last reply
        0
        • L Lost User

          Unfortunately, I've tried this and it doesn't expose the dos commands available at the Ms-dos prompt, such as dir, copy, etc. Thanks for your reply.

          P Offline
          P Offline
          Peter Zajac
          wrote on last edited by
          #4

          Did you try ShellExecute or ShellExecuteEx?

          L 1 Reply Last reply
          0
          • P Peter Zajac

            Did you try ShellExecute or ShellExecuteEx?

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Tried that and it doesn't work. I've tried executing cmd.exe (nt) with dir as a parameter, and that doesn't work either. Any other ideas?

            K 1 Reply Last reply
            0
            • L Lost User

              Tried that and it doesn't work. I've tried executing cmd.exe (nt) with dir as a parameter, and that doesn't work either. Any other ideas?

              K Offline
              K Offline
              Konstantin Vasserman
              wrote on last edited by
              #6

              cmd.exe has number of switches you have to use in order to make it execute a DOS command. For example: cmd.exe /c dir This will execute dir and will close dos window. cmd.exe /k dir This will execute dir and will dos window will remain open. I hope this helps.

              1 Reply Last reply
              0
              • L Lost User

                Hi, I want to execute dos commands from my app, such as "dir", or "copy". Sorry if this is a "stupid" question...and yes, I did RTFM. **FOLLOW UP AND ANSWER** Found the best way to do this from a Win32 app is: WinExec ( "cmd.exe /c doscommand /switch parameters", SW_HIDE ) By doing cmd.exe the next command is executed in a new process and the cmd.exe window will close after execution. Also, by using WinExec, you get the added benefit of hiding the MS-DOS window. Thanks for everyone's suggestions and help!

                T Offline
                T Offline
                Tim Deveaux
                wrote on last edited by
                #7

                void CTestDirDlg::OnButton1()
                {
                // TODO: Add your control notification handler code here

                STARTUPINFO             startInfo;
                PROCESS\_INFORMATION     procInfo;
                
                startInfo.cb = sizeof(STARTUPINFO);
                startInfo.lpReserved = NULL;
                startInfo.lpTitle = NULL;
                startInfo.lpDesktop = NULL;
                startInfo.dwX = startInfo.dwY = startInfo.dwYSize = startInfo.dwXSize = 0;
                
                // the interesting stuff!
                startInfo.dwFlags = STARTF\_USESHOWWINDOW;
                startInfo.hStdInput   = NULL;
                startInfo.hStdOutput  = NULL;
                startInfo.hStdError   = NULL;
                
                startInfo.wShowWindow = SW\_SHOW;
                startInfo.lpReserved2 = NULL;
                startInfo.cbReserved2 = NULL;
                
                
                // Create the process...
                if (!CreateProcess (    NULL,   // app name
                                        "c:\\\\command.com /k dir",                                
                                        NULL,   // proc sec attr
                                        NULL,   // thread sec attr
                                        FALSE,   // inherit handles
                                        NORMAL\_PRIORITY\_CLASS,  // creation flags omitted
                                        NULL,   // p 2 new env block
                                        NULL,   // current dir name
                                        &startInfo,   // our startup info
                                        &procInfo     // info returned for proc
                                        ))
                {
                    int err = GetLastError();
                    // etc...
                }
                

                }

                You can play with this a bit - the next thing to add is the mechanism for passing the stdin, out, and error handles so that you can get the results...

                1 Reply Last reply
                0
                • L Lost User

                  Hi, I want to execute dos commands from my app, such as "dir", or "copy". Sorry if this is a "stupid" question...and yes, I did RTFM. **FOLLOW UP AND ANSWER** Found the best way to do this from a Win32 app is: WinExec ( "cmd.exe /c doscommand /switch parameters", SW_HIDE ) By doing cmd.exe the next command is executed in a new process and the cmd.exe window will close after execution. Also, by using WinExec, you get the added benefit of hiding the MS-DOS window. Thanks for everyone's suggestions and help!

                  M Offline
                  M Offline
                  Mike Dunn
                  wrote on last edited by
                  #8

                  #include <stdlib.h> system("dir C:\\windows"); Bam, there ya go.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups