简体   繁体   English

如何使用JNA Pointer在内存中写入数据?

[英]How to write data in memory using JNA Pointer?

I have a Pointer to a memory like: 我有一个像以下内存的指针:

Pointer pData = new Memory(65536);

and I need to get the pointer of that memory starting from position 8 because I want to send that partof the memory (from 8 to the 65535) to a native C API. 我需要从位置8开始获取该内存的指针,因为我想将内存的一部分(从8到65535)发送到本机C API。

I used: 我用了:

pData8 = pData.getPointer(8);

to obtain the pointer starting from position 8 then I tried to write something to pData8 using: 从位置8开始获取指针然后我尝试使用以下命令向pData8写入内容:

pData8.setInt(0xAAAA);

just to verify that I was writing in the right position but I get Error: Invalid memory access. 只是为了验证我写的是正确的位置,但我得到错误:内存访问无效。

How can I obtain a valid pointer to a part of the memory and be able to write on it? 如何获取指向内存部分的有效指针并能够在其上写入?

below the details: 详情如下:

    80 Pointer pM = new Memory(65536);
    81 Pointer p = pM.getPointer(4);
    82 pM.setInt(0, 0xFFFF);
    83 p.setInt(0, 0xBBBB);

Exception in thread "main" java.lang.Error: Invalid memory access
at com.sun.jna.Native.setInt(Native Method)
at com.sun.jna.Pointer.setInt(Pointer.java:1124)
at it.caen.dig.Demo.main(Demo.java:83)

It looks like you're using the wrong API. 看起来你使用的是错误的API。 getPointer returns the value found at that offset as a pointer (which probably points nowhere). getPointer返回在该偏移处找到的值作为指针(可能无处指向)。 If you want to get a pointer to that offset, use share : 如果要获取指向该偏移量的指针,请使用share

Provide a view of this memory using the given offset to calculate a new base address. 使用给定的偏移量提供此内存的视图以计算新的基址。

Pointer pM = new Memory(65536);
Pointer p = pM.share(4);
pM.setInt(0, 0xFFFF);
p.setInt(0, 0xBBBB);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM