Four Methods of inter-process communication are described in detail.

Source: Internet
Author: User

Four Methods of inter-process communication: clipboard, anonymous, named, and mail

First: clipboard
(1) create a dialog box-based application and set the following interface:



(2) edit the code for the send and receive buttons respectively:
[Cpp]? Void CClipboardDlg: OnBtnSend ()
{
// TODO: Add your control notification handler code here
If (OpenClipboard ())
{
CString str;
HANDLE hClip;
Char * pBuf;
EmptyClipboard (); // set the clipboard ownership to the current window
GetDlgItemText (IDC_EDIT_SEND, str );
HClip = GlobalAlloc (GMEM_MOVEABLE, str. GetLength () + 1); // allocate one byte for carriage return.
PBuf = (char *) GlobalLock (hClip); // lock a memory object and return the memory object handle
Strcpy (pBuf, str );
GlobalUnlock (hClip); // unlock
SetClipboardData (CF_TEXT, hClip); // place data
CloseClipboard (); // close the clipboard
}
}
 
Void CClipboardDlg: OnBtnRecv ()
{
// TODO: Add your control notification handler code here
If (OpenClipboard ())
{
// The IsClipboardFormatAvailable function determines whether the clipboard contains data in the specified format
If (IsClipboardFormatAvailable (CF_TEXT ))
{
HANDLE hClip;
Char * pBuf;
HClip = GetClipboardData (CF_TEXT );
PBuf = (char *) GlobalLock (hClip); // The GlobalLock function locks a global memory object and returns a pointer to the first byte of the object's memory block
GlobalUnlock (hClip );
SetDlgItemText (IDC_EDIT_RECV, pBuf );
CloseClipboard ();
}
}
}

Void CClipboardDlg: OnBtnSend ()
{
// TODO: Add your control notification handler code here
If (OpenClipboard ())
{
CString str;
HANDLE hClip;
Char * pBuf;
EmptyClipboard (); // set the clipboard ownership to the current window
GetDlgItemText (IDC_EDIT_SEND, str );
HClip = GlobalAlloc (GMEM_MOVEABLE, str. GetLength () + 1); // allocate one byte for carriage return.
PBuf = (char *) GlobalLock (hClip); // lock a memory object and return the memory object handle
Strcpy (pBuf, str );
GlobalUnlock (hClip); // unlock
SetClipboardData (CF_TEXT, hClip); // place data
CloseClipboard (); // close the clipboard
}
}

Void CClipboardDlg: OnBtnRecv ()
{
// TODO: Add your control notification handler code here
If (OpenClipboard ())
{
// The IsClipboardFormatAvailable function determines whether the clipboard contains data in the specified format
If (IsClipboardFormatAvailable (CF_TEXT ))
{
HANDLE hClip;
Char * pBuf;
HClip = GetClipboardData (CF_TEXT );
PBuf = (char *) GlobalLock (hClip); // The GlobalLock function locks a global memory object and returns a pointer to the first byte of the object's memory block
GlobalUnlock (hClip );
SetDlgItemText (IDC_EDIT_RECV, pBuf );
CloseClipboard ();
}
}
}


Second: anonymous Pipeline
<1> create a single-document-based project named "Parent"
(1) Add the following menu items and add the command response function CChildView: OnPipeRead (), CChildView: OnPipeWrite ();



(2) Add two handles HANDLE hRead and HANDLE hWrite to the CParentView class, set the attributes to private, initialize them in the constructor, and release the HANDLE in the destructor.
[Cpp]
CParentView: CParentView ()
{
// TODO: add construction code here
HRead = NULL;
HWrite = NULL;
}
 
CParentView ::~ CChildView ()
{
If (hRead)
CloseHandle (hRead );
If (hWrite)
CloseHandle (hWrite );
}

CParentView: CParentView ()
{
// TODO: add construction code here
HRead = NULL;
HWrite = NULL;
}

CParentView ::~ CChildView ()
{
If (hRead)
CloseHandle (hRead );
If (hWrite)
CloseHandle (hWrite );
}

