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");
}
private void button1_Click(object sender, EventArgs e)
{
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int bytesRead = 0;
NativeMethods.WTSVirtualChannelRead(mHandle, 0, buffer, bufferSize, ref bytesRead);
if (bytesRead != 0)
{
MessageBox.Show("Got no Data");
}
else
{
MessageBox.Show("Got data: " + bytesRead);
}
}
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);
}
On NativeMethods.WTSVirtualChannelRead(mHandle, 0, buffer, bufferSize, ref bytesRead); I get the following error every time.
System.AccessViolationException was unhandled by user code
Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Source=RemoteForm
StackTrace:
at RemoteForm.NativeMethods.WTSVirtualChannelRead(IntPtr channelHandle, Int64 timeout, Byte[] buffer, Int32 length, Int32& bytesReaded)
at RemoteForm.Form1.button1_Click(Object sender, EventArgs e) in E:\Visual Studio 2010\Projects\RemoteForm\Form1.cs:line 31
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
InnerException:
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", "This is a test");
}
}
//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;
Any help on this would be greatly appreciated.