UnhappyNeed help with MemoryReadWrite class C#

Posts 1–15 of 15 · Page 1 of 1
Need help with MemoryReadWrite class C#
Here is my code of memory editor class

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

namespace WindowsFormsApp1
{
    class MemoryReadWrite
    {
        #region NativeMethods
        internal static class NativeMethods
        {

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

            [DllImport("kernel32.dll")]
            public static extern bool ReadProcessMemory(int hProcess, int lpBaseAddress, byte[] lpBuffer, int dwSize, ref int lpNumberOfBytesRead);

            [DllImport("kernel32.dll", SetLastError = true)]
            public static extern bool WriteProcessMemory(int hProcess, int lpBaseAddress, byte[] lpBuffer, int nSize, ref int lpNumberOfBytesWritten);

            [DllImport("kernel32.dll")]
            public static extern bool CloseHandle(IntPtr hObject);
        }
        #endregion

        const int PROCESS_ALL_ACCESS = 0x1F0FFF;


        public Process Process { get; private set; }

        public MemoryReadWrite(Process process)
        {
            Process = process;
        }


        public byte[] ReadMemory(IntPtr address, int size)
        {
            IntPtr processHandle = NativeMethods.OpenProcess(PROCESS_ALL_ACCESS, false, Process.Id);

            var buffer = new byte[size];
            var bytesRead = 0;

            NativeMethods.ReadProcessMemory((int)processHandle, (int)address, buffer, buffer.Length, ref bytesRead);

            NativeMethods.CloseHandle(processHandle);

            return buffer;

        }


        public int ReadInt(IntPtr address)
        {
            var bytes = ReadMemory(address, sizeof(int));

            return BitConverter.ToInt32(bytes, 0);

        }

        public float ReadFloat(IntPtr address)
        {
            var bytes = ReadMemory(address, sizeof(float));

            return BitConverter.ToSingle(bytes, 0);

        }

        public double ReadDouble(IntPtr address)
        {
            var bytes = ReadMemory(address, sizeof(float));

            return BitConverter.ToDouble(bytes, 0);
        }

        public bool ReadBool(IntPtr address)
        {
            var bytes = ReadMemory(address, sizeof(bool));

            return BitConverter.ToBoolean(bytes, 0);
        }

        public string ReadStringASCII(IntPtr address, int strLength)
        {
            var bytes = ReadMemory(address, strLength * sizeof(char));

            return Encoding.ASCII.GetString(bytes);
        }

        public string ReadStringUTF8(IntPtr address, int strLength)
        {
            var bytes = ReadMemory(address, strLength * sizeof(char));

            return Encoding.UTF8.GetString(bytes);
        }

        public bool WriteMemory(IntPtr address, byte[] data)
        {
            IntPtr processHandle = NativeMethods.OpenProcess(PROCESS_ALL_ACCESS, false, Process.Id);

            int bytesWritten = 0;

            return NativeMethods.WriteProcessMemory((int)processHandle, (int)address, data, data.Length, ref bytesWritten);

        }

        public bool WriteMemory(IntPtr address, int value)
        {
            var data = BitConverter.GetBytes(value);

            return WriteMemory(address, data);
        }

        public bool WriteMemory(IntPtr address, float value)
        {
            var data = BitConverter.GetBytes(value);

            return WriteMemory(address, data);
        }

        public bool WriteMemory(IntPtr address, double value)
        {
            var data = BitConverter.GetBytes(value);

            return WriteMemory(address, data);
        }

        public bool WriteMemory(IntPtr address, bool value)
        {
            var data = BitConverter.GetBytes(value);

            return WriteMemory(address, data);
        }

        public bool WriteStringASCII(IntPtr address, string value)
        {
            var data = Encoding.ASCII.GetBytes(value);

            return WriteMemory(address, data);
        }

        public bool WriteStringUTF8(IntPtr address, string value)
        {
            var data = Encoding.UTF8.GetBytes(value);

            return WriteMemory(address, data);
        }

    }

}
All work fine ( in my case it csgo external cheats, simple things, like glow, bunnyhop ... )
The problem is that all works fine just about 30 minutes, and than program get crash - System Out Of Memory exception, but the program takes just about 5-6 mb
With library like VAMemory.dll for exemple same cheat codes work fine without any crash. That means something is wrong with my MemoryEditor class.
Can someone help me ? Maybe I do something wrong. Thank you very much!
https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx

sounds like something is not getting disposed properly.
this memoryclass itself looks okay.
i'd suggest you to use dotMemory to find what's using your memory.
the easiest and fastest tool for this kind of stuff
you can also try using GC.Collect(); function (runs garbage collector - cleans ram)
Quote Originally Posted by Zaczero View Post
https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx

