管 道(pipe)實際是用於程序間通訊的一段共享記憶體,建立管道的程序稱為管道伺服器,連線到乙個管道的程序為管道客戶機。乙個程序在向管道寫入資料後,另 一程序就可以從管道的另一端將其讀取出來。匿名管道(anonymous pipes)是在父程序和子程序間單向傳輸資料的一種未命名的管道,只能在本地計算機中使用,而不可用於網路間的通訊。
bool winapi createpipe(
_out_ phandle hreadpipe,
_out_ phandle hwritepipe,
_in_opt_ lpsecurity_attributes lppipeattributes,
_in_ dword nsize
);
bool winapi createprocess(
_inout_opt_ lptstr lpcommandline,
_in_opt_ lpsecurity_attributes lpprocessattributes,
_in_opt_ lpsecurity_attributes lpthreadattributes,
_in_ bool binherithandles,
_in_ dword dwcreationflags,
_in_opt_ lpvoid lpenvironment,
_in_opt_ lpctstr lpcurrentdirectory,
_in_ lpstartupinfo lpstartupinfo,
_out_ lpprocess_information lpprocessinformation
);
方法一:建立乙個pipe然後是cmd執行的結果輸出到pipe裡,然後再讀取,這裡遇到乙個問題就是在hinput 這個witepipe寫完之後得關閉,使write 結束,這樣之後的readfile才可以執行而不被阻塞;
int readcmdcontextone()
st.cb = sizeof(startupinfo);
getstartupinfo(&st);
st.hstdoutput = hinput;
st.hstderror = hinput;
st.wshowwindow = sw_hide;
st.dwflags = startf_useshowwindow |startf_usestdhandles;
if(!createprocess(null,"c:\\windows\\system32\\cmd.exe /c ipconfig /all",null,
null,true,null,null,null,&st,&pi))
dword dwret = waitforsingleobject(pi.hprocess,infinite);
switch(dwret)
memset(buffer,0,sizeof(buffer));
dowhile(readbyte!=0 && hresult);
cout0;}
方法二:
就是執行命令的時候加乙個輸出重定向到檔案,然後再從檔案中讀取;檔案最好建乙個臨時檔案,不過windows還是不會幫我們自動刪除的,需要我們自己刪除;
void exec()
; process_information pi;
char sztemppath[max_path];
char sztempfile[max_path];
char buffer[4096];
char cmdline[100];
dword dwret ;
dword dwreadbyte;
dwret = gettemppath(max_path,sztemppath);
if(!succeeded(dwret))
dwret = gettempfilename(sztemppath,text("jjjjjay.txt"),0,sztempfile);
if(!succeeded(dwret))
memset(cmdline,0,sizeof(cmdline));
int strlen = sizeof("c:\\windows\\system32\\cmd.exe /c ipconfig /all >");
memcpy(cmdline,"c:\\windows\\system32\\cmd.exe /c ipconfig /all >",strlen);
memcpy(cmdline+strlen-1,sztempfile,sizeof(sztempfile));
cmdline[strlen+sizeof(sztempfile)] = '\0';
coutnull,true,null,null,null,&st,&pi))
dwret = waitforsingleobject(pi.hprocess,infinite);
switch(dwret)
hfile = createfile(sztempfile,generic_read,file_share_delete,null,open_always,file_attribute_normal,0);
if(hfile == invalid_handle_value)
memset(buffer,0,sizeof(buffer));
int len = 0 ;
dowhile(dwret && dwreadbyte!=0);
closehandle(hfile);
deletefile(sztempfile);
return
0;}
Powershell使用管道
管道並不是什麼新事物,以前的cmd控制台也有重定向的命令,例如dir more可以將結果分屏顯示。傳統的cmd管道是基於文字的,但是powershell是基於物件。ps ls sort object descending name select object name,length,lastwrit...
Powershell使用管道
管道並不是什麼新事物,以前的cmd控制台也有重定向的命令,例如dir more可以將結果分屏顯示。傳統的cmd管道是基於文字的,但是powershell是基於物件。ps ls sort object descending name select object name,length,lastwrit...
mysql使用命名管道 命名管道
管道是用於相關過程之間的通訊。我們是否可以使用管道進行不相關的程序通訊,比方說,我們要從乙個終端執行客戶端程式,從另乙個終端執行伺服器程式?答案是否定的。那麼怎樣才能實現不相關的程序通訊,簡單的答案就是使用 命名管道。即使這適用於相關的程序,但是使用命名管道進行相關的程序通訊沒有任何意義。我們使用乙...