从一个Java应用程序向另一个Java应用程序发送JNA指针

从一个Java应用程序向另一个Java应用程序发送JNA指针

问题描述:

我有一个客户端和一个服务器应用程序.客户端将String命令发送到服务器,服务器使用JNA对两个DLL库接口进行相关调用.自然地,我会被要求在Java方面使用Pointers.I无法通过套接字连接发送Pointer对象,因为它们不可序列化.为了解决这个问题,我认为我将使用Pointer.nativeValue(p)获得Pointer的本机长值,通过连接发送该long值,并使用它在客户端上创建新的Pointer.但是,尽管客户端的Pointer对象具有与服务器端相同的本机值,但是它没有指向客户端的任何内容,并且出现了无效的内存访问错误.

I have a client and a server application. The client sends String commands to the server, which uses JNA to make relevant calls to two DLL library interfaces. Naturally, I will be required at some point to use Pointers on the java side of things.I am unable to send Pointer objects over the socket connection as they are not serializable. To get around this, I thought that I would get the native long value of the Pointer using Pointer.nativeValue(p), send that long value over the connection and use it to create a new Pointer on the client side. However, though the Pointer object on the client side has the same native value of the one on the server side, it does not point to anything on the client side and I get an Invalid Memory Access error.

现在,在我的客户端应用程序中,我正在使用JavaFX创建一个可在其中绘制DLL的窗口,因此我必须获取JavaFX窗口的HWND.然后,我要做的是将JNA的HWND对象的本机值发送到服务器,然后服务器使用该长本机值重新创建HWND对象.那个有效.但是显然Pointer和其他类似的JNA对象却没有.我认为那些Pointer对象和HWND对象(以及WinDef类中的其他对象)之间的区别在于,HWND实际上是一个本机值,因为它来自Windows本身,否则它作为本机将是毫无用处的窗户把手.但是我认为JNA指针仅存在于当前运行的JRE中.因此,将该指针值传输到另一个JRE(我的客户端正在运行)将无法正常工作.我真的不是100%知道其中的任何一个.

Now, in my client application I am using JavaFX to create a window within which my DLLs can draw, so I have to get the HWND of the JavaFX window. What I then do is send the native value of JNA's HWND object to the server, which then recreates the HWND object using that long native value. THAT works. But obviously the Pointer and those other similar JNA objects do not. I think the difference between those Pointer objects and the HWND object (as well as the others in the WinDef class) is that the HWND is actually a native value, as in it comes from Windows itself, otherwise it would be pretty useless as a native window handle. But I think that JNA Pointers exist only within the currently running JRE. So transferring that pointer value to another JRE (where my client is running) will not work. I'm really not 100% sure about any of this.

我只是C编程的初学者,所以我对指针一无所知,而对Java中的C指针则了解甚少.但是,请有人让我知道我是否对此有正确的想法,并进一步解释它,尤其是这些指针如何在Java中工作.

I'm only a beginner at C programming and so I don't know all that much about pointers, much less C pointers in java. But please can someone let me know if I have the right idea about this and explain it further, particularly about how these Pointers work within java.

这只是我所做的一个例子:

Here's just an example of what I did:

// Server side (PointerTest class)
public long getPtrVal() {
  Pointer p = new Memory(100);
  p.setString(0, "Test");
  long ptrVal = Pointer.nativeValue(p);
  return ptrVal;    // return value is processed in separate class
}

// Client side (MyClient sends String command, returns Object)
public static void main(String[] args) {
  MyClient c = new MyClient();
  long ret = (long) c.sendCommand("PointerTest");
  Pointer p = new Pointer(ret);
  String pointerString = p.getString(0);
  System.out.println(pointerString);
}

我也使用IntByReference进行了尝试.尽管它没有像Pointer对象那样引发错误,但在服务器端设置的值为1234,但在客户端获得的值为0.

I also tried this with IntByReference. Although it did not throw an error as with the Pointer object, the value set on the server side was 1234 but the value I got on the client side was 0.

指针的值是进程地址空间中的地址.在具有虚拟内存子系统的OS(包括您将遇到的所有通用OS,尤其是Windows)上,此地址固有地对于该过程而言是特定的.一个进程无法通过指针或其他方式访问另一个进程的内存.这种内存保护是有意且非常值得期待的功能.

The value of a pointer is an address in the process's address space. On an OS featuring a virtual memory subsystem (including all general-purpose OSs you will meet, and Windows in particular), such an address is inherently particular to the process. One process cannot access the memory of another, whether via a pointer or otherwise. This kind of memory protection is an intentional and highly desirable feature.

另一方面,给定的OS可能会提供具有机器范围的资源.在MS Windows上,窗口就是这种资源,它的句柄为每个进程指定相同的窗口.

On the other hand, a given OS may provide for resources with machine scope. On MS Windows, a window is such a resource, and its handle designates the same window to every process.