Unable to extract data after PtrToStructure through NamedPipe [C#]
<p>Ok, I'm trying to send a STRUCT from a legacy C++ application to a new C# application using Named Pipes, however I can't seem to find out how to correctly extract the information on the C# side - here is the code I currently have:</p>

<pre><code>C++ Sending Code

// Structure for message
struct MESSAGE
{
    CHAR  cSender[256];
    CHAR  cCommand[256];
};

// Sending the MESSAGE
DWORD dwWrote = 0
MESSAGE msg;
strcpy(msg.cSender, "LOCAL");
strcpy(msg.cCommand, "COMMAND");
WriteFile(pipe, msg, sizeof(msg), dwWrote);



C# Recieving Code:

// Corresponding C# MESSAGE structure
[StructLayout(LayoutKind.Sequential)]
public struct MESSAGE
{
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
    public byte[] cSender;

    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
    public byte[] cCommand;
}

MESSAGE msg = new MESSAGE();
byte[] byteData = new byte[Marshal.SizeOf(msg)];
uint uBytesRead;

bool fSuccess = ReadFile(hPipeInst,
                  byteData,
                  (uint)byteData.Length,
                  out uBytesRead,
                  IntPtr.Zero);  // no overlapping


// Generate Structure at the C# side
IntPtr ptr = Marshal.AllocHGlobal(byteData.Length);
try
{
    Marshal.Copy(byteData, 0, ptr, byteData.Length);
    msg = (MESSAGE)Marshal.PtrToStructure(ptr, typeof(MESSAGE));
}
finally
{
    Marshal.FreeHGlobal(ptr);
}

// Extract Sender &amp; Command to be used
string sSender = Encoding.ASCII.GetString(msg.cSender);
string sCommand = Encoding.ASCII.GetString(msg.cCommand);
</code></pre>

<p>Some is going horribly wrong, when I test this and trace out sSender and sCommand I get the following:
Sender: LOCAL ?     (? 8? ?? ?? ?? X       P   P   ?? ???w@??w  ??? ?? ???J??w    x?   ??? ?? ???J??w    ?? #-|    ,? ????                    ?     J??w    ?? I -|    ??? ???D       0  \  ??   ?     ?           ?? ???    ?? </p>

<p>Command: COMMAND      ?? ???    ?? ?w?? ?              ?? ?0-|?       ?? ,? 72-|??          ??  ??J         ?? nW&amp;             "-|?       @? ?-|? ?-|p?? @? ?`&amp;          ??p?? O|   @? ?         ?? ^+ ??????? T? f    ??? l? , ??J </p>

<p>As you can see, in both cases the string is there (LOCAL &amp; COMMAND) but they are both followed by tons of garbage, is there something I can do to fix this or does the cSender and cCommand have to be exactly FIXED LENGTH? I put them at 256/256 thinking "that would be more then long enough for anything I could ever need" but now I am wondering ...</p>

<p>Any help would be much appreciated.
Thanks,</p>
