Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Help me understand this C# code

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
lolAnonymous
Expert Cheater
Reputation: 1

Joined: 19 Jul 2015
Posts: 154

PostPosted: Wed Sep 07, 2016 9:32 pm    Post subject: Help me understand this C# code Reply with quote

Hello guys , I am back with my C# code taken from the C# code I bought for 25$

If u don't know what I am talking about :p
http://forum.cheatengine.org/viewtopic.php?t=593770

small particles suggested me to learn C# first and I appreciated his suggestion and Now its going to complete 1 month learning C# .... I am back because I think I learn't the basics...

Code:
private int i;

        //ReadProcessMemory
        [DllImport("kernel32.dll", SetLastError = true)]
        static extern bool ReadProcessMemory(
                 IntPtr hProcess,
                 IntPtr lpBaseAddress,
                 [Out] byte[] lpBuffer,
                 int dwSize,
                 out int lpNumberOfBytesRead
                );
        //OpenProcess // Flag = All = 0x001F0FFF
        [DllImport("kernel32.dll")]
        public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);

        // CloseHandle
        [DllImport("kernel32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool CloseHandle(IntPtr hObject);
        [StructLayout(LayoutKind.Sequential)]
        public struct MEMORY_BASIC_INFORMATION
        {
            public IntPtr BaseAddress;
            public IntPtr AllocationBase;
            public uint AllocationProtect;
            public IntPtr RegionSize;
            public uint State;
            public uint Protect;
            public uint Type;
        }
        // VirtualQueryEx
        [DllImport("kernel32.dll")]
        static extern int VirtualQueryEx(IntPtr hProcess, IntPtr lpAddress, out MEMORY_BASIC_INFORMATION lpBuffer, uint dwLength);
        //WriteProcessMemory
        [DllImport("kernel32.dll", SetLastError = true)]
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, uint nSize, out UIntPtr lpNumberOfBytesWritten);

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, IntPtr lpBuffer, uint nSize, UIntPtr lpNumberOfBytesWritten);




        private void WriteProcessMemory(IntPtr handle, IntPtr addr, object bytes_write, uint length, out UIntPtr dammy)
        {
            throw new NotImplementedException();
        }

        int GetProcessID(string name)
        {
            foreach (Process proc in Process.GetProcessesByName(name))
                return proc.Id;


            return -1;
        }
        int SignScan(byte[] bytes_scan, IntPtr handle)
        {
            MEMORY_BASIC_INFORMATION m;

            m.BaseAddress = IntPtr.Zero;
            m.RegionSize = IntPtr.Zero;

            long MaxAddress = 0x7ffffffffffffff;
            long address = 0;
            int count = 0;
            while (address <= MaxAddress)
            {

                VirtualQueryEx(handle, (IntPtr)address, out m, (uint)Marshal.SizeOf(typeof(MEMORY_BASIC_INFORMATION)));

                if (address == (long)m.BaseAddress + (long)m.RegionSize)
                    break;
                address = (long)m.BaseAddress + (long)m.RegionSize;
                byte[] buffer = new byte[(uint)m.RegionSize];
                int dammy = 0;
                ReadProcessMemory(handle, (IntPtr)m.BaseAddress, buffer, (int)m.RegionSize, out dammy);

                for (i = 0; i < (int)m.RegionSize; i++)
                {
                    if (buffer[i] == bytes_scan[0])
                    {
                        for (int j = 0; j < bytes_scan.Length; j++)
                        {
                            if (buffer[j + i] == bytes_scan[j])
                            {
                                count++;
                                if (count == bytes_scan.Length)
                                {
                                    return (int)m.BaseAddress + i;
                                }
                            }
                            else
                            {
                                count = 0;
                            }
                        }
                    }
                }
            }




            return -1;
        }

        void WriteBytes(int ID, byte[] bytes_Scan, byte[] bytes_write, IntPtr handle)
        {

            int addr = SignScan(bytes_Scan, handle);
            var dammy = new UIntPtr();
            if (addr == -1)
            {

                return;
            }

            WriteProcessMemory(handle, (IntPtr)addr, bytes_write, (uint)bytes_write.Length, out dammy);



        }



Guys I know these things in C# : Looping , If\else , switch case (we are on this topic) , Array (Simple and Rectangular only) etc etc.... We just made Simple Calculator , Mark sheet , Table ... But I am still confuse in understanding these things... I think its because I don't know Fuctions in C# but I know them in C-lang ... "We are working with console application :/ "



So help me understanding each code. not asking u to feed me things but just if u can explain like what region size is and all that...


Thanks in Advance Smile
Back to top
View user's profile Send private message
cooleko
Grandmaster Cheater
Reputation: 11

Joined: 04 May 2016
Posts: 717

PostPosted: Thu Sep 08, 2016 10:10 am    Post subject: Re: Help me understand this C# code Reply with quote

We could go line for line through that code and tell you what each line does and why, but this would be a vastly disproportionate amount of effort on our part.

I'll do it for you if you do it first. Simply take that code you pasted and add comments above each line telling us what you think it does and why. We will then come through and tell you what it does and why and help clarify where your thoughts deviated.
Back to top
View user's profile Send private message
lolAnonymous
Expert Cheater
Reputation: 1

Joined: 19 Jul 2015
Posts: 154

PostPosted: Thu Sep 08, 2016 3:38 pm    Post subject: Reply with quote

cooleko thanks for a great idea Smile

well if u don't mind I want some time because I think you will have a headache by answering my each question because they are not 100 they are 1000 so I think I should complete my C# course first then I will review this source code and I will ask questions which are necessary...

Sorry If I wasted your time Sad but I will be back soon after completing my C# course and message you with my problems...


