此方法最實用的調用exe.
#include <ShellAPI.h>
string file_path = s_run_dir+"\\ConsoleApplication1.exe"; if (!myfile.IsFileExist(file_path)) { return 1; } LPCWSTR lp_file_path = mystring.StringToLPCWSTR(file_path); SHELLEXECUTEINFO ShExecInfo; ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO); ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS; ShExecInfo.hwnd = NULL; ShExecInfo.lpVerb = NULL; ShExecInfo.lpFile = lp_file_path; ShExecInfo.lpParameters = __T("D:\\a.txt D:\\b.txt D:\\c.txt");//傳出去的參數 ShExecInfo.lpDirectory = NULL; ShExecInfo.nShow = SW_SHOW; ShExecInfo.hInstApp = NULL; BOOL b_ret=ShellExecuteEx(&ShExecInfo); if (b_ret) { //等待調用啟動的exe關閉,此處要設置成ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS; WaitForSingleObject(ShExecInfo.hProcess, INFINITE); } else { return 2; }
傳給控制台程序參數
#include "stdafx.h" #include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { // 參數個數,第一個參數為可執行文件路徑 int iParamCount = argc; cout << "參數個數:" << iParamCount << endl; for (int i = 0; i < iParamCount; i++) { cout << endl << "第" << i + 1 << "個參數:"; wprintf(argv[i]); } getchar(); return 0; }
