C#中远程桌面连接出现奇怪的H_RESULT E_FAIL错误

时间:2011-06-07 14:26:29

标签: c# com rdp

我在C#中使用com组件Microsoft RDP Client Control版本8连接到终端服务器和远程桌面。

代码看起来像这样:

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;

这是第一次运行到远程桌面或终端服务服务器,但在后续调用失败时出现“H_RESULT E_FAIL”错误,堆栈跟踪位于下方。

如何允许某人从同一表单重新连接到其他位置(即在更改属性和尝试连接之前需要清理什么?)

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)

1 个答案:

答案 0 :(得分:1)

结果很简单。

对Disconnect()的调用是异步的。

我必须处理ActiveX控件的OnDisconnect事件,而不是再次尝试Connect(),直到它被触发,例如完全断开了。