批处理
ShellExecute(null, "open ", "c:\\abc.bat ", " ", " ",SW_SHOW );
深入浅出ShellExecute
译者:徐景周(原作:Nishant S)
Q: 如何打开一个应用程序?
正如您所看到的,我并没有传递程序的完整路径。
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:nishinapp@yahoo.com ", " ", " ", 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);
//创建进程
PROCESS_INFORMATION ProcessInfo;
memset(&ProcessInfo ,0,sizeof( PROCESS_INFORMATION ));
STARTUPINFO StartInfo;
memset(&StartInfo , 0 ,sizeof( STARTUPINFO ));
StartInfo.cb = sizeof(STARTUPINFO);
StartInfo.dwFlags = STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW;
StartInfo.hStdInput = ::GetStdHandle(STD_INPUT_HANDLE);
StartInfo.hStdOutput = theApp.hConnectWrite;
StartInfo.hStdError = theApp.hConnectWrite;
StartInfo.wShowWindow = SW_HIDE;
StartInfo.lpReserved = NULL;
StartInfo.cbReserved2 = 0;
StartInfo.lpReserved2 = NULL;
//char szProcess[100]= "D:\\LAN\\bcc55\\Bin\\bcc32.exe ";
//char szName[100]= "D:\\LAN\\bcc55\\Bin\\bcc32.exe ";
char szProcess[100]= "tree.com d:\\peon\\VCP ";
BOOL suc= ::CreateProcess( NULL , szProcess , NULL, NULL,TRUE,CREATE_NEW_CONSOLE,
NULL, NULL,&StartInfo, &ProcessInfo);
if( !suc ) AfxMessageBox( "fail! ");
DWORD result = ::WaitForSingleObject( (HANDLE)ProcessInfo.hProcess ,INFINITE);
//if( WAIT_OBJECT_0 == result )AfxMessageBox( "OK ");
if( WAIT_TIMEOUT == result )AfxMessageBox( "TIME_OUT ");
if( WAIT_ABANDONED == result )AfxMessageBox( "WAIT_ABANDONED ");
CloseHandle( theApp.hConnectWrite );
UINT WinExec(
LPCSTR lpCmdLine,
UINT uCmdShow
);
Syntax
HINSTANCE ShellExecute( HWND hwnd,
LPCTSTR lpOperation,
LPCTSTR lpFile,
LPCTSTR lpParameters,
LPCTSTR lpDirectory,
INT nShowCmd
);
运行批处理
PROCESS_INFORMATION info;
CreateProcess( "c:\\aaa.bat ",NULL,NULL,NULL,FALSE,CREATE_DEFAULT_ERROR_MODE |NORMAL_PRIORITY_CLASS,NULL,NULL,NULL,&pinfo);
批处理执行期间,不断的FindWindow,就是这个aaa.bat的窗口,如果存在则跳过,500毫秒后再检查,直到没找到,说明已完成并关闭了,发现aaa.bat已运行完毕后,就可以使用以下代码获得exit code了:
int code;
GetExitCodeProcess(info.hProcess, &code);
==============================================
另有一个方法:
你的bat运行时,根据不同的运行的结果,在退出之前,建立一个同名文件,但内容不同,如运行的某处,要exit 1时,你这样:
@echo 1> ExitCode.txt
要exit 2时,
@echo 2> ExitCode.txt
程序运行之初要
del ExitCode.txt
这样,你只要不断的检测目录下是否有ExitCode.txt文件,如果有,说明已运行完毕,内容就是所要的ExitCode了。