匿名管道,重新導向PING中的資料

來源:互聯網
上載者:User
#include <windows.h>#include <stdlib.h>void go(HWND hwnd){char * ping = "IPCONFIG /all"; // 命令char pbuf[1024]; // 緩衝DWORD len;STARTUPINFO si;PROCESS_INFORMATION pi;HANDLE hRead1, hWrite1;  // 管道讀寫控制代碼BOOL b;SECURITY_ATTRIBUTES saAttr;saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);saAttr.bInheritHandle = TRUE; // 管道控制代碼是可被繼承的saAttr.lpSecurityDescriptor = NULL;// 建立匿名管道,管道控制代碼是可被繼承的b = CreatePipe(&hRead1, &hWrite1, &saAttr, 1024);if (!b){MessageBox(hwnd, "管道建立失敗。","Information",0);return ;}memset(&si, 0, sizeof(si));si.cb = sizeof(si);si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;si.wShowWindow = SW_HIDE;si.hStdOutput = hWrite1; // 設定需要傳遞到子進程的管道寫控制代碼// 建立子進程,運行ping命令,子進程是可繼承的if (!CreateProcess(NULL, ping, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi)){itoa(GetLastError(), pbuf, 10);MessageBox(hwnd, pbuf,"Information",0);CloseHandle(hRead1);CloseHandle(hWrite1);return ;}// 寫端控制代碼已被繼承,本地則可關閉,不然讀管道時將被阻塞CloseHandle(hWrite1);// 讀管道內容,並用訊息框顯示len = 1000;DWORD l;while (ReadFile(hRead1, pbuf, len, &l, NULL)){if (l == 0) break;pbuf[l] = '\0';MessageBox(hwnd, pbuf, "Information",0);len = 1000;}MessageBox(hwnd, "ReadFile Exit","Information",0);CloseHandle(hRead1);return ;}int main(int argc, char* argv[]){go( NULL );return 0;}

 

文章轉載自:http://blog.csdn.net/lpc_china/article/details/5847260

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.