+1  A: 

Well the issue is you are sending a 16bit unicode string in the sending side and reading out a ansi string on the other so the marshalling layer is terminating the string buffer at the first NUL character. You could either changing the UnmanagedType.LPStr to UnmanagedType.LPWStr or marshal it as a byte array and then convert to a string using a Unicode Encoding class.

Something like this might work (NOTE: untested code as I don't have a server to test on):

public static extern int WTSVirtualChannelRead(IntPtr hChannel, 
        uint Timeout, 
        [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex=3)] byte[] Buffer, 
        uint BufferSize, 
        out uint BytesRead);

string DoRead(IntPtr hChannel)
{ 
    byte[] buf = new byte[1024];
    uint bytesRead;
    if (WTSVirtualChannelRead(hChannel, 0, buf, (uint)buf.Length, out bytesRead) != 0)
    {
        return Encoding.Unicode.GetString(buf, 0, (int)bytesRead);
    }
    else
    {
        return "";
    }
}
tyranid
Changing LPStr to LPWStr got me close, that returned `Hello World!豈悢怒`
Scott Chamberlain
Using your example code fixed it.
Scott Chamberlain
A: 

I feel like taking a shower after writing this but...

private void button1_Click(object sender, EventArgs e)
{
    uint bufferSize = 2;
    StringBuilder buffer = new StringBuilder();
    StringBuilder final = new StringBuilder();
    uint bytesRead;
    NativeMethods.WTSVirtualChannelRead(mHandle, 0, buffer, bufferSize, out bytesRead);
    while (bytesRead != 0)
    {
        final.Append(buffer);
        NativeMethods.WTSVirtualChannelRead(mHandle, 0, buffer, bufferSize, out bytesRead);
    }
        MessageBox.Show("Got data: " + final.ToString());
}

If anyone else can provide a better solution to the only one character transmitting problem I will gladly accept that instead of this.

Scott Chamberlain