MFC的EXE调用VBS,并获取VBS的返回值状态码
VBS代码:
Dim age
age = 21
WScript.Quit age
MFC的EXE代码:
//获取EXE同目录下的VBS文件
TCHAR szExeSelfPath[_MAX_PATH] = {0};
::GetModuleFileName(NULL,szExeSelfPath,_MAX_PATH);
CString strExeSelfPath = szExeSelfPath;
strExeSelfPath = strExeSelfPath.Left(strExeSelfPath.ReverseFind('\\') + 1);
CString strVBSPath = strExeSelfPath + "test.vbs";
//执行VBS文件
DWORD dwExitCode;
SHELLEXECUTEINFO sei = {0};
sei.cbSize = sizeof(SHELLEXECUTEINFO);
sei.fMask = SEE_MASK_NOCLOSEPROCESS;
sei.hwnd = NULL;
sei.lpVerb = NULL;
sei.lpFile = strVBSPath;
sei.lpDirectory = NULL;
sei.nShow = SW_HIDE;
sei.hInstApp = NULL;
ShellExecuteEx(&sei);
//获取VBS返回的状态码
::GetExitCodeProcess(sei.hProcess,&dwExitCode);
CString strExitCode;
strExitCode.Format("%d",dwExitCode);
AfxMessageBox(strExitCode);