| 
			
				|  | Cheat Engine The Official Site of Cheat Engine
 
 
 |  
 
	
		| View previous topic :: View next topic |  
		| Author | Message |  
		| denismu321 Newbie cheater
 
 ![]() Reputation: 0 
 Joined: 24 Dec 2018
 Posts: 20
 
 
 | 
			
				|  Posted: Fri May 28, 2021 5:02 pm    Post subject: Hi ,cannot get WriteProcessMemory to work in C# |   |  
				| 
 |  
				| 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 |  |  
		|  |  
		| atom0s Moderator
 
  Reputation: 205 
 Joined: 25 Jan 2006
 Posts: 8587
 Location: 127.0.0.1
 
 | 
			
				|  Posted: Fri May 28, 2021 10:22 pm    Post subject: |   |  
				| 
 |  
				| 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 |  |  
		|  |  
		| denismu321 Newbie cheater
 
 ![]() Reputation: 0 
 Joined: 24 Dec 2018
 Posts: 20
 
 
 | 
			
				|  Posted: Sat May 29, 2021 3:29 am    Post subject: Hi atom0s |   |  
				| 
 |  
				| 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 |  |  
		|  |  
		|  |  
  
	| 
 
 | 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
 
 |  |