15,979
社区成员
发帖
与我相关
我的任务
分享
void CDosConsoleView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
if (nChar == VK_RETURN)
{
TCHAR szBuffer[_MAX_PATH] = {0};
GetEditCtrl().GetLine(0, szBuffer, _MAX_PATH);
CString szOutput;
My_System(szBuffer, szOutput);
GetEditCtrl().SetWindowText(szOutput);
}
CEditView::OnChar(nChar, nRepCnt, nFlags);
}
BOOL CDosConsoleView::My_System(TCHAR* cmd,CString& szOutPut)
{
SECURITY_ATTRIBUTES lsa;
STARTUPINFO si;
PROCESS_INFORMATION pi, *lppi;
HANDLE hReadPipe, hWritePipe;
lsa.nLength = sizeof(SECURITY_ATTRIBUTES);
lsa.lpSecurityDescriptor = NULL;
lsa.bInheritHandle = TRUE;
lppi = π
CWaitCursor waitCursor;
if(!CreatePipe(&hReadPipe, &hWritePipe, &lsa, 0)) {
AfxMessageBox(_T("Error on create pipe."));
return FALSE;
}
memset(&si, 0, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
si.hStdOutput = hWritePipe;
if(!CreateProcess(_T("C:\\Windows\\System32\\cmd.exe"), cmd, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi)) {
AfxMessageBox(_T("Error on create process"));
return FALSE;
}
DWORD cchReadBuffer;
CString strText;
TCHAR ph[5000];
szOutPut.Empty();
for(;;) {
cchReadBuffer = 0;
if(!PeekNamedPipe(hReadPipe, ph, 1, &cchReadBuffer, NULL, NULL)) break;
if(cchReadBuffer) {
if(!ReadFile(hReadPipe, ph, 4096, &cchReadBuffer, NULL)) break;
ph[cchReadBuffer] = 0;
szOutPut += ph;;
}
else
if(WaitForSingleObject(pi.hProcess, 0) == WAIT_OBJECT_0) break;
Sleep(500);
}
ph[cchReadBuffer] = 0;
szOutPut += ph;
CloseHandle(hReadPipe);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
CloseHandle(hWritePipe);
return TRUE;
}