Java JNA Mapping in Delphi Dll function

被刻印的时光 ゝ 提交于 2019-12-06 09:52:39
David Heffernan

It looks like your Delphi code is actually wrong.

The size parameter appears to be a means to pass the length of the data from the caller to the callee. As such it should not be a var parameter.

The data parameter appears to be a pointer to a buffer allocated by the caller. Again, that should not be a var parameter. Just a plain PByte, passed by value.

Which would make the Delphi code be like this:

function send_command(const command: Byte; size: Byte; data: PByte): Integer; 
  stdcall external 'comunication.dll';

And then the Delphi code would match your Java code.

Of course, I've had to make a number of guesses to write this answer. Guesses based on my experience of such problems, but guesses all the same. The real lesson that you should take away from this is that an interface is not specified by the types of its parameters. You also need to specify the semantics of the parameter passing.

So, when you have a PByte paramerer, is that a pointer to a single byte, or a pointer to an array? If the latter, how large is the array. And so on. You need to specify all of this information to define an interface.

If you really cannot change the DLL, then you'll need to pass the size and data parameters by reference, even though they appear to have value semantics. This answer covers passing by reference in JNA: How do I get a java JNA call to a DLL to get data returned in parameters?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!