sounds like something is not getting disposed properly.
this memoryclass itself looks okay.
i'd suggest you to use dotMemory to find what's using your memory.
the easiest and fastest tool for this kind of stuff
you can also try using GC.Collect(); function (runs garbage collector - cleans ram)
Thank you for reply.
I tried already GC.Collect ... doesn't help me. Anyway thank you very much!
Quote Originally Posted by samuie View Post
Thank you for reply.
I tried already GC.Collect ... doesn't help me. Anyway thank you very much!
you can also see your memory usage on the diagnostics panel
maybe its not the memory class



(1st one from top)

if you see it still rising it will throw the exception earlier or later
it should be a straight line after few seconds after start
Quote Originally Posted by Zaczero View Post
you can also see your memory usage on the diagnostics panel
maybe its not the memory class



(1st one from top)

if you see it still rising it will throw the exception earlier or later
it should be a straight line after few seconds after start
Thank ... now I am try to make diagnostics of ram usage ... maybe I will understand what can be wrong ...
May I ask you ? If it possible, and maybe you can look at my sources to see what I CANT see and tell me were I did mistake ? I know annoy you with my noob problem, but I looked in my code 1000 times, and I cant understand what I am doing wrong.
Cheat for CSGO work great ... but just 30-40 minutes ...
Quote Originally Posted by samuie View Post
Thank ... now I am try to make diagnostics of ram usage ... maybe I will understand what can be wrong ...
May I ask you ? If it possible, and maybe you can look at my sources to see what I CANT see and tell me were I did mistake ? I know annoy you with my noob problem, but I looked in my code 1000 times, and I cant understand what I am doing wrong.
Cheat for CSGO work great ... but just 30-40 minutes ...
If it's like below 200 lines I can take a look
If more nah
pm me
Quote Originally Posted by Zaczero View Post
If it's like below 200 lines I can take a look
If more nah
pm me
need 50 posts to PM someone. Maybe You can add me on sk_ype ?
***** login: universe-games



I did some snapshots of memory, before and after changes are here

HandleCollector+HandleType +4 +112 +112 14 392 392
HandleCollector+HandleType +4 +112 +112 14 392 392

What is the problem that make program to stop working ?
Quote Originally Posted by Zaczero View Post
If it's like below 200 lines I can take a look
If more nah
pm me
I cant PM and I cant attach files here ...
Quote Originally Posted by samuie View Post
need 50 posts to PM someone. Maybe You can add me on sk_ype ?
***** login: universe-games



I did some snapshots of memory, before and after changes are here

HandleCollector+HandleType +4 +112 +112 14 392 392
HandleCollector+HandleType +4 +112 +112 14 392 392

What is the problem that make program to stop working ?
Can't find you (too many results with the same name).
Also I'd suggest you to remove this download link as soon as possible because it's external download and you will get banned :x
Leave me your email
Quote Originally Posted by Zaczero View Post
Can't find you (too many results with the same name).
Also I'd suggest you to remove this download link as soon as possible because it's external download and you will get banned :x
Leave me your email
Okey .. sorry for the link, already delete it. Here is my email live2hack@yandex.ru
Quote Originally Posted by samuie View Post
Okey .. sorry for the link, already delete it. Here is my email live2hack@yandex.ru
So what I see is you should optimize OpenProcess and CloseHandle methods (they should be opened once on program start [initialization] - you don't have to close it)
I don't really see why would it make the ram usage so high. Maybe u changed something in solution properties?
Quote Originally Posted by Zaczero View Post
So what I see is you should optimize OpenProcess and CloseHandle methods (they should be opened once on program start [initialization] - you don't have to close it)
I don't really see why would it make the ram usage so high. Maybe u changed something in solution properties?
I change my MemoryReadWrite class like that:

Code:
        #region NativeMethods
        internal static class NativeMethods
        {
            [DllImport("kernel32.dll")]
            public static extern bool ReadProcessMemory(int hProcess, int lpBaseAddress, byte[] lpBuffer, int dwSize, ref int lpNumberOfBytesRead);

            [DllImport("kernel32.dll", SetLastError = true)]
            public static extern bool WriteProcessMemory(int hProcess, int lpBaseAddress, byte[] lpBuffer, int nSize, ref int lpNumberOfBytesWritten);
        }
        #endregion
If you will help me , I will drunk all the night ))) because I am tired to search so simple things that i CANT see. Iam just learning , but I am stuck ... 3 days I dont know what to do to make it work normal. Please ... Help me with that ...
Quote Originally Posted by samuie View Post
I change my MemoryReadWrite class like that:

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

