7

I have two different executable files running on same computer which has Windows OS. Both of them are build in Unity. I want to send message from one to other without using network.

How do you send message from an exe program to another exe program in Unity?

Is this possible with integrated Mono/.net functionality or something else?

Programmer
  • 121,791
  • 22
  • 236
  • 328
mcelik
  • 1,073
  • 2
  • 12
  • 22

2 Answers2

10

You can use Named Pipes which uses shared memory to communicate with another application on the-same machine.

Go to File --> Build Settings... select PC, Mac & Linux Standalone then click on Player Settings.... Now, change Api Compatibility Level to .NET 2.0.

Close and re-open Visual Studio. Now, you can import using System.IO.Pipes; and be able to use NamedPipeServerStream and NamedPipeClientStream.

Below is a very simplified server and client. You must do that in a Thread and should also handle exception.

If you don't want to use Thread, there is also asynchronous parameter (PipeOptions.Asynchronous) that makes it a non blocking operator. It gets complicated from there and you have to look for some examples for that on MS doc.

Simple Server:

//Create Server Instance
NamedPipeServerStream server = new NamedPipeServerStream("MyCOMApp", PipeDirection.InOut, 1);
//Wait for a client to connect
server.WaitForConnection();
//Created stream for reading and writing
StreamString serverStream = new StreamString(server);
//Send Message to Client
serverStream.WriteString("Hello From Server");
//Read from Client
string dataFromClient = serverStream.ReadString();
UnityEngine.Debug.Log("Received from Client: " + dataFromClient);
//Close Connection
server.Close();

Simple Client:

//Create Client Instance
NamedPipeClientStream client = new NamedPipeClientStream(".", "MyCOMApp",
               PipeDirection.InOut, PipeOptions.None,
               TokenImpersonationLevel.Impersonation);

//Connect to server
client.Connect();
//Created stream for reading and writing
StreamString clientStream = new StreamString(client);
//Read from Server
string dataFromServer = clientStream.ReadString();
UnityEngine.Debug.Log("Received from Server: " + dataFromServer);
//Send Message to Server
clientStream.WriteString("Bye from client");
//Close client
client.Close();

The StreamString class from MS Doc:

public class StreamString
{
    private Stream ioStream;
    private UnicodeEncoding streamEncoding;

    public StreamString(Stream ioStream)
    {
        this.ioStream = ioStream;
        streamEncoding = new UnicodeEncoding();
    }

    public string ReadString()
    {
        int len = 0;

        len = ioStream.ReadByte() * 256;
        len += ioStream.ReadByte();
        byte[] inBuffer = new byte[len];
        ioStream.Read(inBuffer, 0, len);

        return streamEncoding.GetString(inBuffer);
    }

    public int WriteString(string outString)
    {
        byte[] outBuffer = streamEncoding.GetBytes(outString);
        int len = outBuffer.Length;
        if (len > UInt16.MaxValue)
        {
            len = (int)UInt16.MaxValue;
        }
        ioStream.WriteByte((byte)(len / 256));
        ioStream.WriteByte((byte)(len & 255));
        ioStream.Write(outBuffer, 0, len);
        ioStream.Flush();

        return outBuffer.Length + 2;
    }
}
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • 1
    Thank you! This is the solution I was looking for – mcelik Mar 28 '17 at 08:24
  • 1
    I implemented the code you provided. But it throws this error : NotImplementedException: ACL is not supported in Mono I searched for a solution but couldn't find an answer. – mcelik Mar 30 '17 at 13:05
  • You are running this on windows, right? Please start commenting from bottom to up to see which line of code is causing that error. – Programmer Mar 30 '17 at 16:43
  • Yes. It is running on Windows 8.1. It throws the error in the beginning when it creates new NamedPipeClientStream. – mcelik Mar 31 '17 at 05:27
  • `NamedPipeServerStream` must be running before you can use `NamedPipeClientStream`. Can you run it as an admin and see what happens? – Programmer Mar 31 '17 at 05:43
  • So this works in Unity now? For a while people were saying that named pipes caused a crash. – Jeff B Feb 25 '19 at 16:27
  • `Cannot resolve symbol 'StreamString'` I got this error in my case. Any ideas why? I included using System.IO; – tpbafk Dec 21 '21 at 15:16
1

You could have a file they both write and read to. You can put a timestamp with it to show when the last message was written

Youri Leenman
  • 228
  • 2
  • 8
  • True @Programmer You could use like a database which you add rows with a message column to but that is quite a lot of work for a very little thing – Youri Leenman Mar 28 '17 at 07:27
  • 2
    Year. Any database that uses RAM for storage should do it. I think there are few out there. Although, that's like drink water with a bucket. – Programmer Mar 28 '17 at 08:19
  • I don't think your answer is very useful as it stands now. I think you at least need to mention the problems that might occur with this solution. What happens when both applications try to write to the file at the same time for example? Or when one application is still writing and the other already wants to read from the file? – PJvG Mar 28 '17 at 08:19
  • 1
    I thought about this too but this didn't seem like a good solution. But it solves the problem anyway :) – mcelik Mar 28 '17 at 08:20
  • if you use `File.WriteAllText("path", "message");` you should not have this problem – Youri Leenman Mar 28 '17 at 08:22