相关函数可以参考MFC帮助文档;
class CExeCommandLine
{public:
CExeCommandLine()
{
ZeroMemory( &m_si, sizeof(m_si) );
m_si.cb = sizeof(m_si);
//隐藏窗口cmd窗口;
m_si.dwFlags = STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
m_si.wShowWindow=SW_HIDE;
ZeroMemory( &m_pi, sizeof(m_pi) );
}
~CExeCommandLine()
{
TerminateExe();
}
private:
STARTUPINFO m_si;
PROCESS_INFORMATION m_pi;
public:
BOOL ExeCmd( CString strExecmd )
{
// Start the child process.
if( !CreateProcess( NULL, // No module name (use command line).
strExecmd.GetBuffer(0), // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
0, // No creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&m_si, // Pointer to STARTUPINFO structure.
&m_pi ) // Pointer to PROCESS_INFORMATION structure.
)
{
//ErrorExit( "CreateProcess failed." );
return 0;
}
strExecmd.ReleaseBuffer();
return TRUE;
BOOL TerminateExe()
{
if ( m_pi.hProcess && m_pi.hThread )
{
DWORD dwEC = 0;
BOOL b = GetExitCodeProcess(
m_pi.hProcess, // handle to the process
&dwEC // termination status
);
if (b)
{
TerminateProcess( m_pi.hProcess, dwEC );
}
// Close process and thread handles.
CloseHandle( m_pi.hProcess );
CloseHandle( m_pi.hThread );
}
ZeroMemory( &m_si, sizeof(m_si) );
m_si.cb = sizeof(m_si);
ZeroMemory( &m_pi, sizeof(m_pi) );
return TRUE;
}
};