namespace WindowsFormsApp1
{
    class MemoryReadWrite
    {
        #region NativeMethods
        internal static class NativeMethods
        {
            [DllImport("kernel32.dll")]
            public static extern bool ReadProcessMemory(int hProcess, int lpBaseAddress, byte[] lpBuffer, int dwSize, ref int lpNumberOfBytesRead);

            [DllImport("kernel32.dll", SetLastError = true)]
            public static extern bool WriteProcessMemory(int hProcess, int lpBaseAddress, byte[] lpBuffer, int nSize, ref int lpNumberOfBytesWritten);
        }
        #endregion

        const int PROCESS_ALL_ACCESS = 0x1F0FFF;


        public Process Process { get; private set; }

        public MemoryReadWrite(Process process)
        {
            Process = process;
        }


        public byte[] ReadMemory(IntPtr address, int size)
        {

            var buffer = new byte[size];
            var bytesRead = 0;

            NativeMethods.ReadProcessMemory((int)Process.Handle, (int)address, buffer, buffer.Length, ref bytesRead);

            return buffer;

        }


        public int ReadInt(IntPtr address)
        {
            var bytes = ReadMemory(address, sizeof(int));

            return BitConverter.ToInt32(bytes, 0);

        }

        public float ReadFloat(IntPtr address)
        {
            var bytes = ReadMemory(address, sizeof(float));

            return BitConverter.ToSingle(bytes, 0);

        }

        public double ReadDouble(IntPtr address)
        {
            var bytes = ReadMemory(address, sizeof(float));

            return BitConverter.ToDouble(bytes, 0);
        }

        public bool ReadBool(IntPtr address)
        {
            var bytes = ReadMemory(address, sizeof(bool));

            return BitConverter.ToBoolean(bytes, 0);
        }

        public string ReadStringASCII(IntPtr address, int strLength)
        {
            var bytes = ReadMemory(address, strLength * sizeof(char));

            return Encoding.ASCII.GetString(bytes);
        }

        public string ReadStringUTF8(IntPtr address, int strLength)
        {
            var bytes = ReadMemory(address, strLength * sizeof(char));

            return Encoding.UTF8.GetString(bytes);
        }

        public bool WriteMemory(IntPtr address, byte[] data)
        {
            int bytesWritten = 0;

            return NativeMethods.WriteProcessMemory((int)Process.Handle, (int)address, data, data.Length, ref bytesWritten);
        }

        public bool WriteMemory(IntPtr address, int value)
        {
            var data = BitConverter.GetBytes(value);

            return WriteMemory(address, data);
        }

        public bool WriteMemory(IntPtr address, float value)
        {
            var data = BitConverter.GetBytes(value);

            return WriteMemory(address, data);
        }

        public bool WriteMemory(IntPtr address, double value)
        {
            var data = BitConverter.GetBytes(value);

            return WriteMemory(address, data);
        }

        public bool WriteMemory(IntPtr address, bool value)
        {
            var data = BitConverter.GetBytes(value);

            return WriteMemory(address, data);
        }

        public bool WriteStringASCII(IntPtr address, string value)
        {
            var data = Encoding.ASCII.GetBytes(value);

            return WriteMemory(address, data);
        }

        public bool WriteStringUTF8(IntPtr address, string value)
        {
            var data = Encoding.UTF8.GetBytes(value);

            return WriteMemory(address, data);
        }

    }

}
I mean i delete from this class OpenProcess
Now how do I Open it in Form load ?

Code:
private void Form1_Load(object sender, EventArgs e)
        {
            try
            {

                Data.memory = new MemoryReadWrite(Process.GetProcessesByName("csgo")[0]);
                Process csgo = Process.GetProcessesByName("csgo")[0];


                foreach (ProcessModule Module in csgo.Modules)
                {
                    if (Module.ModuleName == "client.dll")
                        Data.client = (int)Module.BaseAddress;

                    if (Module.ModuleName == "engine.dll")
                        Data.engine = (int)Module.BaseAddress;
                }

                MessageBox.Show("The program successfully started!" + "\n" + $"Client adress has been found: {Data.client}", "Start", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                glowplayers = new GlowPlayers(Data.memory);
            }

            catch
            {
                MessageBox.Show("The program coud not be started" + "\n" + "Please run CS:GO first !  ~(^_^)~", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Environment.Exit(0);
            }
        }
If you will help me , I will drunk all the night ))) because I am tired to search so simple things that i CANT see. Iam just learning , but I am stuck ... 3 days I dont know what to do to make it work normal. Please ... Help me with that ...
you shold add OpenProcess in:

Code:
        public MemoryReadWrite(Process process)
        {
            Process = process;
            // HERE
        }
ps. check your email
Problem solved! Thank you all !!! Thread can be closed !
//solved //2short//
Posts 1–15 of 15 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Need help?