Reputation: 27
i've been trying to use Named Pipes in C# for a while but can't get it to work. I'm working with the following code:
internal class SplitManager
{
public static void initialize()
{
Debug.Log("Initializing LiveSplit pipe");
SplitManager.pipeClientStream = new NamedPipeClientStream("//.//pipe//LiveSplit");
Debug.Log("Successfully initialized LiveSplit pipe");
}
public static void startRun()
{
Debug.Log("[PIPE]: start");
SplitManager.WriteString("start");
}
public static void performSplit()
{
Debug.Log("[PIPE]: split");
SplitManager.WriteString("split");
}
private static void WriteString(string str)
{
SplitManager.pipeClientStream.Connect();
new StreamString(SplitManager.pipeClientStream).WriteString(str);
SplitManager.pipeClientStream.Close();
}
private static NamedPipeClientStream pipeClientStream;
}
public class StreamString
{
public StreamString(Stream ioStream)
{
this.ioStream = ioStream;
this.streamEncoding = new UnicodeEncoding();
}
public string ReadString()
{
int num = this.ioStream.ReadByte() * 256;
num += this.ioStream.ReadByte();
byte[] array = new byte[num];
this.ioStream.Read(array, 0, num);
return this.streamEncoding.GetString(array);
}
public int WriteString(string outString)
{
byte[] bytes = this.streamEncoding.GetBytes(outString);
int num = bytes.Length;
if (num > 65535)
{
num = 65535;
}
this.ioStream.WriteByte((byte)(num / 256));
this.ioStream.WriteByte((byte)(num & 255));
this.ioStream.Write(bytes, 0, num);
this.ioStream.Flush();
return bytes.Length + 2;
}
private Stream ioStream;
private UnicodeEncoding streamEncoding;
}
When i run this code i get Win32Exception
with an error message that it cannot find the specified file. I'm 100% sure the path is fine, since i checked it with powershell command [System.IO.Directory]::GetFiles("\\.\\pipe\\")
. Any ideas why does this error happen?
Upvotes: 1
Views: 492
Reputation: 27
It turns out that C# actually provides the "//.//pipe//" prefix to the string, so simply replacing SplitManager.pipeClientStream = new NamedPipeClientStream("//.//pipe//LiveSplit");
to SplitManager.pipeClientStream = new NamedPipeClientStream("LiveSplit");
worked!
Upvotes: 0