Results 1 to 1 of 1
  1. #1
    horato is offline Member
    Join Date
    Sep 2011
    Posts
    9
    Rep Power
    0

    Default Java Native Access - bad memory reading

    Hello, Iam trying to edit memory of 1 program, but it always gets something completely different than expected. I'll show you what I mean:
    Java Native Access - bad memory reading-jneu.jpg

    The program on left side i Cheat Engine and shows the real values (on the bottom), but eclipse shows some weird -122 and etc numbers. As you can see the address which Iam trying to read (ap_to_get_address1) is same as 1st line of cheat engine. Any clues what can cause this? (code on the screenshot is bad, i've made change which is posted below, but its still not working)

    //edit: lol fail, outputBuffer.getInt(0); did the trick :D.

    here are the libs Iam using:
    Java Code:
    https://github.com/downloads/twall/jna/platform.jar
    https://github.com/downloads/twall/jna/jna.jar
    and the code:
    main.java:
    Java Code:
    package main;
    
    import com.sun.jna.Memory;
    import com.sun.jna.Pointer;
    
    import de.keving.CKernel32;
    import de.keving.CUser32;
    
    public class Main
    {
    	private static final int PROCESS_QUERY_INFORMATION = 0x0400;
    	private static final int PROCESS_VM_READ = 0x0010;
    	private static final int PROCESS_VM_WRITE = 0x0020;
    	private static final int PROCESS_VM_OPERATION = 0x0008;
    	private static final int PROCESS_ACCESS_FLAG = 0x0439;
    	private static final int ap_to_get_address1 = 0x2031FC80;
    	private static final int ap_to_get_address2 = 0x2031FC70;
    	private static final int enemy_count_address = 0x20692020;
    	private static CUser32 User32 = CUser32.INSTANCE;
    	private static CKernel32 Kernel32 = CKernel32.INSTANCE;
    	
    	public static void main(String[] args)
    	{
    		int hWnd = User32.FindWindowA(null, "Final Fantasy X (PAL-E) [SCES-50490] [Status = Playable] [1 Fixes]");
    		int[] dwProcessId = new int[1];
    		User32.GetWindowThreadProcessId(hWnd, dwProcessId);
    		
    		Pointer process = Kernel32.OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, false, dwProcessId[0]);
    		if (process == null)
    			throw new RuntimeException("no such pid");
    
    		Memory outputBuffer = new Memory(4);
    		Kernel32.ReadProcessMemory(process, ap_to_get_address1, outputBuffer, 4, null);
    		for(byte b: outputBuffer.getByteArray(0, 4))
    		System.out.println(b);
    	}
    }
    CUser32.java
    Java Code:
    package de.keving;
    
    import com.sun.jna.Library;
    import com.sun.jna.Native;
    import com.sun.jna.Platform;
    
    /**
     * @author Mark
     * 
     */
    public interface CUser32 extends Library
    {
    
    	CUser32 INSTANCE = (CUser32) Native.loadLibrary((Platform.isWindows() ? "user32" : null), CUser32.class);
    
    	int FindWindowA(String ClassName, String WindowName);
    
    	int GetWindowThreadProcessId(int hWnd, int[] lpdwProcessId);
    }
    and CKernel32.java
    Java Code:
    package de.keving;
    
    import com.sun.jna.Library;
    import com.sun.jna.Native;
    import com.sun.jna.Platform;
    import com.sun.jna.Pointer;
    import com.sun.jna.ptr.IntByReference;
    
    /**
     * @author Mark
     * 
     */
    public interface CKernel32 extends Library
    {
    
    	CKernel32 INSTANCE = (CKernel32) Native.loadLibrary((Platform.isWindows() ? "kernel32" : null), CKernel32.class);
    
    	Pointer OpenProcess(int dwDesiredAccess, boolean bInheritHandle, int dwProcessId);
    
    	int TerminateProcess(int hProcess, int uExitCode);
    
    	int WriteProcessMemory(int hProcess, int lpBaseAddress, int[] lpBuffer, int nSize, int[] lpNumberOfBytesWritten);
    
    	boolean ReadProcessMemory(Pointer hProcess, int inBaseAddress, Pointer outputBuffer, int nSize, IntByReference outNumberOfBytesRead);
    }
    Thanks
    Last edited by horato; 06-26-2012 at 08:56 PM.

Similar Threads

  1. Replies: 1
    Last Post: 07-14-2011, 02:14 AM
  2. Replies: 1
    Last Post: 07-12-2011, 02:02 PM
  3. Replies: 2
    Last Post: 03-18-2010, 01:39 PM
  4. Connection to DLL using Java Native Access
    By cowboy in forum New To Java
    Replies: 1
    Last Post: 01-23-2010, 09:43 PM
  5. Java Native Access (JNA) return types of void *
    By burnumd in forum Advanced Java
    Replies: 5
    Last Post: 01-15-2010, 01:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •