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 


Hi ,cannot get WriteProcessMemory to work in C#

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

Joined: 24 Dec 2018
Posts: 13

PostPosted: Fri May 28, 2021 5:02 pm    Post subject: Hi ,cannot get WriteProcessMemory to work in C# Reply with quote

Hi there,

I cannot seem to get WriteProcessMemory to work in this Code :

Code:

Process targetProcess = Process.GetProcessesByName(proc.ProcessName)[0];

            IntPtr procHandle = OpenProcess(PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ, false, targetProcess.Id);

            if(procHandle != null)
            {
                MessageBox.Show("opened process success.\n");
            }
            else
            {
                MessageBox.Show("opened process error.\n");

            }

            IntPtr loadLibraryAddr = GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");

            if (loadLibraryAddr != null)
            {
                MessageBox.Show("got proc address.\n");
            }
            else
            {
                MessageBox.Show("get proc address error.\n");

            }

            string dllName = "test.dll";
            byte[] bytes = Encoding.ASCII.GetBytes(dllName);

           

            IntPtr allocMemAddress = VirtualAllocEx(procHandle, IntPtr.Zero, (uint)((dllName.Length + 1) * Marshal.SizeOf(typeof(char))), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);

            if (allocMemAddress != null)
            {
                MessageBox.Show("alocated memory success.\n");
            }
            else
            {
                MessageBox.Show("memory allocation error.\n");

            }

            IntPtr bytesWritten;

            if(WriteProcessMemory(procHandle, allocMemAddress, bytes,bytes.Length, out bytesWritten))
            {
                MessageBox.Show("write success.\n");

            }
            else
            {
                MessageBox.Show("write error.\n");
            }

            // creating a thread that will call LoadLibraryA with allocMemAddress as argument
            IntPtr thread = CreateRemoteThread(procHandle, IntPtr.Zero, 0, loadLibraryAddr, allocMemAddress, 0, IntPtr.Zero);

           
            if(thread != null)
            {
                MessageBox.Show("thread created.\n");

            }
            else
            {
                MessageBox.Show("thread creation error.\n");

            }

        }



It always returns as false.

Is there something wrong with it, since this is the only part the code breaks ?
Back to top
View user's profile Send private message Send e-mail
atom0s
Moderator
Reputation: 179

Joined: 25 Jan 2006
Posts: 8238
Location: 127.0.0.1

PostPosted: Fri May 28, 2021 10:22 pm    Post subject: Reply with quote

You can use Marshal.GetLastWin32Error() to get the error code that is set when any Win32 API is called to find out why it failed. It should give you a better understanding of what failed.

As is, the WriteProcessMemory call looks fine, assuming the import is correct. Your VirtualAllocEx call may have issues though since you are trying to execute on the page later on but its only set to ReadWrite. (You will probably need to adjust that to ExecuteReadWrite instead to allow the injection to fully work once you get the other part working.)

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
denismu321
Newbie cheater
Reputation: 0

Joined: 24 Dec 2018
Posts: 13

PostPosted: Sat May 29, 2021 3:29 am    Post subject: Hi atom0s Reply with quote

Thanks for reaching out to me so fast.
I will use the Error function ,to have a better
understanding of what is going wrong as well
as setting the Rights of VirtualAllocEx to also
Execute.

Thanks again for your help,

I will report back later,

Denis.
Back to top
View user's profile Send private message Send e-mail
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