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