WTSVirtualChannelRead Only reads the first letter of the string.
- by Scott Chamberlain
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";