问题
I'm trying to make a WinForms RDP client in C# using the RDP ActiveX control as a bit of a learning exercise. I can get everything working when the target server doesn't use Network Level Authentication (NLA), but when I try and configure the control to use 'EnableCredSspSupport', which I think is needed for NLA, I get the following error when running through the code:
An unhandled exception of type 'System.Windows.Forms.AxHost.InvalidActiveXStateException' occurred in AxInterop.MSTSCLib.dll
The code is:
AxMsRdpClient9NotSafeForScripting rdp;
rdp = new AxMsRdpClient9NotSafeForScripting();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
((System.ComponentModel.ISupportInitialize)(rdp)).BeginInit();
rdp.Dock = System.Windows.Forms.DockStyle.Fill;
rdp.Enabled = true;
rdp.Location = new System.Drawing.Point(0, 0);
rdp.Name = "rdp";
rdp.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("rdp.OcxState")));
rdp.Size = new System.Drawing.Size(828, 687);
rdp.TabIndex = 0;
rdp.AdvancedSettings9.AuthenticationLevel = 2;
rdp.AdvancedSettings9.EnableCredSspSupport = true;
MainsplitContainer.Panel2.Controls.Add(rdp);
((System.ComponentModel.ISupportInitialize)(rdp)).EndInit();
rdp.Server = "XXX.XXX.XXX.XXX";
rdp.Connect();
A lot of this code is mangled together from the following sources, but learning to code is usually standing on the shoulders of giants, right!?
http://msdn.microsoft.com/en-us/library/aa383022(VS.85).aspx https://searchcode.com/codesearch/view/3716390/
...And a couple more sources I don't have the reputation to share (First post!)
Any insights out there to help me on my way?
Thanks!
....Aaaand working code:
AxMSTSCLib.AxMsRdpClient8NotSafeForScripting _RDPClient;
_RDPClient = new AxMSTSCLib.AxMsRdpClient8NotSafeForScripting();
MainsplitContainer.Panel2.Controls.Add(_RDPClient);
((System.ComponentModel.ISupportInitialize)(_RDPClient)).BeginInit();
_RDPClient.Dock = System.Windows.Forms.DockStyle.Fill;
_RDPClient.Enabled = true;
_RDPClient.Location = new System.Drawing.Point(0, 0);
_RDPClient.Name = "axMsTscAxNotSafeForScripting1";
_RDPClient.OcxState = ((System.Windows.Forms.AxHost.State)(_RDPClient.OcxState));
_RDPClient.Size = new System.Drawing.Size(579, 608);
_RDPClient.TabIndex = 0;
_RDPClient.AdvancedSettings8.EnableCredSspSupport = true;
((System.ComponentModel.ISupportInitialize)(_RDPClient)).EndInit();
_RDPClient.OnDisconnected += new IMsTscAxEvents_OnDisconnectedEventHandler(axMsTscAx_OnDisconnected);
_RDPClient.Server = IP;
_RDPClient.Connect();
回答1:
Use :
CType(rdp, System.ComponentModel.ISupportInitialize).EndInit()
rdp.CreateControl()
Find your equivalent in C#
来源:https://stackoverflow.com/questions/27963640/winforms-activex-rdp-client-issue-with-nla