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

本文关键字:RESULT 错误 FAIL 程桌面 连接 桌面 | 更新日期: 2023-09-27 17:58:30

我使用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"错误,堆栈跟踪在下面。

我如何允许某人从相同的表单重新连接到其他地方(即,在更改属性并尝试connect()之前,我需要清理什么?)

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)

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

答案很简单。

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

我不得不处理ActiveX控件的OnDisconnect事件,在它触发之前(例如完全断开连接),我不会再次尝试Connect()。