c# 外挂操作(内存操作)(内存读写取窗口句柄移动窗口取模块地址)工具类


来源于网上  参考 https://www.cnblogs.com/fuhua/p/5877781.html 等众多文章

详情取看我第二个例子封装功能较多 https://www.cnblogs.com/xuexidememeda/p/10392528.html

内容包括

取窗口句柄

移动窗口

读写内存整数型

读写内存小数型

写写内存字节集

取空白字节集

取进程句柄

字节集转小数型

根据名称取进程PID

获取模块地址

取窗口句柄

取窗口位置返回RECT

画方框

画直线

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace GDI1.Util
{
    class GameUtil
    {

        [DllImport("kernel32.dll", EntryPoint = "ReadProcessMemory")]
        public static extern int _MemoryReadByteSet(int hProcess, int lpBaseAddress, byte[] lpBuffer, int nSize, int lpNumberOfBytesRead);

        [DllImport("kernel32.dll", EntryPoint = "ReadProcessMemory")]
        public static extern int _MemoryReadInt32(int hProcess, int lpBaseAddress, ref int lpBuffer, int nSize, int lpNumberOfBytesRead);

        [DllImport("kernel32.dll", EntryPoint = "WriteProcessMemory")]
        public static extern int _MemoryWriteByteSet(int hProcess, int lpBaseAddress, byte[] lpBuffer, int nSize, int lpNumberOfBytesWritten);

        [DllImport("kernel32.dll", EntryPoint = "WriteProcessMemory")]
        public static extern int _MemoryWriteInt32(int hProcess, int lpBaseAddress, ref int lpBuffer, int nSize, int lpNumberOfBytesWritten);

        [DllImport("kernel32.dll", EntryPoint = "GetCurrentProcess")]
        public static extern int GetCurrentProcess();

        [DllImport("kernel32.dll", EntryPoint = "OpenProcess")]
        public static extern int OpenProcess(int dwDesiredAccess, int bInheritHandle, int dwProcessId);

        [DllImport("kernel32.dll", EntryPoint = "CloseHandle")]
        public static extern int CloseHandle(int hObject);

        [DllImport("kernel32.dll", EntryPoint = "RtlMoveMemory")]
        public static extern int _CopyMemory_ByteSet_Float(ref float item, ref byte source, int length);
        const int PROCESS_POWER_MAX = 2035711;

        [DllImport("kernel32.dll ")]
        public static extern bool ReadProcessMemory(int hProcess, int lpBaseAddress, float[,] lpBuffer, int nSize, out int lpNumberOfBytesRead);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);

        [StructLayout(LayoutKind.Sequential)]
        public  struct RECT
        {
            public int Left; //最左坐标
            public int Top; //最上坐标
            public int Right; //最右坐标
            public int Bottom; //最下坐标
        }


        //打开进程  
        //kernel32.dll系统动态链接库  
        [DllImportAttribute("kernel32.dll", EntryPoint = "OpenProcess")]
        public static extern IntPtr OpenProcess
        (
            int iAccess,
            bool Handle,
            int ProcessID
        );
        //关闭句柄  
        [DllImport("kernel32.dll", EntryPoint = "CloseHandle")]
        private static extern void CloseHandle
        (
            IntPtr hObject
        );
        //取窗口句柄 FindWindow
        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        public extern static IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("User32.dll", EntryPoint = "FindWindowEx")]
        public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName);
        //移动窗口
        /// <summary>
        /// 设置目标窗体大小,位置
        /// </summary>
        /// <param name="hWnd">目标句柄</param>
        /// <param name="x">目标窗体新位置X轴坐标</param>
        /// <param name="y">目标窗体新位置Y轴坐标</param>
        /// <param name="nWidth">目标窗体新宽度</param>
        /// <param name="nHeight">目标窗体新高度</param>
        /// <param name="BRePaint">是否刷新窗体</param>
        /// <returns></returns> 
        /// 移动窗口
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int MoveWindow(IntPtr hWnd, int x, int y, int nWidth, int nHeight, bool BRePaint);
        //---------------------------------------------------------------------------------------------------------------



        /// <summary>
        /// 读内存整数型
        /// </summary>
        /// <param name="pID">进程ID</param>
        /// <param name="bAddress">0x地址</param>
        /// <returns>0失败</returns>
        public static int ReadMemoryInt32(int pID, int bAddress)
        {
            int num = 0;
            int handle = getProcessHandle(pID);
            int num3 = GameUtil._MemoryReadInt32(handle, bAddress, ref num, 4, 0);
            GameUtil.CloseHandle(handle);
            if (num3 == 0)
            {
                return 0;
            }
            else
            {
                return num;
            }
        }

        /// <summary>
        /// 写内存整数型
        /// </summary>
        /// <param name="pID">进程ID</param>
        /// <param name="bAddress">0x地址</param>
        /// <param name="value">写入值</param>
        /// <returns>false失败 true成功</returns>
        public static bool WriteMemoryInt32(int pID, int bAddress, int value)
        {
            int handle = getProcessHandle(pID);
            int num2 = GameUtil._MemoryWriteInt32(handle, bAddress, ref value, 4, 0);
            GameUtil.CloseHandle(handle);
            return num2 != 0;
        }

        /// <summary>
        /// 读内存小数型
        /// </summary>
        /// <param name="pID">进程ID</param>
        /// <param name="bAddress">0x地址</param>
        /// <returns>0失败</returns>
        public static float ReadMemoryFloat(int pID, int bAddress)
        {
            //byte[] array = test.GetVoidByteSet(4);
            byte[] array = new byte[4];//不取空字节集也可以正确转换成单精度小数型
            int handle = getProcessHandle(pID);
            int temp = GameUtil._MemoryReadByteSet(handle, bAddress, array, 4, 0);
            if (temp == 0)
            {
                return 0f;
            }
            else
            {
                return GameUtil.getFloatFromByteSet(array, 0);
            }
        }

        /// <summary>
        /// 写内存小数型
        /// </summary>
        /// <param name="pID">进程ID</param>
        /// <param name="bAddress">0x地址</param>
        /// <param name="value">写入数据</param>
        /// <returns>false失败</returns>
        public static bool WriteMemoryFloat(int pID, int bAddress, float value)
        {
            //byte[] byteSet = test.GetByteSet(value);
            byte[] byteSet = BitConverter.GetBytes(value);//https://msdn.microsoft.com/en-us/library/yhwsaf3w
                                                          //byte[] byteSet = Encoding.GetEncoding("gb2312").GetBytes(value.ToString());
            return GameUtil.WriteMemoryByteSet(pID, bAddress, byteSet, 0);
        }

        /// <summary>
        /// 写内存字节集
        /// </summary>
        /// <param name="pID">进程ID</param>
        /// <param name="bAddress">0x地址</param>
        /// <param name="value">字节数据</param>
        /// <param name="length">写入长度 0代表字节数据的长度</param>
        /// <returns>false失败</returns>
        private static bool WriteMemoryByteSet(int pID, int bAddress, byte[] value, int length = 0)
        {
            int handle = GameUtil.getProcessHandle(pID);
            int nSize = (length == 0) ? value.Length : length;
            int tmp = GameUtil._MemoryWriteByteSet(handle, bAddress, value, nSize, 0);//byte[]属于引用类型 引用类型不用ref也是以传址方式进行运算
                                                                                  //test.CloseHandle(pID);
            return tmp != 0;
        }

        /// <summary>
        /// 取空白字节集
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static byte[] getVoidByteSet(int num)
        {
            if (num <= 0)
            {
                num = 1;
            }
            string text = "";
            for (int i = 0; i < num; i++)
            {
                text += "0";
            }
            return Encoding.UTF8.GetBytes(text);
        }

        /// <summary>
        /// 取进程句柄
        /// </summary>
        /// <param name="pID">进程ID</param>
        /// <returns>进程句柄</returns>
        public static int getProcessHandle(int pID)
        {
            if (pID == -1)
            {
                return GameUtil.GetCurrentProcess();
            }
            else
            {
                return GameUtil.OpenProcess(PROCESS_POWER_MAX, 0, pID);
            }
        }

        /// <summary>
        /// 字节集转小数型
        /// </summary>
        /// <param name="sourceValue">字节集</param>
        /// <param name="index">索引</param>
        /// <returns></returns>
        public static float getFloatFromByteSet(byte[] sourceValue, int index)
        {
            float result = 0f;
            GameUtil._CopyMemory_ByteSet_Float(ref result, ref sourceValue[index], 4);
            return result;
        }

        /// <summary>
        /// 获取字节集
        /// </summary>
        /// <param name="data">需要转换到字节集的数据</param>
        /// <returns></returns>
        public static byte[] getByteSet(float data)
        {
            return Encoding.UTF8.GetBytes(data.ToString());
        }

        //根据名称取进程PID 不得有后缀.exe
        //根据进程名获取PID  不得有后缀.exe
        public static int getPidByProcessName(string processName)
        {
            Process[] ArrayProcess = Process.GetProcessesByName(processName);
            foreach (Process pro in ArrayProcess)
            {
                return pro.Id;
            }
            return 0;
        }
        //获取模块地址  可以是dll或者exe程序
        public static int getMoudleAddress(string processName ,string dllname )
        {
            foreach (Process p in Process.GetProcessesByName(processName))
            {

                foreach (ProcessModule m in p.Modules)
                {
                    if (m.ModuleName.Equals(dllname))
                        return (int)m.BaseAddress;
                }

            }
            return 0;
        }
        //取窗口句柄 这里传入的值是窗口标题 和窗口类名
        public static IntPtr getWindowsHandler_findwindow(String classname,String windowtitle)
        {
            IntPtr maindHwnd = FindWindow(classname, windowtitle); //获得QQ登陆框的句柄  
           
                return maindHwnd;

        }
        //取窗口位置返回RECT
        public static RECT getWindowsRect(IntPtr windowsHandler)
        {
            RECT rec = new RECT();
            GetWindowRect(windowsHandler, ref rec);//h为窗口句柄


            return rec;
        }

        //画方框
        public static void drawRectangle(Graphics g,int x,int y,int width,int heigth ,Color color,float px)
        {
            Pen pen = new Pen(color, px);
            g.DrawRectangle(pen, x, y, width, heigth);
           

        }
        //画直线
        public static void drawLine(Graphics g, int x, int y, int x1, int y1, Color color, float px)
        {
            Pen pen = new Pen(color, px);
           
            g.DrawLine(pen, new Point(x, y), new Point(x1, y1));

        }


    }
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM