Net 3.5框架下运用。
RDPSession 可以用于无人为输入用户名和密码进行远程桌面的连接和共享。
原来在Win10下用visual studio 2019下进行3.5框架运用,但是调用3.5框架下的axRDPViewer会出错,因此改用windows7下的visual studio 2010进行(主要是考虑远古win系统应用)。
(以下代码参考了csdn和StackOverflow下的N多例子,再次感谢)
Server端:
添加引用:interop.RDPCAMPILib.dll
#region
private static RDPCOMAPILib.RDPSession rdp = new RDPCOMAPILib.RDPSession();
private static RDPCOMAPILib.RDPSRAPIInvitation invitation=null;
private static RDPCOMAPILib.IRDPSRAPIVirtualChannel virtualChannel;
private static string localIP;
private static string rdpConnectStr;
#endregion
//rdp 连接以后事件
rdp.OnAttendeeConnected+=new RDPCOMAPILib._IRDPSessionEvents_OnAttendeeConnectedEventHandler(rdp_OnAttendeeConnected);
//rdp失去连接以后事件
rdp.OnAttendeeDisconnected += new RDPCOMAPILib._IRDPSessionEvents_OnAttendeeDisconnectedEventHandler(rdp_OnAttendeeDisconnected);
//rdp设置能看的区域
rdp.SetDesktopSharedRect(0, 0, System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width, System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height);
//rdp创建一个发送message的,名称为111的虚拟通道,这个名称要和浏览端创建的虚拟通道名称一致
virtualChannel = rdp.VirtualChannelManager.CreateVirtualChannel("111",RDPCOMAPILib.CHANNEL_PRIORITY.CHANNEL_PRIORITY_HI,(uint)RDPCOMAPILib.CHANNEL_FLAGS.CHANNEL_FLAGS_LEGACY);
//rdp虚拟通道接收到信息事件
rdp.OnChannelDataReceived += new RDPCOMAPILib._IRDPSessionEvents_OnChannelDataReceivedEventHandler(rdp_OnChannelDataReceived);
//创建一个邀请string,null 和Guid参数为文档要求Unique,连接数为1,连接密码空
invitation = rdp.Invitations.CreateInvitation(null, System.Guid.NewGuid().ToString(), "", 1);
//这个string要发给浏览端
rdpConnectStr = invitation.ConnectionString;
rdp.Open();
Console.WriteLine("{0}:RDPSession is open.......",DateTime.Now.ToLongTimeString());
各种事件
//这个事件用来接收利用rdp虚拟通道发来的信息
static void rdp_OnChannelDataReceived(object pChannel, int lAttendeeId, string bstrData)
{
Console.WriteLine("{0}:From RdpViewr Message:{1}",DateTime.Now.ToLongTimeString(),bstrData.Trim());
//throw new NotImplementedException();
}
//这个用来处理viewer端失去连接后的事件
static void rdp_OnAttendeeDisconnected(object pDisconnectInfo)
{
Console.WriteLine("{0}:Remote RdpViewer Closed........",DateTime.Now.ToLongTimeString());
//throw new NotImplementedException();
}
//此事件为必须,否则无法共享桌面
static void rdp_OnAttendeeConnected(object pAttendee)
{
Console.WriteLine("{0}:rdp Viewing........",DateTime.Now.ToLongTimeString());
RDPCOMAPILib.IRDPSRAPIAttendee att = pAttendee as RDPCOMAPILib.IRDPSRAPIAttendee;
//设置viewer的权限,现在设置的为仅浏览
att.ControlLevel = RDPCOMAPILib.CTRL_LEVEL.CTRL_LEVEL_VIEW;
//设置rdp虚拟通道的权限,现在设置为发送和接收
virtualChannel.SetAccess(att.Id,RDPCOMAPILib.CHANNEL_ACCESS_ENUM.CHANNEL_ACCESS_ENUM_SENDRECEIVE);
//throw new NotImplementedException();
}
浏览端
添加引用:
axInterop.RDPCOMPAPILib.dll,动态创建 axrdpviewer
private static AxRDPCOMAPILib.AxRDPViewer axRDPViewer = new AxRDPCOMAPILib.AxRDPViewer();
//必须接收到服务器端发送的连接字符串 rdpConnectString
axRDPViewer.Dock = System.Windows.Forms.DockStyle.Fill;
axRDPViewer.Size = new System.Drawing.Size(frm.Width, frm.Height);
axRDPViewer.OnConnectionTerminated += new AxRDPCOMAPILib._IRDPSessionEvents_OnConnectionTerminatedEventHandler(axRDPViewer_OnConnectionTerminated);
axRDPViewer.OnConnectionEstablished += new EventHandler(axRDPViewer_OnConnectionEstablished);
((System.ComponentModel.ISupportInitialize)axRDPViewer).BeginInit();
frm.Controls.Add(axRDPViewer);
((System.ComponentModel.ISupportInitialize)axRDPViewer).EndInit();
axRDPViewer.SmartSizing = true;
axRDPViewer.Connect(rdpConnectString, System.Guid.NewGuid().ToString(), "");
frm.ShowDialog();
各种事件
void axRDPViewer_OnConnectionEstablished(object sender, EventArgs e)
{
MessageBox.Show("Remote Viewer Viewing!");
//send message through rdp ????
RDPCOMAPILib.IRDPSRAPIVirtualChannel virtualChannel = axRDPViewer.VirtualChannelManager.CreateVirtualChannel("111", RDPCOMAPILib.CHANNEL_PRIORITY.CHANNEL_PRIORITY_HI, (uint)RDPCOMAPILib.CHANNEL_FLAGS.CHANNEL_FLAGS_LEGACY);
virtualChannel.SendData("This is a test TEXT from Viewer!", (int)RDPCOMAPILib.RDPENCOMAPI_CONSTANTS.CONST_ATTENDEE_ID_EVERYONE, (uint)RDPCOMAPILib.CHANNEL_FLAGS.CHANNEL_FLAGS_LEGACY);
//throw new NotImplementedException();
}
private void frm_FormClosing(object sender,FormClosingEventArgs e)
{
//MessageBox.Show("this form will closed,event:frm_closing");
try
{
axRDPViewer.Disconnect();
}
catch (Exception)
{
//throw;
}
System.Windows.Forms.Form f = sender as System.Windows.Forms.Form;
f.Dispose();//动态创建的axRdpViewer,必须要有这个dispose来释放所有资源
}
void axRDPViewer_OnConnectionTerminated(object sender, AxRDPCOMAPILib._IRDPSessionEvents_OnConnectionTerminatedEvent e)
{
System.Windows.Forms.Form f =((AxRDPCOMAPILib.AxRDPViewer)sender).Parent as System.Windows.Forms.Form;
MessageBox.Show("Client Shutdown");
//axRDPViewer.Disconnect();
f.Dispose();
f.Close();
//throw new NotImplementedException();
}