我正在尝试编写一个 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_StringValue = "H";