Interprocess Communication (IPC) is a set of methods for the exchange of data among multiple threads in one or more processes.
IPC has four general approaches:
Pipes are basically files that reside in memory instead on disk. Writing to a pipe and reading from a pipe is in FIFO manner.
In my last project i came accross a scenario where my plugin needs to interact with a C# process.
Plugin need to send some data to C# process. Based on that data C# process needs to perform some action and on successful completion, C# process sends back some data to the plugin.
The following code creates a named pipe, executes the C# process, sends data to the C# process and waits till C# process completes its processing. After C# process completes its processing the code reads data from the named pipe.
#define BUFFER_SIZE 100
//The unique pipe name. This string must have the following form:\\.\pipe\pipename
LPTSTR lpszPipename = TEXT(\\\\.\\pipe\\mynamedpipe);
HANDLE pipe = CreateNamedPipe(lpszPipename, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE|PIPE_READMODE_BYTE|PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, 1024, 1024, NMPWAIT_USE_DEFAULT_WAIT, NULL);
// If pipe is created successfully.
if (pipe != INVALID_HANDLE_VALUE)
//Execute the external process which will first connect to the pipe and will read data from the pipe.
ShellExecute(NULL,"open",<external process name>,NULL,NULL,SW_SHOWNORMAL);
// Wait for the client(external process) to connect.
BOOL bConnected = ConnectNamedPipe(pipe, NULL) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);
string pipeData = "Hello";
// Send data in named pipe.
WriteFile(pipe, pipeData.c_str(), pipeData.length(), &numWritten, NULL);
//Get data from named pipe.
DWORD cbBytesRead, cbRequestBytes = 0;
cbRequestBytes = BUFFER_SIZE;
BOOL bConnected1 = ConnectNamedPipe(pipe, NULL) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);
if(bConnected1 == TRUE)
ReadFile(pipe, chRequest, cbRequestBytes, &cbBytesRead, NULL); // chRequest contains the data read from named pipe.
// Close the pipe.