在vc中调用黑屏下的cmd命令目前知道4种方法,分别是system、WinExec,ShellExecute和CreateProcess四个命令.下面简单介绍一下
1.在VC中调用DOS命令时,可以用函数system("DOS命令"),如我们经常见的命令system("pause"),当然也可以执行别的应用程序,比如 system("F://lame//lame.exe F://lame//a.wav F://lame//a.mp3")。只要在cmd下执行的在这里应该也没问题,只是注意打开文件或保存文件时若不指定绝对路径,将默认是当前程序的路径,也可以使用绝对路径。
2.WinExec多了个显示窗口方式,在MSDN中函数为:
UINT WinExec( LPCSTR lpCmdLine, UINT uCmdShow );
WinExec主要运行EXE文件。如:WinExec("notepad.exe", SW_SHOW); WinExec默认路径为程序所在的路径,所以一般使用绝对路径。
现在举例如下: WinExec("F://lame//lame.exe F://lame//a.wav F://lame//a.mp3", SW_HIDE); 对于多条命令用"&&"连接,第二个参数是隐藏CMD窗口的意思,还可以为其它的函数: nCmdShow Specifies how the CWnd is to be shown. It must be one of the following values: SW_HIDE Hides this window and passes activation to another window.
SW_MINIMIZE Minimizes the window and activates the top-level window in the system's list. SW_RESTORE Activates and displays the window. If the window is minimized or maximized, Windows restores it to its original size and position.
SW_SHOW Activates the window and displays it in its current size and position. SW_SHOWMAXIMIZED Activates the window and displays it as a maximized window. SW_SHOWMINIMIZED Activates the window and displays it as an icon. SW_SHOWMINNOACTIVE Displays the window as an icon. The window that is currently active remains active.
SW_SHOWNA Displays the window in its current state. The window that is currently active remains active.
SW_SHOWNOACTIVATE Displays the window in its most recent size and position. The window that is currently active remains active.
SW_SHOWNORMAL Activates and displays the window. If the window is minimized or maximized, Windows restores it to its original size and position.
3.ShellExecute ShellExecute不仅可以运行EXE文件,也可以运行已经关联的文件。 ShellExecute的一个参数为窗口句柄,第二个参数为处理方式如“open”“edit”“find”等,第三个参数为命令,第四个为命令参数,第五个为默认文件夹,第六个为控制台显示方式,一般命令参数只写命令,具体位置在默认文件夹处指定。对于一般的控制台程序,可以这样调用ShellExecute,
ShellExecute(NULL,"open","calc.exe",NULL,NULL, SW_SHOW ); 其中exe命令或者为系统的,或者为某个文件夹下的应用程序,这时需要在第5个参数指定应用程序位置文件夹。
下面引用关于ShellExecute的几个问答。
Q: 如何打开一个应用程序?
ShellExecute(this->m_hWnd,"open","calc.exe","","", SW_SHOW ); 或 ShellExecute(this->m_hWnd,"open","notepad.exe", "c://MyLog.log","",SW_SHOW ); 正如您所看到的,我并没有传递程序的完整路径。
Q: 如何打开一个同系统程序相关连的文档?
ShellExecute(this->m_hWnd,"open", "c://abc.txt","","",SW_SHOW ); Q: 如何打开一个网页? ShellExecute(this->m_hWnd,"open", "http://www.google.com","","", SW_SHOW );
Q: 如何激活相关程序,发送EMAIL?
ShellExecute(this->m_hWnd,"open", "mailto:[email protected]","","", SW_SHOW );
Q: 如何用系统打印机打印文档?
ShellExecute(this->m_hWnd,"print", "c://abc.txt","","", SW_HIDE);
Q: 如何用系统查找功能来查找指定文件?
ShellExecute(m_hWnd,"find","d://nish", NULL,NULL,SW_SHOW);
Q: 如何启动一个程序,直到它运行结束?
(打开一个程序,直到它关闭再回到主程序)
SHELLEXECUTEINFO ShExecInfo = {0};
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd = NULL; ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = "c://MyProgram.exe";
ShExecInfo.lpParameters = "";
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_SHOW;
ShExecInfo.hInstApp = NULL;
ShellExecuteEx(&ShExecInfo);
WaitForSingleObject(ShExecInfo.hProcess,INFINITE);
或: PROCESS_INFORMATION ProcessInfo;
STARTUPINFO StartupInfo; //This is an [in] parameter
ZeroMemory(&StartupInfo, sizeof(StartupInfo));
StartupInfo.cb = sizeof StartupInfo; //Only compulsory field if(CreateProcess("c://winnt//notepad.exe", NULL, NULL,NULL,FALSE,0,NULL, NULL,&StartupInfo,&ProcessInfo))
{
WaitForSingleObject(ProcessInfo.hProcess,INFINITE);
CloseHandle(ProcessInfo.hThread);
CloseHandle(ProcessInfo.hProcess);
}
else
{
MessageBox("The process could not be started...");
}
Q: 如何显示文件或文件夹的属性?
SHELLEXECUTEINFO ShExecInfo ={0};
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_INVOKEIDLIST ;
ShExecInfo.hwnd = NULL; ShExecInfo.lpVerb = "properties";
ShExecInfo.lpFile = "c://"; //can be a file as well ShExecInfo.lpParameters = ""; ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_SHOW;
ShExecInfo.hInstApp = NULL;
ShellExecuteEx(&ShExecInfo);
4.CreateProcess因为使用复杂,比较少用。
BOOL CMSChartuseDlg::RunCMD(LPCTSTR pszCommand)
{
// TCHAR szAppName[_MAX_PATH] = TEXT("fexplore.exe");//资源管理器程序 TCHAR szAppName[60] = TEXT("c://windows//system32//cmd.exe");//资源管理器程序 PROCESS_INFORMATION pi;
STARTUPINFO si;
ZeroMemory(&si,sizeof(si));
ZeroMemory(&pi,sizeof(pi));
si.cb=sizeof(si);
si.dwFlags=STARTF_USESHOWWINDOW;
si.wShowWindow=SW_HIDE; if(CreateProcess(szAppName,(char*)pszCommand,NULL,NULL,NULL, 0,NULL,NULL,&si,&pi)==0)
{
LPVOID lpMsgBuf; FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM, NULL,GetLastError(),MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), // Default language (LPTSTR)&lpMsgBuf,0,NULL);
CString strMsg;
strMsg.Format("执行软件时出错:%s",(char*)lpMsgBuf);
LocalFree(lpMsgBuf); MessageBox(strMsg);
return FALSE;
}
else{
// system(pszCommand);
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
// WaitForSingleObject(pi.hProcess,INFINITE);
return TRUE;
}
}