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 


C# writelong write byte

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General Discussions
View previous topic :: View next topic  
Author Message
neowrs
Newbie cheater
Reputation: 0

Joined: 07 Oct 2015
Posts: 15

PostPosted: Sat Nov 14, 2015 12:16 pm    Post subject: C# writelong write byte Reply with quote

class ReadWritingMemory

Code:
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;

public class ReadWritingMemory
{

   [DllImport("kernel32.dll")]
   public static extern IntPtr OpenProcess(UInt32 dwDesiredAccess, Int32 bInheritHandle, UInt32 dwProcessId);
   [DllImport("kernel32.dll")]
   public static extern Int32 CloseHandle(IntPtr hObject);
   [DllImport("kernel32.dll")]
   public static extern Int32 ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,    [In(), Out()]
byte[] buffer, UInt32 size, ref IntPtr lpNumberOfBytesRead);
   [DllImport("kernel32.dll")]
   public static extern Int32 WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,    [In(), Out()]
byte[] buffer, UInt32 size, ref IntPtr lpNumberOfBytesWritten);
   [DllImport("kernel32", EntryPoint = "ReadProcessMemory", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
   private static extern int ReadProcessMemory1(int hProcess, int lpBaseAddress, ref int lpBuffer, int nSize, ref int lpNumberOfBytesWritten);
   [DllImport("kernel32", EntryPoint = "ReadProcessMemory", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
   private static extern float ReadProcessMemory2(int hProcess, int lpBaseAddress, ref float lpBuffer, int nSize, ref int lpNumberOfBytesWritten);
   [DllImport("kernel32", EntryPoint = "ReadProcessMemory", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
   private static extern long ReadProcessMemory3(int hProcess, int lpBaseAddress, ref long lpBuffer, int nSize, ref int lpNumberOfBytesWritten);
   [DllImport("kernel32", EntryPoint = "WriteProcessMemory", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
   private static extern int WriteProcessMemory1(int hProcess, int lpBaseAddress, ref int lpBuffer, int nSize, ref int lpNumberOfBytesWritten);
   [DllImport("kernel32", EntryPoint = "WriteProcessMemory", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
   private static extern float WriteProcessMemory2(int hProcess, int lpBaseAddress, ref float lpBuffer, int nSize, ref int lpNumberOfBytesWritten);
   [DllImport("kernel32", EntryPoint = "WriteProcessMemory", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
   private static extern long WriteProcessMemory3(int hProcess, int lpBaseAddress, ref long lpBuffer, int nSize, ref int lpNumberOfBytesWritten);

   private const int PROCESS_ALL_ACCESS = 127231;
   public static float ReadFloat(string ProcessName, int Address, int nsize)
   {
      float num2 = 0;
      float num3 = 0;
      if (ProcessName.EndsWith(".exe")) {
         ProcessName = ProcessName.Replace(".exe", "");
      }
      Process[] processesByName = Process.GetProcessesByName(ProcessName);
      if ((processesByName.Length == 0)) {
         MessageBox.Show((ProcessName + " is not open !"));
         return num2;
      }
      IntPtr ptr = (IntPtr)OpenProcess(127231, 0, processesByName(0).Id);
      if ((ptr == IntPtr.Zero)) {
         MessageBox.Show("Run As Admin");
         return num2;
      }
      int lpBaseAddress = Address;
      int lpNumberOfBytesWritten = 0;
      ReadProcessMemory2(Convert.ToInt32(ptr), lpBaseAddress, ref num3, nsize, ref lpNumberOfBytesWritten);
      return num3;
   }

   public static int ReadInteger(string ProcessName, int Address, int nsize)
   {
      int num2 = 0;
      int num3 = 0;
      if (ProcessName.EndsWith(".exe")) {
         ProcessName = ProcessName.Replace(".exe", "");
      }
      Process[] processesByName = Process.GetProcessesByName(ProcessName);
      if ((processesByName.Length == 0)) {
         MessageBox.Show((ProcessName + " is is not open !"));
         return num2;
      }
      IntPtr ptr = (IntPtr)OpenProcess(127231, 0, processesByName(0).Id);
      if ((ptr == IntPtr.Zero)) {
         MessageBox.Show("Run As Admin");
         return num2;
      }
      int lpBaseAddress = Address;
      int lpNumberOfBytesWritten = 0;
      ReadProcessMemory1(Convert.ToInt32(ptr), lpBaseAddress, ref num3, nsize, ref lpNumberOfBytesWritten);
      return num3;
   }

   public static long ReadLong(string ProcessName, int Address, int nsize)
   {
      long num2 = 0;
      long num3 = 0;
      if (ProcessName.EndsWith(".exe")) {
         ProcessName = ProcessName.Replace(".exe", "");
      }
      Process[] processesByName = Process.GetProcessesByName(ProcessName);
      if ((processesByName.Length == 0)) {
         MessageBox.Show((ProcessName + " is not open !"));
         return num2;
      }
      IntPtr ptr = (IntPtr)OpenProcess(127231, 0, processesByName(0).Id);
      if ((ptr == IntPtr.Zero)) {
         MessageBox.Show("Run As Admin");
         return num2;
      }
      int lpBaseAddress = Address;
      int lpNumberOfBytesWritten = 0;
      ReadProcessMemory3(Convert.ToInt32(ptr), lpBaseAddress, ref num3, nsize, ref lpNumberOfBytesWritten);
      return num3;
   }

   public static void WriteLong(string ProcessName, int Address, long Value, int nsize)
   {
      if (ProcessName.EndsWith(".exe")) {
         ProcessName = ProcessName.Replace(".exe", "");
      }
      Process[] processesByName = Process.GetProcessesByName(ProcessName);
      if ((processesByName.Length == 0)) {
         MessageBox.Show((ProcessName + " is not open !"));
      } else {
         IntPtr ptr = (IntPtr)OpenProcess(127231, 0, processesByName(0).Id);
         if ((ptr == IntPtr.Zero)) {
            MessageBox.Show("Run As Admin");
         } else {
            int lpBaseAddress = Address;
            long lpBuffer = Value;
            int lpNumberOfBytesWritten = 0;
            WriteProcessMemory3(Convert.ToInt32(ptr), lpBaseAddress, ref lpBuffer, nsize, ref lpNumberOfBytesWritten);
         }
      }
   }

   public static void WriteFloat(string ProcessName, int Address, float Value, int nsize)
   {
      if (ProcessName.EndsWith(".exe")) {
         ProcessName = ProcessName.Replace(".exe", "");
      }
      Process[] processesByName = Process.GetProcessesByName(ProcessName);
      if ((processesByName.Length == 0)) {
         MessageBox.Show((ProcessName + " is not open !"));
      } else {
         IntPtr ptr = (IntPtr)OpenProcess(127231, 0, processesByName(0).Id);
         if ((ptr == IntPtr.Zero)) {
            MessageBox.Show("Run As Admin");
         } else {
            int lpBaseAddress = Address;
            float lpBuffer = Value;
            int lpNumberOfBytesWritten = 0;
            WriteProcessMemory2(Convert.ToInt32(ptr), lpBaseAddress, ref lpBuffer, nsize, ref lpNumberOfBytesWritten);
         }
      }
   }

   public static void WriteInteger(string ProcessName, int Address, int Value, int nsize)
   {
      if (ProcessName.EndsWith(".exe")) {
         ProcessName = ProcessName.Replace(".exe", "");
      }
      Process[] processesByName = Process.GetProcessesByName(ProcessName);
      if ((processesByName.Length == 0)) {
         MessageBox.Show((ProcessName + " is not open !"));
      } else {
         IntPtr ptr = (IntPtr)OpenProcess(127231, 0, processesByName(0).Id);
         if ((ptr == IntPtr.Zero)) {
            MessageBox.Show("Run As Admin");
         } else {
            int lpBaseAddress = Address;
            int lpBuffer = Value;
            int lpNumberOfBytesWritten = 0;
            WriteProcessMemory1(Convert.ToInt32(ptr), lpBaseAddress, ref lpBuffer, nsize, ref lpNumberOfBytesWritten);
         }
      }
   }

   public static bool WriteDMAFloat(string Process, int Address, int[] Offsets, float Value, int Level, int nsize = 4)
   {
      bool flag = false;
      try {
         int num = Address;
         int num1 = Level;
         int num2 = 1;
         do {
            num = Convert.ToInt32(Math.Round(Convert.ToDouble((Convert.ToSingle((ReadWritingMemory.ReadFloat(Process, num, nsize) + Convert.ToSingle(Offsets(num2 - 1))))))));
            num2 = num2 + 1;
         } while (num2 <= num1);
         ReadWritingMemory.WriteFloat(Process, num, Value, nsize);
         flag = true;
      } catch (System.Exception ex) {
         Interaction.MsgBox(ex);
         flag = false;
      }
      return flag;
   }

   public static bool WriteDMAInteger(string Process, int Address, int[] Offsets, int Value, int Level, int nsize = 4)
   {
      bool flag = false;
      try {
         int num = Address;
         int num1 = Level;
         int num2 = 1;
         do {
            num = ReadWritingMemory.ReadInteger(Process, num, nsize) + Offsets(num2 - 1);
            num2 = num2 + 1;
         } while (num2 <= num1);
         ReadWritingMemory.WriteInteger(Process, num, Value, nsize);
         flag = true;
      } catch (System.Exception ex) {
         Interaction.MsgBox(ex);
         flag = false;
      }
      return flag;
   }

   public static bool WriteDMALong(string Process, int Address, int[] Offsets, long Value, int Level, int nsize = 4)
   {
      bool flag = false;
      try {
         int num = Address;
         int num1 = Level;
         int num2 = 1;
         do {
            num = Convert.ToInt32((ReadWritingMemory.ReadLong(Process, num, nsize) + Convert.ToInt64(Offsets(num2 - 1))));
            num2 = num2 + 1;
         } while (num2 <= num1);
         ReadWritingMemory.WriteLong(Process, num, Value, nsize);
         flag = true;
      } catch (System.Exception ex) {
         Interaction.MsgBox(ex);
         flag = false;
      }
      return flag;
   }

   public static float ReadDMAFloat(string Process, int Address, int[] Offsets, int Level, int nsize = 4)
   {
      float single = 0f;
      bool flag = false;
      try {
         int num = Address;
         int num1 = Level;
         int num2 = 1;
         do {
            num = Convert.ToInt32(Math.Round(Convert.ToDouble((Convert.ToSingle((ReadWritingMemory.ReadFloat(Process, num, nsize) + Convert.ToSingle(Offsets(num2 - 1))))))));
            num2 = num2 + 1;
         } while (num2 <= num1);
         single = ReadWritingMemory.ReadFloat(Process, num, nsize);
         flag = true;
      } catch (System.Exception ex) {
         Interaction.MsgBox(ex);
      }
      flag = false;
      return single;
   }

   public static int ReadDMAInteger(string Process, int Address, int[] Offsets, int Level, int nsize = 4)
   {
      int num = 0;
      bool flag = false;
      try {
         int num1 = Address;
         int num2 = Level;
         int num3 = 1;
         do {
            num1 = ReadWritingMemory.ReadInteger(Process, num1, nsize) + Offsets(num3 - 1);
            num3 = num3 + 1;
         } while (num3 <= num2);
         num = ReadWritingMemory.ReadInteger(Process, num1, nsize);
         flag = true;
      } catch (System.Exception ex) {
         Interaction.MsgBox(ex);
      }
      flag = false;
      return num;
   }

   public static long ReadDMALong(string Process, int Address, int[] Offsets, int Level, int nsize = 4)
   {
      long num = 0L;
      bool flag = false;
      try {
         int num1 = Address;
         int num2 = Level;
         int num3 = 1;
         do {
            num1 = Convert.ToInt32((ReadWritingMemory.ReadLong(Process, num1, nsize) + Convert.ToInt64(Offsets(num3 - 1))));
            num3 = num3 + 1;
         } while (num3 <= num2);
         num = ReadWritingMemory.ReadLong(Process, num1, nsize);
         flag = true;
      } catch (System.Exception ex) {
         Interaction.MsgBox(ex);
      }
      flag = false;
      return num;
   }

   public static void WriteXBytes(string ProcessName, long Address, string Value)
   {
      if (ProcessName.EndsWith(".exe")) {
         ProcessName = ProcessName.Replace(".exe", "");
      }
      Process[] processesByName = Process.GetProcessesByName(ProcessName);
      if ((Convert.ToInt32(processesByName.Length) != 0)) {
         IntPtr intPtr = (IntPtr)ReadWritingMemory.OpenProcess(127231, 0, processesByName(0).Id);
         if ((intPtr != intPtr.Zero)) {
            int num = 0;
            int num1 = 1;
            int num2 = Convert.ToInt32(Math.Round(Math.Round((Convert.ToDouble(Strings.Len(Value)) / 2))));
            int i = 1;
            while ((i <= num2)) {
               byte num3 = Convert.ToByte(Math.Round(Conversion.Val(string.Concat("0x", Strings.Mid(Value, num1, 2)))));
               int num4 = num3;
               int num5 = 0;
               ReadWritingMemory.WriteProcessMemory1(Convert.ToInt32(intPtr), Convert.ToInt32((Address + Convert.ToInt64(num))), ref num4, 1, ref num5);
               num3 = Convert.ToByte(num4);
               num = (num + 1);
               num1 = (num1 + 2);
               i = (i + 1);
            }
         } else {
            MessageBox.Show(string.Concat("Run As Admin"));
         }
      } else {
         MessageBox.Show(string.Concat(ProcessName, " is not open !"));
      }
   }

}



Code:

if checkbox1.checked = true
{
 ReadWritingMemory.WriteLong("Client", 0x5987CC, 5033894382085862156, 8);
}

Game always crash
but if i use it like that
if checkbox1.checked = true
{
ReadWritingMemory.WriteLong("Client", 0x24EBF0, 2344895961, 4);
}
it working


Last edited by neowrs on Sat Nov 14, 2015 1:32 pm; edited 1 time in total
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Sat Nov 14, 2015 1:02 pm    Post subject: Reply with quote

If you are going to try to get help with your code, at least put effort into your post.. You give no context on anything, no code aside from the line crashing you (which shows you probably have no idea what you are doing) and you give no information as to what you are writing to.
_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
neowrs
Newbie cheater
Reputation: 0

Joined: 07 Oct 2015
Posts: 15

PostPosted: Sat Nov 14, 2015 1:34 pm    Post subject: Reply with quote

atom0s wrote:
If you are going to try to get help with your code, at least put effort into your post.. You give no context on anything, no code aside from the line crashing you (which shows you probably have no idea what you are doing) and you give no information as to what you are writing to.


ok sorry the problem is if i use value 4 bytes it work but 8 bytes not work why i need to use 8 byte because the value hacked = normal value in 4 bytes
Back to top
View user's profile Send private message
Zanzer
I post too much
Reputation: 126

Joined: 09 Jun 2013
Posts: 3278

PostPosted: Sat Nov 14, 2015 5:39 pm    Post subject: Reply with quote

When writing a long constant, you need to add an "L" after the number.
Code:
ReadWritingMemory.WriteLong("Client", 0x5987CC, 5033894382085862156L, 8);
Back to top
View user's profile Send private message
neowrs
Newbie cheater
Reputation: 0

Joined: 07 Oct 2015
Posts: 15

PostPosted: Sat Nov 14, 2015 7:02 pm    Post subject: Reply with quote

Zanzer wrote:
When writing a long constant, you need to add an "L" after the number.
Code:
ReadWritingMemory.WriteLong("Client", 0x5987CC, 5033894382085862156L, 8);

Thanks
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 Discussions 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