Thanks Smile

Not necessary :-

If u want to help me a little I have a question the source code I have only scan not writable aobs... It works fine if the given aob is not writable else it gives an error... I want to scan writable aobs also but it gives error with it...


Last edited by lolAnonymous on Thu Sep 08, 2016 5:14 pm; edited 1 time in total
Back to top
View user's profile Send private message
Zanzer
I post too much
Reputation: 126

Joined: 09 Jun 2013
Posts: 3278

PostPosted: Thu Sep 08, 2016 4:31 pm    Post subject: Reply with quote

You passing 0x001F0FFF as dwDesiredAccess to OpenProcess?
Back to top
View user's profile Send private message
lolAnonymous
Expert Cheater
Reputation: 1

Joined: 19 Jul 2015
Posts: 154

PostPosted: Thu Sep 08, 2016 5:13 pm    Post subject: Reply with quote

Zanzer its funny but I really don't know... I took this source code from a friend... well Zanzer do u know the problem why its not scanning writable aobs... ?
Back to top
View user's profile Send private message
lolAnonymous
Expert Cheater
Reputation: 1

Joined: 19 Jul 2015
Posts: 154

PostPosted: Thu Sep 15, 2016 10:32 pm    Post subject: Reply with quote

Hey friends I am back and Now I understand 70% of the Trainer Source Smile


So I am back with questions :-

Code:
private int SignScan(byte[] bytes_scan, IntPtr handle)
        {
            Form1.MEMORY_BASIC_INFORMATION mEMORY_BASIC_INFORMATION;
            mEMORY_BASIC_INFORMATION.BaseAddress = IntPtr.Zero;
            mEMORY_BASIC_INFORMATION.RegionSize = IntPtr.Zero;
            long num = 576460752303423487L; 
            long num2 = 0L;
            int num3 = 0;
            int result;
            while (num2 <= num)
            {
                Form1.VirtualQueryEx(handle, (IntPtr)num2, out mEMORY_BASIC_INFORMATION, (uint)Marshal.SizeOf(typeof(Form1.MEMORY_BASIC_INFORMATION))); // I can't understand few thing in this!

                if (num2 == (long)mEMORY_BASIC_INFORMATION.BaseAddress + (long)mEMORY_BASIC_INFORMATION.RegionSize)
                {
                    break;
                }
                num2 = (long)mEMORY_BASIC_INFORMATION.BaseAddress + (long)mEMORY_BASIC_INFORMATION.RegionSize;
                byte[] array = new byte[(int)mEMORY_BASIC_INFORMATION.RegionSize];
                int num4 = 0;
                Form1.ReadProcessMemory(handle, mEMORY_BASIC_INFORMATION.BaseAddress, array, (int)mEMORY_BASIC_INFORMATION.RegionSize, out num4);
                s = 0;
                while (s < (int)mEMORY_BASIC_INFORMATION.RegionSize)  // here s is not less than Region size right? then how while loop will work?
                {
                   
                    if (array[s] == bytes_scan[0])
                    {
                        for (int i = 0; i < bytes_scan.Length; i++)
                        {
                         
                            if (array[i + s] == bytes_scan[i])  // what is the value of i and s here?
                            {
                                num3++;
                               
                                if (num3 == bytes_scan.Length)
                                {
                                    result = (int)mEMORY_BASIC_INFORMATION.BaseAddress + s;  // s = ?
                                    return result;
                                }
                            }
                            else
                            {
                                num3 = 0;
                            }
                        }
                    }
                    s++;
                }
            }
            result = -1;
            return result;
        }





The main Problem is it does not scan writable Aobs

Thanks In Advance Smile
Back to top
View user's profile Send private message
hollow87
Cheater
Reputation: 0

Joined: 07 Feb 2015
Posts: 28

PostPosted: Wed Sep 21, 2016 4:58 pm    Post subject: Reply with quote

Quote:
Form1.VirtualQueryEx(handle, (IntPtr)num2, out mEMORY_BASIC_INFORMATION, (uint)Marshal.SizeOf(typeof(Form1.MEMORY_BASIC_INFORMATION))); // I can't understand few thing in this!

P/Invoke Interop method for the win32 API function [url=https://msdn.microsoft.com/en-us/library/windows/desktop/aa366907(v=vs.85).aspx]VirtualQueryEx[/url] would be my best guess.

Quote:
while (s < (int)mEMORY_BASIC_INFORMATION.RegionSize) // here s is not less than Region size right? then how while loop will work?

Look quite literally 1 line above that one.

Quote:
if (array[i + s] == bytes_scan[i]) // what is the value of i and s here?

Look at the loops that this line is in for value of i and s

Quote:
result = (int)mEMORY_BASIC_INFORMATION.BaseAddress + s; // s = ?

Again look at the loops that this line is in.
Back to top
View user's profile Send private message
lolAnonymous
Expert Cheater
Reputation: 1

Joined: 19 Jul 2015
Posts: 154

PostPosted: Fri Sep 23, 2016 12:24 am    Post subject: Reply with quote

Finally someone replied :p


Thanks for replying hallow87

Now I understand these things and everything look fine but still I can't understand why it is not scanning writable Aobs Shocked


d0 30 d0 49 00 d1 5e e8 01 2b -- Writable Aobs which are taken from swf file (AS3 flash file)

01 42 24 51 89 41 04 8B 45 10 -- These Aobs are taken from CE , they are not writable Aobs

----------------------------------------------------------------------

For Not Writable Aobs : i.imgur.com/FPXcvHE.png

For Writable Aobs : http://i.imgur.com/apbIEjq.png

Thanks In Advance Very Happy
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites