Windows中的管道技術

來源:互聯網
上載者:User
   在以前學Linux的時候,碰到過管道的問題,認為管道不過是一個程式的輸出作為一個程式的輸入。就像這樣:
   #cat file | grep "abc"
   這裡,cat file的output,直接當作grep "abc"這個命令的input,利用管道,可以改變程式預設的input與output.

   
   今天無意中翻MSDN,看到windows當中也有Pipe的概念.以前都一直不知道,汗~~
   
   Pipe是怎麼定義的:
         A pipe is a section of shared memory that processes use for communication. The process that creates a pipe is the pipe server. A process that connects to a pipe is a pipe client. One process writes information to the pipe, then the other process reads the information from the pipe. 

         也就是說,管道就是一部份共用記憶體以便進程可以用來相互連信,建立了Pipe核心對象的進程就是一個Pipe Server, 當另一個進程與這個進程建立的Pipe Server串連時,就稱為Pipe Client.當一個進程往Piple當中寫入資訊時,另一個進程便可以從這個Pipe讀出這個資訊。

There are two types of pipes: anonymous pipes and named pipes. Anonymous pipes require less overhead than named pipes, but offer limited services.

The term pipe, as used here, implies that a pipe is used as an information conduit. Conceptually, a pipe has two ends. A one-way pipe allows the process at one end to write to the pipe, and allows the process at the other end to read from the pipe. A two-way (or duplex) pipe allows a process to read and write from its end of the pipe:

   Pipe分為兩種:一個是anonymous pipes(末名命Pipe),另一個是named pipes(命名Pipe), anonymous pipes所需要的開銷要比named pipes要來得少,但是缺點是提供的功能也少。
   pipe這個術語在這裡的意思是指:作為一個提供資訊的管道,從概念上來理解,Pipe包含了兩個端,一端可以允許進程寫入,另一端允許進程讀出。兩個端都可以讓進程讀或者寫。

1.Anonymous Pipes

An anonymous pipe is an unnamed, one-way pipe that typically transfers data between a parent process and a child process. Anonymous pipes are always local; they cannot be used for communication over a network.
      可以允許父進程與子進程之間傳輸資料。它常常用於本地機器,對於網路內的不同機器之間的通訊它並不適合。

2.Named Pipes

A named pipe is a named, one-way or duplex pipe for communication between the pipe server and one or more pipe clients. All instances of a named pipe share the same pipe name, but each instance has its own buffers and handles, and provides a separate conduit for client-server communication. The use of instances enables multiple pipe clients to use the same named pipe simultaneously.
   命名Pipe中的兩個端可以作為一台pipe伺服器與另一台(或多台)pipe用戶端通訊的工具。所有的pipe執行個體對象共用Pipe Name,但是,每個Pipe執行個體卻有自己的buffer和handle.另外,Pipe Server與各個Pipe client傳輸資料所用的管道也是不同的,並不共用。它最大的作用在於可以讓多個Pipe Client用這個Pipe Name來同時傳送資料。

Any process can access named pipes, subject to security checks, making named pipes an easy form of communication between related or unrelated processes. Named pipes can be used to provide communication between processes on the same computer or between processes on different computers across a network.
   任何進程都可以控制named pipes。並遵照其安全性檢查,任何有關聯的和不關聯的進程都可以通過Named Pipes進行通訊,可以在一個網路內或不同網路之間的進程進行通訊。

Any process can act as both a server and a client, making peer-to-peer communication possible. As used here, the term pipe server refers to a process that creates a named pipe, and the term pipe client refers to a process that connects to an instance of a named pipe.
   進程既可以作為Server端又可以作為Client端,在端對端的通訊環境下,術語Pipe Server是指建立Pipe的那個進程,而Pipe Client是指串連Pipe執行個體的進程。

Example:

   第一個,先看一個unonymous pipe的例子。這個例子是講把一個DOS進程上的內容通過管道技術輸出到一個MFC應用程式的CEdit控制項中。

   例子來源:http://dev.csdn.net/develop/article/18/18338.shtm

   首先,建立一個Pipe,設定好其兩個端,用CreatePipe函數。在CreatePipe前,設定其安全選項,因為要與子進程通訊,子進程也需要用到這個pipe核心對象. 所以核心對象的控制代碼還是要被繼承的。(核心對象詳見核心編程第三章)
  

HANDLE hReadPipe, hWritePipe;
SECURITY_ATTRIBUTES sa;
          
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL; //使用系統預設的安全性描述元
sa.bInheritHandle = TRUE; //一定要為TRUE,不然控制代碼不能被繼承。

CreeatePipe(&hReadPipe,&hWritePipe,&sa,0); //建立pipe核心對象,設定好hReadPipe,hWritePipe.

   下面,建立一個DOS進程與其進行通訊.建立進程的函數是CreateProcess.這個函數中有個LPSTARTUPINFO lpStartupInfo參數要注意一下.它設定的是建立進程視窗的風格,獲得更多的建立進程的方法,見:核心編程第四章

   

  STARTUPINFO si;
  PROCESS_INFORMATION pi; 
  si.cb = sizeof(STARTUPINFO);
  GetStartupInfo(&si); 
  si.hStdError = hWritePipe; //設定其標準錯誤輸出為hWritePipe
  si.hStdOutput = hWritePipe; //設定其標準輸出為hWritePipe
  si.wShowWindow = SW_HIDE;
  si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
  if (!CreateProcess(NULL,"c:\\windows\\system32\\cmd.exe/c dir /?"
        ,NULL,NULL,TRUE,NULL,NULL,NULL,&si,&pi)) {
        MessageBox("Error on CreateProcess()");
        return;
  }
  CloseHandle(hWritePipe);

   上面設定的是Dos視窗的內容輸出到hWritePipe,下面就是要把hReadPipe設定好,兩個Process之間就可以通訊了.
   

char buffer[4096] = {0};
  DWORD bytesRead; 
  while (true) {
      if (ReadFile(hReadPipe,buffer,4095,&bytesRead,NULL) == NULL)//從hReadPipe中讀出資料.
          break;
      m_Edit1 += buffer;
      UpdateData(false);
      Sleep(200); 
  } 

還有Named Pipes的內容,下回分解...........

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.