获取CMD命令结果

UINT __cdecl ThreadProc(LPVOID lParam) { HWND hWnd = (HWND)lParam; ASSERT(hWnd); TCHAR szApp[MAX_PATH] = _T("netstat -s"); HANDLE hRead = NULL; HANDLE hWrite = NULL; SECURITY_ATTRIBUTES sa = {0}; sa.nLength = sizeof(sa); sa.lpSecurityDescriptor = NULL; sa.bInheritHandle = TRUE; if(!CreatePipe(&hRead, &hWrite, &sa, 0)) { AfxMessageBox(_T("CreatePipe failed.")); return -1; } STARTUPINFO si = {sizeof(si)}; PROCESS_INFORMATION pi = {0}; si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES; si.hStdOutput = hWrite; si.hStdError = hWrite; si.wShowWindow = SW_HIDE; if(!CreateProcess(NULL, szApp, NULL, NULL, TRUE, NULL, NULL, NULL, &si, &pi)) { CloseHandle(hWrite); CloseHandle(hRead); return -1; } CloseHandle(pi.hThread); CloseHandle(pi.hProcess); CloseHandle(hWrite); #define MAX_BUFFER 1024 TCHAR szBuf[MAX_BUFFER] = {0}; DWORD dwRead = 0; CString strText(_T("")); while(ReadFile(hRead, szBuf, MAX_BUFFER-1, &dwRead, NULL)) { strText += szBuf; memset(szBuf, 0, sizeof(szBuf)); } CloseHandle(hRead); AfxMessageBox(strText); return 0; }

你可能感兴趣的:(cmd,null,buffer,Path,attributes)