0

I have the following C# code:

  int num = Convert.ToInt32(this.Memory.ReadInt(
        this.Memory.BaseAddress() + 0x80f0cc, new int[] { 12, 0x6c5c }));

This reads the data from the memory and gives me the integer value.

Is that somehow possible to achieve in Java? I've tried searching online but couldn't get anything.

4 Answers 4

1

No, Java is specifically designed to disallow that... but...

It's not officially supported (and how/whether or not it works likely varies between JVM implementations), but the class sun.misc.Unsafe allows one to do stuff that you're normally not supposed to be able to do. See this article.

Excerpt:

@Test
public void testCopy() throws Exception {
    long address = unsafe.allocateMemory(4L);
    unsafe.putInt(address, 100);
    long otherAddress = unsafe.allocateMemory(4L);
    unsafe.copyMemory(address, otherAddress, 4L);
    assertEquals(100, unsafe.getInt(otherAddress));
}

The unsafe.getInt(otherAddress) call may be the closest thing you can achieve in pure Java.

There was a survey in 2014 about whether or not Unsafe should gain official support, but I'm not sure what (if anything) came of that.

Sign up to request clarification or add additional context in comments.

1 Comment

If you allocate a memory via the unsafe.allocateMemory, don't forget to call unsafe.freeMemory when you finish your work with that memory. Otherwise you will create a memory leak.
0

No. It is not possible to access native memory with pure Java code. You can use JNA or JNI to link to native libraries, and they can access arbitrary memory but Java itself runs in a virtual machine and does not allow you to access memory explicitly inside or outside of the virtual machine.

2 Comments

Thanks for you answer. Could you name any JNA library I can use with Java to achieve that ?
Any native library that allows you to access native memory. For example, your C# code above.
0

Makky, you can look at the old reply about memory management in java. He describes why you can not do this.

Comments

0

You could try going over to Project Kenai and having a look at the API and their examples:

Project Kenai

I also found an example for someone else who was trying to do something similar. Hope it helps.

Example

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.