C++获取外部exe程序的返回值
#include "stdafx.h"
#include <Windows.h>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR *argv[])
{
STARTUPINFOA si;
PROCESS_INFORMATION pi;
ZeroMemory(&pi, sizeof(pi));
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(STARTUPINFOA);
TCHAR cmd[256] = _T("D:\\7za.exe a D:\\nv1.zip D:\\test\\*.*");
BOOL working = ::CreateProcess(NULL, cmd, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS , NULL, NULL, &si, &pi);
if (working == 0)
{
DWORD error = GetLastError();
cout << "CreateProcess Error : " << error << endl;
getchar();
return 0;
}
WaitForSingleObject(pi.hProcess, INFINITE);
unsigned long Result;
GetExitCodeProcess(pi.hProcess, &Result);
cout << "Exit Code : " << Result << endl;
getchar();
return 0;
}
调用命令行程序并获取返回信息
CString ExeCmd(CString pszCmd)
{
//创建匿名管道
SECURITY_ATTRIBUTES sa = {sizeof(SECURITY_ATTRIBUTES), NULL, TRUE};
HANDLE hRead, hWrite;
if (!CreatePipe(&hRead, &hWrite, &sa, 0))
{
return _T("");
}
//设置命令行进程启动信息(以隐藏方式启动命令并定位其输出到hWrite)
STARTUPINFO si = {sizeof(STARTUPINFO)};
GetStartupInfo(&si);
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
si.wShowWindow = SW_HIDE;
si.hStdError = hWrite;
si.hStdOutput = hWrite;
//启动命令行
PROCESS_INFORMATION pi;
if (!CreateProcess(NULL, (LPWSTR)(LPCWSTR)pszCmd, NULL, NULL, TRUE, NULL, NULL, NULL, &si, &pi))
{
return _T("");
}
//立即关闭hWrite
CloseHandle(hWrite);
//读取命令行返回值
char buff[1024] = {0};
DWORD dwRead = 0;
while (ReadFile(hRead, buff, 1024, &dwRead, NULL))
{
}
CString strRet(buff);
CloseHandle(hRead);
return strRet;
}
使用:
ExeCmd(_T("ping baidu.com"));
ExeCmd(_T("identify -format "%[colorspace]" E:\\P70306-163226.jpg"));
其他参考:
BOOL Start(LPCTSTR lpszTaskInfo)
{
if (!lpszTaskInfo)
{
return FALSE;
}
if (m_hRenderProcess)
{
if (Wait(0))
{
m_nProgress = 0;
SAFE_CLOSE_HANDLE(m_hRenderProcess);
m_dwRenderThreadId = 0;
}
else
{
return FALSE;
}
}
BOOL bRet = FALSE;
HANDLE hDebugInfo = NULL;
do
{
TCHAR szDebugLog[MAX_PATH];
_tcscpy(szDebugLog, m_szFastRenderExPath);
_tcscat(szDebugLog, _T(".debug"));
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(sa);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
hDebugInfo = CreateFile(szDebugLog, FILE_WRITE_DATA, FILE_SHARE_READ, &sa, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
si.hStdInput = NULL;
si.hStdOutput = hDebugInfo;
si.hStdError = hDebugInfo;
si.wShowWindow = SW_HIDE;
TCHAR szCmdline[MAX_PATH * 2];
_sntprintf(szCmdline, ARRAYSIZE(szCmdline), _T(" -i \"%s\""), lpszTaskInfo); // 第一个空格非常重要
// Start the child process.
if( !CreateProcess( m_szFastRenderExPath, // Module name
szCmdline, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
TRUE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi ) // Pointer to PROCESS_INFORMATION structure
)
{
break;
}
// Close process and thread handles.
m_hRenderProcess = pi.hProcess;
CloseHandle( pi.hThread );
m_dwRenderThreadId = pi.dwThreadId;
// Start the message receive thread
if (!m_dwReceiveThreadId)
{
HANDLE hThread = (HANDLE) _beginthreadex(NULL, 0, ThreadFn, this, 0, &m_dwReceiveThreadId);
if (hThread == NULL)
{
break;
}
CloseHandle(hThread);
}
if (!m_hMsgEvent)
{
m_hMsgEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
}
else
{
ResetEvent(m_hMsgEvent);
}
bRet = TRUE;
}
while (FALSE);
SAFE_CLOSE_HANDLE(hDebugInfo);
if (!bRet)
{
if (m_hRenderProcess)
{
TerminateProcess(m_hRenderProcess, -1);
SAFE_CLOSE_HANDLE(m_hRenderProcess);
}
m_dwRenderThreadId = 0;
SAFE_CLOSE_HANDLE(m_hMsgEvent);
}
return bRet;
}
C++调用外部程序
三个SDK函数: WinExec, ShellExecute,CreateProcess
ShellExecute
void mySystem(CString cmd, CString par, int nShow)
{
SHELLEXECUTEINFO ShExecInfo = {0};
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = cmd;//调用的程序名
ShExecInfo.lpParameters = par;//调用程序的命令行参数
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_HIDE;//窗口状态为隐藏
ShExecInfo.hInstApp = NULL;
ShellExecuteEx(&ShExecInfo); //启动新的程序
WaitForSingleObject(ShExecInfo.hProcess,INFINITE);////等到该进程结束
}
CreateProcess
PROCESS_INFORMATION pi;
DWORD dwExitCode;
//Spawn the child process.
BOOL fSuccess = CreateProcess(..., ...);
if(fSuccess)
{
//Close the thread handle as soon as
//it is no longer needed!
CloseHandle(pi.hThread);
//Suspend our execution until
//the child has terminated.
WaitForSingleObject(pi.hProcess, INFINITE);
//The child process terminated;
//get its exit code.
GetExitCodeProcess(pi.hProcess,
&dwExitCode);
//Close the process handle as soon as
//it is no longer needed.
CloseHandle(pi.hProcess);
}
TCHAR szCommandLine[] = TEXT(".\\ffplay.exe -window_title CDFacePlay -exitonkeydown -exitonmousedown -fs -autoexit .\\video.mp4");
STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;
si.wShowWindow = FALSE;
si.dwFlags = STARTF_USESHOWWINDOW;
BOOL ret = ::CreateProcess(NULL, szCommandLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
if (ret)
{
WaitForSingleObject(pi.hProcess, INFINITE);//等待进程完成
}
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);