C#远程连接mstscax组件和upnp组件

           C\windows\mstsccax.dll.组件   axMsRdpClient2NotSafeForScripting1.Server = "10.168.1.254";
            axMsRdpClient2NotSafeForScripting1.AdvancedSettings2.RDPPort = 3389;
            axMsRdpClient2NotSafeForScripting1.UserName = "administrator";

            axMsRdpClient2NotSafeForScripting1.AdvancedSettings2.ClearTextPassword = "hubenadmin";

         //在rdp.AdvancedSettings2下还有很多高级设置,自行摸索吧,            axMsRdpClient2NotSafeForScripting1.Connect();

        

   

            NATUPnP 1.0 Type Library

        

          

var eport = 8733;

var iport = 8733;

var description = "Mgen测试";

 

//创建COM类型

var upnpnat = new UPnPNAT();

var mappings = upnpnat.StaticPortMappingCollection;

 

//错误判断

if (mappings == null)

{

    Console.WriteLine("没有检测到路由器,或者路由器不支持UPnP功能。");

    return;

}

 

//添加之前的ipv4变量(内网IP),内部端口,和外部端口

mappings.Add(eport, "TCP", iport, ipv4.ToString(), true, description);

 

Console.WriteLine("外部端口:{0}", eport);

Console.WriteLine("内部端口:{0}", iport);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
下面是一个简单的C#程序,演示如何使用远程桌面连接服务器: ```csharp using System; using System.Net; using System.Net.Sockets; using System.Runtime.InteropServices; using System.Threading; namespace RemoteDesktopDemo { class Program { // 定义远程桌面连接参数 const int RDP_VERSION = 0x00080004; // RDP版本号 const int RDP_FLAG = 0; // RDP标志 const int RDP_WIDTH = 1024; // 屏幕宽度 const int RDP_HEIGHT = 768; // 屏幕高度 const int RDP_BPP = 16; // 颜色位数 // 定义Win32 API函数 [DllImport("mstscax.dll")] static extern void MstscAxShell(); [DllImport("user32.dll")] static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll")] static extern bool SetForegroundWindow(IntPtr hWnd); [DllImport("user32.dll")] static extern bool SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam); static void Main(string[] args) { // 获取远程桌面服务器的IP地址和端口号 string ip = "127.0.0.1"; int port = 3389; // 创建TCP客户端连接 TcpClient client = new TcpClient(); client.Connect(ip, port); // 获取TCP连接的网络流 NetworkStream stream = client.GetStream(); // 发送远程桌面连接参数 byte[] buffer = new byte[20]; BitConverter.GetBytes(RDP_VERSION).CopyTo(buffer, 0); BitConverter.GetBytes(RDP_FLAG).CopyTo(buffer, 4); BitConverter.GetBytes(RDP_WIDTH).CopyTo(buffer, 8); BitConverter.GetBytes(RDP_HEIGHT).CopyTo(buffer, 12); BitConverter.GetBytes(RDP_BPP).CopyTo(buffer, 16); stream.Write(buffer, 0, buffer.Length); // 启动远程桌面连接 MstscAxShell(); // 等待远程桌面连接窗口出现 Thread.Sleep(1000); // 获取远程桌面连接窗口句柄 IntPtr hWnd = FindWindow("TscShellContainerClass", null); // 激活远程桌面连接窗口 SetForegroundWindow(hWnd); // 发送ENTER键消息,开始连接远程桌面服务器 const uint WM_KEYDOWN = 0x0100; const int VK_RETURN = 0x0D; SendMessage(hWnd, WM_KEYDOWN, VK_RETURN, 0); // 关闭网络流和TCP客户端连接 stream.Close(); client.Close(); } } } ``` 这个程序使用TCP连接到远程桌面服务器,并发送远程桌面连接参数。然后使用Win32 API函数启动远程桌面连接程序,并发送ENTER键消息开始连接远程桌面服务器。最后关闭网络流和TCP客户端连接

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值