Pergunta

I am trying to write a hello world type program for using virtual channels in the windows terminal services client.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

    }
    IntPtr mHandle = IntPtr.Zero;
    private void Form1_Load(object sender, EventArgs e)
    {
        mHandle = NativeMethods.WTSVirtualChannelOpen(IntPtr.Zero, -1, "TSCRED");
        if (mHandle == IntPtr.Zero)
        {
            throw new Win32Exception(Marshal.GetLastWin32Error());
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        uint bufferSize = 1024;
        StringBuilder buffer = new StringBuilder();
        uint bytesRead;
        NativeMethods.WTSVirtualChannelRead(mHandle, 0, buffer, bufferSize, out bytesRead);
        if (bytesRead == 0)
        {
            MessageBox.Show("Got no Data");
        }
        else
        {
            MessageBox.Show("Got data: " + buffer.ToString());
        }
    }

    protected override void Dispose(bool disposing)
    {
        if (mHandle != System.IntPtr.Zero)
        {
            NativeMethods.WTSVirtualChannelClose(mHandle);
        }
        base.Dispose(disposing);

    }
}

internal static class NativeMethods
{
    [DllImport("Wtsapi32.dll")]
    public static extern IntPtr WTSVirtualChannelOpen(IntPtr server,
        int sessionId, [MarshalAs(UnmanagedType.LPStr)] string virtualName);

    //[DllImport("Wtsapi32.dll", SetLastError = true)]
    //public static extern bool WTSVirtualChannelRead(IntPtr channelHandle, long timeout,
    //        byte[] buffer, int length, ref int bytesReaded);

    [DllImport("Wtsapi32.dll")]
    public static extern bool WTSVirtualChannelClose(IntPtr channelHandle);


    [DllImport("Wtsapi32.dll", EntryPoint = "WTSVirtualChannelRead")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool WTSVirtualChannelRead(
        [In()] System.IntPtr hChannelHandle
        , uint TimeOut
        , [Out()] [MarshalAs(UnmanagedType.LPStr)] 
          System.Text.StringBuilder Buffer
        , uint BufferSize
        , [Out()] out uint pBytesRead);

}

I am sending the data from the MSTSC COM object and ActiveX controll.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        rdp.Server = "schamberlainvm";
        rdp.UserName = "TestAcct";
        IMsTscNonScriptable secured = (IMsTscNonScriptable)rdp.GetOcx();
        secured.ClearTextPassword = "asdf";
        rdp.CreateVirtualChannels("TSCRED");
        rdp.Connect();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        rdp.SendOnVirtualChannel("TSCRED", "Hello World!");
    }
}
//Designer code
// 
// rdp
// 
this.rdp.Enabled = true;
this.rdp.Location = new System.Drawing.Point(12, 12);
this.rdp.Name = "rdp";
this.rdp.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("rdp.OcxState")));
this.rdp.Size = new System.Drawing.Size(1092, 580);
this.rdp.TabIndex = 0;

I am getting a execption every time NativeMethods.WTSVirtualChannelRead runs

Any help on this would be greatly appreciated.

EDIT -- mHandle has a non-zero value when the function runs. updated code to add that check.

EDIT2 -- I used the P/Invoke Interop Assistant and generated a new sigiture

    [DllImport("Wtsapi32.dll", EntryPoint = "WTSVirtualChannelRead")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool WTSVirtualChannelRead(
        [In()] System.IntPtr hChannelHandle
        , uint TimeOut
        , [Out()] [MarshalAs(UnmanagedType.LPStr)] 
          StringBuilder Buffer
        , uint BufferSize
        , [Out()] out uint pBytesRead);

it now receives the text string (Yea!) but it only gets the first letter of my test string(Boo!). Any ideas on what is going wrong?

EDIT 3 --- After the call that should of read the hello world;

BytesRead = 24

Buffer.Length = 1; Buffer.Capacity = 16; Buffer.m_StringValue = "H";

Foi útil?

Solução

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 "";
    }
}

Outras dicas

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top