Strange H_RESULT E_FAIL error with Remote Desktop Connection in C#
I'm using the com component Microsoft RDP Client Control version 8 in C# to connect to terminal servers and remote desktops.
The code looks something like this:
if (rdp.Connected.ToString() == "1")
{
rdp.Disconnect();
}
rdp.Server = userDetails.RoleMachine;
rdp.UserName = userDetails.UserName;
IMsTscNonScriptable secured = (IMsTscNonScriptable)rdp.GetOcx();
secured.ResetPassword();
secured.ClearTextPassword = userDetails.Password;
This works the first time, to either a Remote Desktop or Terminal Services Server but fails on subsequent calls with an "H_RESULT E_FAIL" error, stack trace beneath.
How can I allow someone to reconnect to somewhere else from the same form (i.e. what do I need to clean up before changing properties and attempting a connect() ?开发者_Go百科)
at MSTSCLib.IMsRdpClient7.set_Server(String pServer)
at AxMSTSCLib.AxMsRdpClient7.set_Server(String value)
at RDP.FrmRemote.Connect() in \\..\FrmRemote.cs:line 56
at RDP.FrmRemote.Operations1_Click(Object sender, EventArgs e) in \\...\FrmRemote.cs:line 157
at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ToolStrip.WndProc(Message& m)
at System.Windows.Forms.ToolStripDropDown.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.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
The answer turned out to be quite simple.
A call to Disconnect() is asynchronous.
I had to handle the ActiveX control's OnDisconnect event, not attempting to Connect() again until it had fired, e.g. completely disconnected.
精彩评论