Recently I made a small program, some of which use C ++ and some use C #. The two programs need to call each other. Both programs have interfaces. I have never called C # And C ++ in the past. Although C # Can call C ++ DLL, I am not familiar with creating C ++ dll, in addition, it is inconvenient to make the C ++ function in this program into a dll, so I want to make C # And C ++ into two independent programs and implement mutual calls through process communication.
I have found an article on the Internet titled pipeline for Process Communication. I can follow this article and repost it by the way.
The following is a reprinted article.
This article is just a test example. The core code is a set of windows api functions in kernel32.dll. The code is on the codeproject.
Http://www.codeproject.com/KB/threads/dotnetnamedpipespart1.aspx
The test result is as follows. You can obtain feedback after sending messages to the console app and aspx:
The console app is the server code as follows:
UsingSystem;
UsingAppModule. InterProcessComm;
UsingAppModule. NamedPipes;
UsingSystem. Threading;
NamespaceServer
{
ClassProgram
{
// ** C # Use namedpipe for inter-process communication
// ** The component code is from codeproject
// ** Http://www.codeproject.com/KB/threads/dotnetnamedpipespart1.aspx
// ** Download the code above and compile the AppModule. InterProcessComm and AppModule. NamedPipes.
// ** Reference the two dll files to this example and run the following code as a server-side test.
// ** Test code byjinjazz (because the two test programs of the original author are complicated, this is simplified for your reference)
StaticvoidMain (string [] args)
{
ServerPipeConnectionPipeConnection = newServerPipeConnection ("np-test-by-jinjazz", 512,512,500 0, false );
Console. WriteLine ("listening ..");
While (true)
{
Try
{
PipeConnection. Disconnect ();
PipeConnection. Connect ();
Stringrequest = PipeConnection. Read ();
If (! String. IsNullOrEmpty (request ))
{
Console. WriteLine ("get:" + request );
PipeConnection. Write ("get:" + request );
If (request. ToLower () = "break") break;
}
}
Catch (Exceptionex)
{
Console. WriteLine (ex. Message );
Break;
}
}
PipeConnection. Dispose ();
Console. Write ("pressanykeytoexit ..");
Console. Read ();
}
}
}
The aspx code of the client is as follows:
UsingSystem;
UsingSystem. Web;
UsingAppModule. InterProcessComm;
UsingAppModule. NamedPipes;
Publicpartialclass_Default: System. Web. UI. Page
{
ProtectedvoidPage_Load (objectsender, EventArgse)
{
Response. Write (SendRequest ("test asdf "));
}
/// <Summary>
/// Test the namepiped Client
/// </Summary>
/// <Paramname = "request"> send command </param>
/// <Returns> return data </returns>
StringSendRequest (stringrequest)
{
Stringresponse = "";
IInterProcessConnectionclientConnection = null;
Try
{
ClientConnection = newClientPipeConnection ("np-test-by-jinjazz ",".");
ClientConnection. Connect ();
ClientConnection. Write (request );
Response = clientConnection. Read ();
ClientConnection. Close ();
}
Catch (Exceptionex)
{
ClientConnection. Dispose ();
Response = ex. Message;
}
Returnresponse;
}
}
The test environment is windows vista and windows.