网站建议:179001057@qq.com

获取CMD命令结果 收藏

技术2022-05-12  0

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;   }  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;}

本文来自博客,转载请标明出处:http://blog.csdn.net/VisualEleven/archive/2011/04/26/6365366.aspx


最新回复(0)