1

我正在尝试编写一个 hello world 类型的程序以在 Windows 终端服务客户端中使用虚拟通道。

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);

}

我正在从 MSTSC COM 对象和 ActiveX 控件发送数据。

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;

每次NativeMethods.WTSVirtualChannelRead运行我都会得到一个执行

对此的任何帮助将不胜感激。

编辑——当函数运行时,mHandle 有一个非零值。更新代码以添加该检查。

EDIT2 -- 我使用了P/Invoke Interop Assistant并生成了一个新的签名

    [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);

它现在接收文本字符串(是的!),但它只获取我的测试字符串的第一个字母(嘘!)。关于出了什么问题的任何想法?

编辑 3 --- 在应该读取 hello world 的调用之后;

字节读取 = 24

缓冲区长度 = 1; 缓冲区.容量 = 16; Buffer.m_S​​tringValue = "H";

4

2 回答 2

2

那么问题是您在发送端发送一个 16 位 unicode 字符串并在另一端读取一个 ansi 字符串,因此编组层在第一个 NUL 字符处终止字符串缓冲区。您可以将其更改UnmanagedType.LPStrUnmanagedType.LPWStr或将其编组为字节数组,然后使用 Unicode 编码类转换为字符串。

这样的事情可能会起作用(注意:未经测试的代码,因为我没有要测试的服务器):

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 "";
    }
}
于 2010-04-26T21:36:06.153 回答
0

写完这篇我想洗澡,但是...

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());
}

如果其他人可以为唯一的一个字符传输问题提供更好的解决方案,我将很乐意接受它而不是这个。

于 2010-04-26T21:49:57.097 回答