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#] Writing value in to a memory address

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Dami
Master Cheater
Reputation: 0

Joined: 23 Oct 2006
Posts: 336

PostPosted: Sun Oct 12, 2008 2:14 pm    Post subject: [C#] Writing value in to a memory address Reply with quote

Hmm last time i had to read, this time i have to write...
1st i wasnt able to write a value at all, but then i found out i have to send it in bytes :O

Code:
                IntPtr muffin = (IntPtr)0x00803570;

                int Random = 10;
                byte ahmg = Convert.ToByte(Random);

                byte[] roflcakes = { ahmg, 0x0 };
                pKernel.WriteProcessMemory(muffin, roflcakes);


This would write value 16 to the specific address, but because i want to write directly from another memoryadress to this (mousefly)
This is just freaking clumsy!

How can i write directly a normal value like 4000 without turning it to a bytes?
Other wrappers or what ever they are...

Code:
            [DllImport("kernel32.dll")]
            public static extern Int32 WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [In, Out] byte[] buffer, UInt32 size, out IntPtr lpNumberOfBytesWritten);

~~~~~~~~~~

        public void WriteProcessMemory(IntPtr MemoryAddress, byte[] bytesToWrite)
        {
            IntPtr ptrBytesWritten;
            UNUSED_APIS.ProcessMemoryReaderApi.WriteProcessMemory(m_hProcess, MemoryAddress, bytesToWrite, (uint)bytesToWrite.Length, out ptrBytesWritten);
            // bytesWritten = ptrBytesWritten.ToInt32();
        }



I got this workspace from a interwebz, so that is the reason im a bit knocked down Sad
I tried to change some of the wrapper stuff to int, but that would just write always 0, because it fails ?
Back to top
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Sun Oct 12, 2008 2:25 pm    Post subject: Reply with quote

I imagine just writing an Int should be fine as long as you have the right size, and the pinvoke signature is right.
Back to top
View user's profile Send private message
Dami
Master Cheater
Reputation: 0

Joined: 23 Oct 2006
Posts: 336

PostPosted: Sun Oct 12, 2008 4:12 pm    Post subject: Reply with quote

slovach wrote:
I imagine just writing an Int should be fine as long as you have the right size, and the pinvoke signature is right.


You mean int size 255 or less ?
Pinvoke ? gotta google that one Wink
Back to top
View user's profile Send private message
Overload
Master Cheater
Reputation: 0

Joined: 08 Feb 2008
Posts: 293

PostPosted: Sun Oct 12, 2008 4:57 pm    Post subject: Reply with quote

Code:
[DllImport("kernel32.dll")]
static extern bool WriteProcessMemory(
IntPtr hProcess,
IntPtr lpBaseAddress,
byte[] lpBuffer,
UIntPtr nSize,
out IntPtr lpNumberOfBytesWritten
);

public bool WriteToMem(IntPtr ProcessHandle)
{
IntPtr bytesout;
//Create Byte array containing a NOP (0×90 in Hexadecimal)
byte[] NewVal = { 0×90 };

//Write To Memory (0×05b9466)
return WriteProcessMemory(ProcessHandle, (IntPtr)0×05b9466 , NewVal, (UIntPtr)NewVal.Length, out bytesout);
}


Code:
if (
(WriteToMem(ProcessHandle) == true))
{
MessageBox.Show(”WriteProcessMemory Succeeded!”);
}
else
{
MessageBox.Show(”WriteProcessMemory Failed!”);
}
//Close Handle
CloseHandle(ProcessHandle);
}


that what you want?

_________________
Blog

Quote:
Rhys says:
you can be my maid
Rhys says:
ill buy you a french maid outfit
Tyler says:
Sounds good
Rhys says:
ill hold you to that
Back to top
View user's profile Send private message MSN Messenger
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Sun Oct 12, 2008 5:19 pm    Post subject: Reply with quote

Dami3n wrote:
slovach wrote:
I imagine just writing an Int should be fine as long as you have the right size, and the pinvoke signature is right.


You mean int size 255 or less ?
Pinvoke ? gotta google that one Wink


The DLLImport stuff is your PInvoke signature.

Change the declaration from byte array to an int, then just write it as expected.

I swear whatever kind of class / wrapper you're using is just complicating things.

Code:
using System.Runtime.InteropServices;

   [DllImport("kernel32.dll")]
      public static extern bool WriteProcessMemory(IntPtr hProcess,
                       IntPtr lpBaseAddress,
                       UInt32 lpBuffer,
                       UInt32 nSize,
                       ref UInt32 lpNumberOfBytesWritten);
   
   Process[] p = Process.GetProcessesByName("winmine");
   WriteProcessMemory(p[0].Handle, 0x100579C, 500, sizeof(UInt32), 0);
Back to top
View user's profile Send private message
Dami
Master Cheater
Reputation: 0

Joined: 23 Oct 2006
Posts: 336

PostPosted: Mon Oct 13, 2008 4:39 am    Post subject: Reply with quote

slovach wrote:
Dami3n wrote:
slovach wrote:
I imagine just writing an Int should be fine as long as you have the right size, and the pinvoke signature is right.


You mean int size 255 or less ?
Pinvoke ? gotta google that one Wink


The DLLImport stuff is your PInvoke signature.

Change the declaration from byte array to an int, then just write it as expected.

I swear whatever kind of class / wrapper you're using is just complicating things.

Code:
using System.Runtime.InteropServices;

   [DllImport("kernel32.dll")]
      public static extern bool WriteProcessMemory(IntPtr hProcess,
                       IntPtr lpBaseAddress,
                       UInt32 lpBuffer,
                       UInt32 nSize,
                       ref UInt32 lpNumberOfBytesWritten);
   
   Process[] p = Process.GetProcessesByName("winmine");
   WriteProcessMemory(p[0].Handle, 0x100579C, 500, sizeof(UInt32), 0);



