Search code examples
javac++jna

Java JNA Dereference Pointer


Well I have the following code in c++

uintptr_t buf;

if ((reason = vm_read(task, (uintptr_t) &start, sizeof(int), &buf, &bytesRead)) != KERN_SUCCESS) {
    cout << "Failed to read: " << reason << endl;
}

cout << "Step 2 (after write, should = 100): " << (*(int *) buf) << endl;

Where it prints in step 2, I deref the buf pointer and convert to its int value.

I was wondering how can I do this in Java using JNA?

I've tried the following but it doesn't print the correct value.

    IntByReference o = new IntByReference();
    MemoryBuffer buffer = Cacheable.buffer(4);
    System.out.println(mac.vm_read(process.task(), new Pointer(140734594927432L), 4,  buffer, o));
    System.out.println(Pointer.nativeValue(buffer));

Solution

  • In order to read the proper value, I had to first call buffer.getPointer(0).getInt()

    Ex:

    IntByReference o = new IntByReference();
    MemoryBuffer buffer = Cacheable.buffer(4);
    System.out.println(mac.vm_read(process.task(), new Pointer(140734594927432L), 4,  buffer, o));
    System.out.println(buffer.getPointer(0).getInt());