|
- private string AdbPath { get; set; }
- private string DevicesInfo { get; set; }
- private int MousePort { get; set; }
- private int TxtPort { get; set; }
- private int Pid { get; set; }
- /// <summary>
- /// 模拟器对应的虚拟机ID
- /// </summary>
- public int NoxVmHandlePid { get; set; }
- /// <summary>
- ///
- /// </summary>
- public IntPtr MainWindowHandle { get; set; }
- /// <summary>
- /// 初始化构造函数要求夜神模拟器版本 2.5.0.0
- /// </summary>
- /// <param name="path">模拟器路径 列:“H:\夜神模拟器\Nox\bin\”</param>
- /// <param name="name">要启动的模拟器名称 列“Nox_1”</param>
- public AdbOperation(string path,string name)
- {
- AdbPath = path + "nox_adb.exe";
- Process process = new Process();
- ProcessStartInfo startInfo = new ProcessStartInfo
- {
- FileName = path+"Nox",
- Arguments = "-clone:"+name,
- };
- process.StartInfo = startInfo;
- process.Start();
- Pid = process.Id;
- Thread thread = new Thread(() =>
- {
- int port = 0;
- while (true)
- {
- Thread.Sleep(2000);
- if (port == 0)
- {
- port = MemoryReadWrite.ReadInt(process.Handle, (IntPtr) 0x00685240);
- }
- else
- {
- if (port > 0)
- {
- if (port != MemoryReadWrite.ReadInt(process.Handle, (IntPtr) 0x00685240))
- {
- DevicesInfo = string.Format("127.0.0.1:{0}",
- MemoryReadWrite.ReadInt(process.Handle, (IntPtr) 0x00685240).ToString());//设备端口
- MousePort = MemoryReadWrite.ReadInt(process.Handle, (IntPtr)0x00685258);//鼠标端口
- TxtPort = MemoryReadWrite.ReadInt(process.Handle, (IntPtr) 0x00685244);//文本端口
- NoxVmHandlePid = GetNoxVmHandlePid(name);
- MainWindowHandle = process.MainWindowHandle;
- break;
- }
- }
- }
- }
- }) {IsBackground = true};
- thread.Start();
-
- }
复制代码- /// <summary>
- /// 修改内存页面属性
- /// </summary>
- /// <param name="processHandle"></param>
- /// <param name="pvAddressRemote"></param>
- /// <returns></returns>
- private static void VirtualProtectEx(IntPtr processHandle, IntPtr pvAddressRemote)
- {
- int i = 0;
- Miscellaneous.VirtualProtectEx(processHandle, pvAddressRemote, 2, 4, ref i);
- }
- /// <summary>
- /// 读内存4字节整数型
- /// </summary>
- /// <param name="processHandle">进程句柄 IntPtr.Zero 表示本进程</param>
- /// <param name="pvAddressRemote">要读取的内存地址</param>
- /// <returns></returns>
- public static int ReadInt(IntPtr processHandle, IntPtr pvAddressRemote)
- {
- try
- {
- if (processHandle == IntPtr.Zero)
- {
- VirtualProtectEx(Miscellaneous.GetCurrentProcess(), pvAddressRemote);
- return Marshal.ReadInt32(pvAddressRemote);
- }
- else
- {
- byte[] vBuffer = new byte[4];
- IntPtr vBytesAddress = Marshal.UnsafeAddrOfPinnedArrayElement(vBuffer, 0);
- VirtualProtectEx(processHandle, pvAddressRemote);
- Miscellaneous.ReadProcessMemoryInt(processHandle, pvAddressRemote, vBytesAddress, 4, IntPtr.Zero);
- return Marshal.ReadInt32(vBytesAddress);
- }
- }
- catch
- {
- return 0;
- }
- }
复制代码- /// <修改内存页面属性>
- ///
- /// </修改内存页面属性>
- /// <param name="hWnd"></param>
- /// <param name="lpAddress"></param>
- /// <param name="dwSize"></param>
- /// <param name="flNewProtect"></param>
- /// <param name="lpf噜阿噜dProtect"></param>
- /// <returns></returns>
- [DllImport("kernel32.dll")]
- public static extern bool VirtualProtectEx(IntPtr hWnd, IntPtr lpAddress, Int32 dwSize, Int32 flNewProtect, ref Int32 lpf噜阿噜dProtect);
复制代码- /// <读内存四字节>
- ///
- /// </读内存四字节>
- /// <param name="hProcess"></param>
- /// <param name="lpBaseAddress"></param>
- /// <param name="lpBuffer"></param>
- /// <param name="nSize"></param>
- /// <param name="lpNumberOfBytesRead"></param>
- /// <returns></returns>
- [DllImport("kernel32.dll", EntryPoint = "ReadProcessMemory")]
- public static extern bool ReadProcessMemoryInt(IntPtr hProcess, IntPtr lpBaseAddress, IntPtr lpBuffer, uint nSize, IntPtr lpNumberOfBytesRead);
复制代码- /// <summary>
- /// 输入文本字符串
- /// </summary>
- /// <param name="text">要输入的字符串</param>
- public void InputText(string text)
- {
- try
- {
- var socketSend = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- IPAddress ip = IPAddress.Parse("127.0.0.1");
- IPEndPoint point = new IPEndPoint(ip, TxtPort);
- socketSend.Connect(point);
- byte[] sendData = Encoding.UTF8.GetBytes(text.Trim());
- socketSend.Send(sendData);
- socketSend.Close();
- }
- catch (Exception)
- {
- MessageBox.Show("TCP连接或者发送失败");
- }
- }
- ///<summary>
- /// 模拟鼠标点击 屏幕分辨率要求="280 450 120"
- /// </summary>
- /// <param name="x">模拟器屏幕左上角0开始</param>
- /// <param name="y">模拟器屏幕左上角0开始</param>
- public void MouseClick(int x,int y)
- {
- SendUdpData(MousePort, string.Format("MULTI:1:0:{0}:{1}.", x.ToString(), y.ToString()));
- SendUdpData(MousePort, string.Format("MULTI:1:1:{0}:{1}.", x.ToString(), y.ToString()));
- // Execute(AdbPath + " -s " + DevicesInfo + " shell input tap" + " " + x.ToString()+" "+y.ToString(), 0);
- }
- /// <summary>
- /// 屏幕滑动
- /// </summary>
- /// <param name="startX">起始x</param>
- /// <param name="startY">起始y</param>
- /// <param name="purposeX">目标x</param>
- /// <param name="purposeY">目标y</param>
- public void Sliding(int startX, int startY, int purposeX, int purposeY)
- {
- SendUdpData(MousePort, string.Format("MULTI:1:0:{0}:{1}", startX.ToString(), startY.ToString()));
- SendUdpData(MousePort, string.Format("MULTI:1:2:{0}:{1}", purposeX.ToString(), purposeY.ToString()));
- SendUdpData(MousePort, string.Format("MULTI:1:1:{0}:{1}", purposeX.ToString(), purposeY.ToString()));
- }
- /// <summary>
- /// 发送UDP数据
- /// </summary>
- /// <param name="port">端口</param>
- /// <param name="sendData">要发送的内容</param>
- public static void SendUdpData(int port, string sendData)
- {
- var client = new UdpClient();
- var remotePoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port);
- byte[] sendUdpData = Encoding.UTF8.GetBytes(sendData);
- client.Send(sendUdpData, sendUdpData.Length, remotePoint);
- }
复制代码 以前写的代码有点乱将就看吧 这几个端口的内存地址 针对版本的 其他版本可以 自己CE搜索端口就有地址了 最重要的还是 TCP和UDP数据的发送哪里
|
|