-1

Related question

In C++, I require a TCHAR string (LPTSTR). C# StreamWriters can output ASCII, Unicode, UTF32, etc... Not TCHAR strings.

I am not calling a function in C++, I am sending a string message over a named pipe.

C#:

using (NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", "mynamedpipe", PipeDirection.InOut))
using (StreamWriter sw = new StreamWriter(pipeClient, Encoding.UTF8))
using (StreamReader sr = new StreamReader(pipeClient, Encoding.Unicode))
{
    pipeClient.Connect();
    pipeClient.ReadMode = PipeTransmissionMode.Message;
    sw.Write("Howdy from Kansas");
    sw.Flush();

    var b = sr.ReadLine();
    Console.Write(b);
}

C++ expects a TCHAR. Suggestions?

Community
  • 1
  • 1
HL-SDK
  • 160
  • 10
  • Remember that `TCHAR` is not really a definite type. It's a possibility of a type, after the programmer decides. See the [tchar tag-wiki](https://stackoverflow.com/tags/tchar/info). – Deduplicator Feb 23 '15 at 18:58
  • Are you really still coding for Windows 98? No other reason to use `TCHAR`? – David Heffernan Feb 23 '15 at 19:24
  • No, sorry my windows C/C++ experience is spotty at best. It is apparently a wchar_t. I was confusing it with the likes of BSTR and com interop. I may be able to answer this question with some more trial&error. – HL-SDK Feb 23 '15 at 19:48
  • @Deduplicator, you are right. How do you decide [TCHAR in DEV_BROADCAST_DEVICEINTERFACE](https://msdn.microsoft.com/en-us/library/windows/desktop/aa363244.aspx)? – AaA Apr 09 '18 at 11:32

2 Answers2

0

This is not a direct answer since it does not use a streamwriter, as was the goal. Due to limitations though, this approach works just fine.

Workaround:

using (NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", "mynamedpipe", PipeDirection.InOut))
{
    pipeClient.Connect();
    pipeClient.ReadMode = PipeTransmissionMode.Message;
    var msg = Encoding.Unicode.GetBytes("Hello from Kansas!");
    pipeClient.Write(msg, 0, msg.Length);
}
HL-SDK
  • 160
  • 10
0

According to your comments, you actually need UTF-16 encoded text. That corrresponds to Encoding.Unicode. So you would use

new StreamWriter(pipeClient, Encoding.Unicode)

That said, you should also at least consider the issue of endianness. When transmitting data over a network I would expect you to convert to network byte order when ending, and convert to host byte order when receiving.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • I need to find a bit more proof and I agree with you. I think it was nasty stream business that was interacting poorly with the message mode of pipes. I'll report back soon – HL-SDK Feb 24 '15 at 16:40