Did i change something wrong?

Code:
Error   1   The best overloaded method match for 'DamiInfo.frmMain.WriteProcessMemory(System.IntPtr, System.IntPtr, uint, uint, ref uint)' has some invalid arguments
Error   2   Argument '5': cannot convert from 'int' to 'ref uint'   

~~~~~~~~~~


                IntPtr MOUSEADDRESS = (IntPtr)0x00803570;
                WriteProcessMemory(p[0].Handle, MOUSEADDRESS, 500, sizeof(UInt32), 0);


~~~~~~~~~~

        [DllImport("kernel32.dll")]
        public static extern bool WriteProcessMemory(IntPtr hProcess,
                   IntPtr lpBaseAddress,
                   UInt32 lpBuffer,
                   UInt32 nSize,
                   ref UInt32 lpNumberOfBytesWritten);



Edit: Soz didnt notice your reply overload...
i want to write normal value, not hex Wink
Back to top
View user's profile Send private message
Stylo
Grandmaster Cheater Supreme
Reputation: 3

Joined: 16 May 2007
Posts: 1073
Location: Israel

PostPosted: Mon Oct 13, 2008 7:10 am    Post subject: Reply with quote

there's a tool on the internet that helped me a lot with reading / writing to memory it calls trainer maker helper
refresh to see attachment
edit: almost forgot, it comes along with this class too ProcessMemoryReaderLib



The Extension 'rar' was deactivated by an board admin, therefore this Attachment is not displayed.


The Extension 'rar' was deactivated by an board admin, therefore this Attachment is not displayed.


_________________
Stylo


Last edited by Stylo on Mon Oct 13, 2008 7:16 am; edited 2 times in total
Back to top
View user's profile Send private message
Dami
Master Cheater
Reputation: 0

Joined: 23 Oct 2006
Posts: 336

PostPosted: Mon Oct 13, 2008 7:14 am    Post subject: Reply with quote

1qaz wrote:
there's a tool on the internet that helped me a lot with reading / writing to memory it calls trainer maker helper
refresh to see attachment


Nnnnnnice Wink

Il take a look at it, and see if i can get something made Razz
Ok i checked it, ITS FREAKING NICE!!!!!!!!!!!!!!!!!!!!!!!!!!
I didnt expect this much help for c# from here, my god Wink
Thank you all, cya soon when i get something else to ask Very Happy


Last edited by Dami on Mon Oct 13, 2008 7:24 am; edited 1 time in total
Back to top
View user's profile Send private message
Stylo
Grandmaster Cheater Supreme
Reputation: 3

Joined: 16 May 2007
Posts: 1073
Location: Israel

PostPosted: Mon Oct 13, 2008 7:16 am    Post subject: Reply with quote

watch edit...
_________________
Stylo
Back to top
View user's profile Send private message
Dami
Master Cheater
Reputation: 0

Joined: 23 Oct 2006
Posts: 336

PostPosted: Mon Oct 13, 2008 7:25 am    Post subject: Reply with quote

1qaz wrote:
watch edit...


Yep, got it Wink
Back to top
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Mon Oct 13, 2008 12:30 pm    Post subject: Reply with quote

Dami3n wrote:
Code:
Error   1   The best overloaded method match for 'DamiInfo.frmMain.WriteProcessMemory(System.IntPtr, System.IntPtr, uint, uint, ref uint)' has some invalid arguments
Error   2   Argument '5': cannot convert from 'int' to 'ref uint'   

                IntPtr MOUSEADDRESS = (IntPtr)0x00803570;
                WriteProcessMemory(p[0].Handle, MOUSEADDRESS, 500, sizeof(UInt32), 0);


~~~~~~~~~~

        [DllImport("kernel32.dll")]
        public static extern bool WriteProcessMemory(IntPtr hProcess,
                   IntPtr lpBaseAddress,
                   UInt32 lpBuffer,
                   UInt32 nSize,
                   ref UInt32 lpNumberOfBytesWritten);



Edit: Soz didnt notice your reply overload...
i want to write normal value, not hex Wink


My bad, try
Quote:
out IntPtr lpNumberOfBytesWritten
Back to top
View user's profile Send private message
Dami
Master Cheater
Reputation: 0

Joined: 23 Oct 2006
Posts: 336

PostPosted: Mon Oct 13, 2008 1:11 pm    Post subject: Reply with quote

slovach wrote:
Dami3n wrote:
Code:
Error   1   The best overloaded method match for 'DamiInfo.frmMain.WriteProcessMemory(System.IntPtr, System.IntPtr, uint, uint, ref uint)' has some invalid arguments
Error   2   Argument '5': cannot convert from 'int' to 'ref uint'   

                IntPtr MOUSEADDRESS = (IntPtr)0x00803570;
                WriteProcessMemory(p[0].Handle, MOUSEADDRESS, 500, sizeof(UInt32), 0);


~~~~~~~~~~

        [DllImport("kernel32.dll")]
        public static extern bool WriteProcessMemory(IntPtr hProcess,
                   IntPtr lpBaseAddress,
                   UInt32 lpBuffer,
                   UInt32 nSize,
                   ref UInt32 lpNumberOfBytesWritten);



Edit: Soz didnt notice your reply overload...
i want to write normal value, not hex Wink


My bad, try
Quote:
out IntPtr lpNumberOfBytesWritten


Hmm i already took 1qaz method, and its working Wink
But yeah, that is way much better than what i had before 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