Dos command from mfc app
-
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!
-
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!
-
One Way is This UINT WinExec( LPCSTR lpCmdLine, // address of command line UINT uCmdShow // window style for new application );
-
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.
Did you try ShellExecute or ShellExecuteEx?
-
Did you try ShellExecute or ShellExecuteEx?
-
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?
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.
-
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!
void CTestDirDlg::OnButton1()
{
// TODO: Add your control notification handler code hereSTARTUPINFO 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...
-
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!