Click here to Skip to main content
15,881,173 members
Articles / Programming Languages / C#
Tip/Trick

C# MSConnectionCS DLL using Mailslots for Inter-process Communication

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
4 Aug 2020CPOL2 min read 4.4K   172   7   1
Mailslot Inter-process communication
This library was developed to add compatibility with a C# program for my old project, written in C++ using mailslots.

Introduction

The purpose of creating MSConnectionCS DLL using mailslots is to minimize the efforts of the programmer for developing C# applications.

It was necessary to add compatibility with a C# program for my old project, written in C++ using mailslots, so there was a need to develop this library.

In contrast with the following articles...

...this library does not use unsafe code, has separated classes for client and server and transmits not only string messages but also structures.

A Mailslot is a one-way interprocess communication mechanism, available on the Microsoft Windows operating system, that allows communication between processes both locally and over a network (https://en.wikipedia.org/wiki/Mailslot).

MSConnectionCS DLL includes the following classes:

  • MSConnectionCS: structures and functions, used in client and server classes
  • MSConnectionClient: functions for creation client object and sending messages to mailslot.
    This class uses as is in client application.
  • MSConnectionServer: functions for creation server object and receiving messages from mailslot.
    This class is base class for user's class in server application for processing received messages (see class MSConnectionServerTest in download source files).
    Marshaling mechanism is used for preparation messages.
    Each message includes CONNECT_HEADER or CONNECT_SHORT_HEADER and data.
  • CONNECT_HEADER – for transmit data between computers
  • CONNECT_SHORT_HEADER – for transmit data between processes in one computer.
    C#
    public struct CONNECT_SHORT_HEADER
    {
      public uint msg_id;
      public uint tickCount;
      public uint ProcessId;
      public int RandomId;      // offset 12 bytes
      [MarshalAs(UnmanagedType.ByValArray, SizeConst = Global.MAX_CLIENTNAME_LENGTH)]
      public byte[] ClientName;
    }
    

Field msg_id is filled by User for identification transmitted message on server side, each message is described in its structure (see file UserStructures.cs in download source files).

Field RandomId is filled automatically and used to throw away repeated messages in case of transmitting messages between computers, because Mailslot mechanism transmits data using several protocols. I received two messages in my demo, but if to turn off access resources on a Microsoft network, only one message transmits.

Run ncpa.cpl:

Image 1

Block Diagrams MSConnectionCS DLL

Transmit messages between processes in one computer (ConnectionMode = SAME_COMPUTER):

Image 2

Transmit messages between computers (ConnectionMode = OTHER_COMPUTER):

Image 3

Creation Server

C#
server = new MSConnectionServerTest();
// numberOfMessages - only for ConnectionMode.SAME_COMPUTER
// mode = ConnectionMode.SAME_COMPUTER | ConnectionMode.OTHER_COMPUTER
bool ret = server.BuildServer(MailslotName, (int)numberOfMessages, mode);

if (ret == false)
{
   Console.WriteLine("Server Creation error!");
   Environment.Exit(0);
}
while (true)
{
   string word = Console.ReadLine();
   if (word == "exit")
   {
      server.Dispose();
      Environment.Exit(0);
   }
}

Creation Client

C#
client = new MSConnectionCS.MSConnectionClient();
client.BuildClient(server_address, Name, client_name, mode);

Demo Parameters and Running

    usage: mscon /wm:server|client|debug /cm:0|1 /print:0|1       /nmsg:number
      /msname:<mailslot name>
        /cname:<client name>
          /scomp:<server comp.name>
            /scompip:<server comp.ip>
              /wm      - workmode
              /cm      - connection mode 1 - different computers or 
                                         0 -same computer (by default 1)
              /print   - write(1) or not write(0) on console debug information (by default 1)
              /nmsg    - numberOfMessages in MailSlot queue - only for /cm = 0 
                                                              (by default = 10)
              /msname  - server_name by default = server
              /cname   - client_name < 19 symbols
              /scomp   - server computer name  (if server computer name  != "."  /cm = 1
              /scompip - server computer id (if server computer ip is set - /cm = 1  
  
    Run console application demo mscon.exe twice:
       mscon /wm:server /msname:myserver
       mscon /wm:client /msname:myserver
    or once:
       mscon /wm:debug /msname:myserver<br>

    other examples:
       mscon /wm:debug /msname:myserver /scomp:DESKTOP_OK
       mscon /wm:debug /msname:myserver /scompip:10.0.0.14  

Conclusion

If your server application must receive messages from multiple clients, Mailslot Inter-process Communication is a good decision. But do not forget that size of message cannot be larger than 424 bytes when sent between computers. If size of message is larger, you can fragment it and use client name and client computer name in CONNECT_HEADER, but MSConnection DLL does not support this feature.

History

  • 5th August, 2020: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Slovenia Slovenia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
PraiseMailslots !?! Pin
Ashok Kumar RV5-Aug-20 2:21
Ashok Kumar RV5-Aug-20 2:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.