码迷,mamicode.com
首页 > 其他好文 > 详细

命令窗口

时间:2020-06-03 13:56:15      阅读:58      评论:0      收藏:0      [点我收藏+]

标签:sleep   let   ons   tde   message   flags   pipe   out   des   

void CFileRenameDlg::_execute(const CString& cmd)
{
SECURITY_ATTRIBUTES sa;
HANDLE hRead,hWrite;

sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;

if(!CreatePipe(&hRead,&hWrite,&sa,0))
{
MessageBox(_T("启动失败"));
return;
}

STARTUPINFO si;
PROCESS_INFORMATION pi;

ZeroMemory(&si,sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
GetStartupInfo(&si);
si.hStdError = hWrite;
si.hStdOutput = hWrite;
si.wShowWindow = SW_HIDE;
si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;

CString sCmd = _T("cmd /c \"") + cmd + _T("\"");
TCHAR* szCmd = new TCHAR[sCmd.GetLength() + 1];
_tcscpy(szCmd, sCmd.GetString());

if(!CreateProcess(NULL, szCmd, NULL,NULL,TRUE,NULL,NULL,NULL,&si,&pi))
{
MessageBox(_T("启动失败"));
delete[] szCmd;
return;
}
delete[] szCmd;
CloseHandle(hWrite);

char buffer[4096] = {0};
CString strOutput;
DWORD bytesRead;

while(1)
{
if(NULL == ReadFile(hRead,buffer,4095,&bytesRead,NULL))
{
break;
}
strOutput += buffer;
Sleep(1000);
}
CloseHandle(hRead);
}

 

test:

_execute(_T("D:\\test.csv"));

命令窗口

标签:sleep   let   ons   tde   message   flags   pipe   out   des   

原文地址:https://www.cnblogs.com/Pond-ZZC/p/13036782.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!