(3) write the CParentView: OnPipeCreate () function. Note that two functions CreatePipe (...) and CreateProcess (...) are used to create the pipeline and promoter processes respectively. The Code is as follows:
[Cpp]
Void CParentView: OnPipeCreate ()
{
// TODO: Add your command handler code here
SECURITY_ATTRIBUTES sa; // defines a Security Attribute structure.
Sa. bInheritHandle = TRUE; // TRUE indicates that the quilt process can be inherited.
Sa. lpSecurityDescriptor = NULL;
Sa. nLength = sizeof (SECURITY_ATTRIBUTES );
If (CreatePipe (& hRead, & hWrite, & sa, 0) // create an anonymous Pipeline
{
MessageBox ("An error occurred while creating the anonymous MPs queue! ");
Return;
}
// If the sub-process is successfully created, the sub-process transmits the read/write handle of the anonymous pipeline to the sub-process.
STARTUPINFO sui;
PROCESS_INFORMATION pi;
ZeroMemory (& sui, sizeof (STARTUPINFO); // set all the Members in this structure to 0.
Sui. cb = sizeof (STARTUPINFO );
Sui. dwFlags = STARTF_USESTDHANDLES;
Sui. hStdInput = hRead;
Sui. hStdOutput = hWrite;
Sui. hStdError = GetStdHandle (STD_ERROR_HANDLE );
If (! CreateProcess (".. \ Child \ Debug \ Child.exe", NULL,
TRUE, 0, NULL, NULL, & sui, & pi ))
{
CloseHandle (hRead );
CloseHandle (hWrite );
HRead = NULL;
HWrite = NULL;
MessageBox ("failed to create sub-process! ");
Return;
 
}
Else
{
CloseHandle (pi. hProcess); // closes the master Process Handle
CloseHandle (pi. hThread); // closes the main process thread handle
}

}

Void CParentView: OnPipeCreate ()
{
// TODO: Add your command handler code here
SECURITY_ATTRIBUTES sa; // defines a Security Attribute structure.
Sa. bInheritHandle = TRUE; // TRUE indicates that the quilt process can be inherited.
Sa. lpSecurityDescriptor = NULL;
Sa. nLength = sizeof (SECURITY_ATTRIBUTES );
If (CreatePipe (& hRead, & hWrite, & sa, 0) // create an anonymous Pipeline
{
MessageBox ("An error occurred while creating the anonymous MPs queue! ");
Return;
}
// If the sub-process is successfully created, the sub-process transmits the read/write handle of the anonymous pipeline to the sub-process.
STARTUPINFO sui;
PROCESS_INFORMATION pi;
ZeroMemory (& sui, sizeof (STARTUPINFO); // set all the Members in this structure to 0.
Sui. cb = sizeof (STARTUPINFO );
Sui. dwFlags = STARTF_USESTDHANDLES;
Sui. hStdInput = hRead;
Sui. hStdOutput = hWrite;
Sui. hStdError = GetStdHandle (STD_ERROR_HANDLE );
If (! CreateProcess (".. \ Child \ Debug \ Child.exe", NULL,
TRUE, 0, NULL, NULL, & sui, & pi ))
{
CloseHandle (hRead );
CloseHandle (hWrite );
HRead = NULL;
HWrite = NULL;
MessageBox ("failed to create sub-process! ");
Return;

}
Else
{
CloseHandle (pi. hProcess); // closes the master Process Handle
CloseHandle (pi. hThread); // closes the main process thread handle
}

} (4) Compile the specific implementation of OnPipeRead () and OnPipeWrite:
[Cpp]
Void CParentView: OnPipeRead ()
{
// TODO: Add your command handler code here
Char buf [100];
DWORD dwRead;
If (! ReadFile (hRead, buf, 100, & dwRead, NULL ))
{
MessageBox ("failed to read data! ");
Return;
}
MessageBox (buf );
}
 
Void CParentView: OnPipeWrite ()
{
// TODO: Add your command handler code here
Char buf [] = "http://www.sunxin.org ";
DWORD dwWrite;
If (! WriteFile (hWrite, buf, strlen (buf) + 1, & dwWrite, NULL ))
{
MessageBox ("failed to write data! ");
Return;
}
}

Void CParentView: OnPipeRead ()
{
// TODO: Add your command handler code here
Char buf [100];
DWORD dwRead;
If (! ReadFile (hRead, buf, 100, & dwRead, NULL ))
{
MessageBox ("failed to read data! ");
Return;
}
MessageBox (buf );
}

Void CParentView: OnPipeWrite ()
{
// TODO: Add your command handler code here
Char buf [] = "http://www.sunxin.org ";
DWORD dwWrite;
If (! WriteFile (hWrite, buf, strlen (buf) + 1, & dwWrite, NULL ))
{
MessageBox ("failed to write data! ");
Return;
}
}

 

 

<2> Add another project to the project. The project name is also based on a single document. The project name is "Child" and the project level is equal to that of "Parent.
(1) Add the following menu items and add the command response function CChildView: OnPipeRead (), CChildView: OnPipeWrite ();


(2) First, we need to obtain the standard input and standard output handle of the child process. This can be obtained when the CChildView class window is completely created. In this case, we can use a CChildView :: onInitialUpdate () is the first function to be executed after the window is fully created.
(3) add two handles HANDLE hRead and HANDLE hWrite to the CChildView class, set the attributes to private, initialize them in the constructor, and release the HANDLE in the destructor.
[Cpp]
CChildView: CChildView ()
{
// TODO: add construction code here
HRead = NULL;
HWrite = NULL;
}
 
CChildView ::~ CChildView ()
{
If (hRead)
CloseHandle (hRead );
If (hWrite)
CloseHandle (hWrite );
}

CChildView: CChildView ()
{
// TODO: add construction code here
HRead = NULL;
HWrite = NULL;
}

CChildView ::~ CChildView ()
{
If (hRead)
CloseHandle (hRead );
If (hWrite)
CloseHandle (hWrite );
} (4) in the OnInitialUpdate () function, get the standard input and output handle using GetStdHandle (STD_INPUT_HANDLE) and GetStdHandle (STD_OUTPUT_HANDLE ).
[Cpp]
HRead = GetStdHandle (STD_INPUT_HANDLE); // obtain the read handle of the MPs queue.
HWrite = GetStdHandle (STD_OUTPUT_HANDLE); // get the pipeline write handle

HRead = GetStdHandle (STD_INPUT_HANDLE); // obtain the read handle of the MPs queue.
HWrite = GetStdHandle (STD_OUTPUT_HANDLE); // get the write handle of the pipeline (5). Then write the specific implementation of the OnPipeRead () process and OnPipeWrite () Code of the write process:
[Cpp]
Void CChildView: OnPipeRead ()
{
// TODO: Add your command handler code here
Char buf [100];
DWORD dwRead;
If (! ReadFile (hRead, buf, 100, & dwRead, NULL ))
{
MessageBox ("failed to read data! ");
Return;
}
MessageBox (buf );
}
 
Void CChildView: OnPipeWrite ()
{
// TODO: Add your command handler code here
Char buf [] = "anonymous Pipeline Test Program ";
DWORD dwWrite;
If (! WriteFile (hWrite, buf, strlen (buf) + 1, & dwWrite, NULL ))
{
MessageBox ("failed to write data! ");
Return;
}
}

Void CChil
DView: OnPipeRead ()
{
// TODO: Add your command handler code here
Char buf [100];
DWORD dwRead;
If (! ReadFile (hRead, buf, 100, & dwRead, NULL ))
{
MessageBox ("failed to read data! ");
Return;
}
MessageBox (buf );
}

Void CChildView: OnPipeWrite ()
{
// TODO: Add your command handler code here
Char buf [] = "anonymous Pipeline Test Program ";
DWORD dwWrite;
If (! WriteFile (hWrite, buf, strlen (buf) + 1, & dwWrite, NULL ))
{
MessageBox ("failed to write data! ");
Return;
}
}

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.