C # use a named pipe for inter-process communication

Source: Internet
Author: User

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.

Related Article

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.