views:

284

answers:

2

Hey there,

I would like to know if theres a possibility to call C++ methods from java.

Pretty much I want to be able to read memory processes from java.

I know c++, but I Would like to use a higher lvl like java, but still be able to hack into processes memory.

Any clues?

[]'s

+2  A: 

Here's the JNA project, which lets you call any function exposed in a DLL, without writing any JNI code.

Jonathan Feinberg
Hey there, I managed to find a nice example in here.. http://forums.sun.com/thread.jspa?threadID=5155307But its unclear to me, how to properly return the value of a given memory address.I tried entering the memory address in there, and it returned some ascii trash
rgoytacaz
A: 

here's the code snippet. Can someone clarify?

import com.sun.jna.Memory;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.win32.StdCallLibrary;

public class ReadProcessMemoryDemo {

    public static final int PROCESS_QUERY_INFORMATION = 0x0400;
    public static final int PROCESS_VM_READ = 0x0010;

    public interface Kernel32 extends StdCallLibrary {
        Kernel32 INSTANCE = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class);

        public Pointer OpenProcess(int dwDesiredAccess, boolean bInheritHandle, int dwProcessId);

        boolean ReadProcessMemory(Pointer hProcess, int inBaseAddress, Pointer outputBuffer, int nSize,
                IntByReference outNumberOfBytesRead);
    }

    public static void main(String[] args) {

        Kernel32 lib = Kernel32.INSTANCE;

        int pid = 1276;
        int bufferSize = 128;
        int offset = 65536;
        Pointer process = lib.OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, false, pid);

        if (process == null) {
            throw new RuntimeException("no such pid");
        }

        IntByReference baseAddress = new IntByReference();
        baseAddress.setValue(offset);
        Memory outputBuffer = new Memory(bufferSize);
        boolean success = lib.ReadProcessMemory(process, offset, outputBuffer, bufferSize, null);
        System.out.println("success = " + success);

        byte[] bufferBytes = outputBuffer.getByteArray(0, bufferSize);
        System.out.println(new String(bufferBytes));
    }
}
rgoytacaz