关闭

mfc 执行命令

87人阅读 评论(0) 收藏 举报
BOOL EI_bExcuteCMD(char *pcCmd)
{
CFile fp;
SECURITY_ATTRIBUTES sa;
HANDLE hRead,hWrite;
    CString strData;

// 建立匿名管道
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
if (!CreatePipe(&hRead, &hWrite, &sa, 1024))
    {
OutputDebugString("Error On CreatePipe()\n");
return FALSE;


// 建立进程,并继承管道输出
STARTUPINFO si;
PROCESS_INFORMATION pi; 
ZeroMemory( &si, sizeof(si) );
ZeroMemory( &pi, sizeof(pi) );
si.cb = sizeof(STARTUPINFO);
GetStartupInfo(&si); 
// si.hStdInput  = GetStdHandle( STD_INPUT_HANDLE );
//  si.hStdError  = GetStdHandle( STD_ERROR_HANDLE );
si.hStdError   = hWrite;
si.hStdOutput  = hWrite;
si.wShowWindow = SW_SHOW;
si.dwFlags     = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;


    if (!CreateProcess(NULL, pcCmd,NULL,NULL,TRUE, CREATE_NO_WINDOW,NULL,NULL,&si,&pi))//CREATE_NEW_CONSOLE | 
    {
OutputDebugString("Error on CreateProcess()\n");
return FALSE;
}

//超时时间5秒
CString sTemp;
DWORD lpExitCode;
sTemp.Format("%s", pcCmd);

if (WaitForSingleObject(pi.hProcess, 5000) == WAIT_TIMEOUT)
{
//进程还在的话强制退出
GetExitCodeProcess (pi.hProcess, &lpExitCode);
if (lpExitCode == STILL_ACTIVE)
{
lpExitCode= TerminateProcess(pi.hProcess, 0);
}
return FALSE;
}

//进程还在的话强制退出
GetExitCodeProcess (pi.hProcess, &lpExitCode);
if (lpExitCode == STILL_ACTIVE)
{
lpExitCode= TerminateProcess(pi.hProcess, 0);
}

return TRUE;
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:8626次
    • 积分:280
    • 等级:
    • 排名:千里之外
    • 原创:20篇
    • 转载:3篇
    • 译文:0篇
    • 评论:0条