[英]JNA wrong structure field values
I'm dancing around JNA adapter to "twain.h" - almost did it, but still have some problems with getting scanner capabilities. 我在JNA适配器周围跳舞到“ twain.h”-差不多做到了,但是在获取扫描仪功能方面仍然存在一些问题。 There is an original enity: 有一个原始的实体:
typedef unsigned short TW_UINT16, FAR *pTW_UINT16;
typedef unsigned long TW_UINT32, FAR *pTW_UINT32;
typedef struct {
TW_UINT16 ItemType;
TW_UINT32 MinValue; /* Starting value in the range. */
TW_UINT32 MaxValue; /* Final value in the range. */
TW_UINT32 StepSize; /* Increment from MinValue to MaxValue. */
TW_UINT32 DefaultValue; /* Power-up value. */
TW_UINT32 CurrentValue; /* The value that is currently in effect. */
} TW_RANGE, FAR * pTW_RANGE;
This is my mapping (thnx JNAerator) 这是我的映射(thnx JNAerator)
public static class TW_RANGE extends Structure {
/** C type : TW_UINT16 */
public short ItemType;
/** C type : TW_UINT32 */
public NativeLong MinValue;
/** C type : TW_UINT32 */
public NativeLong MaxValue;
/** C type : TW_UINT32 */
public NativeLong StepSize;
/** C type : TW_UINT32 */
public NativeLong DefaultValue;
/** C type : TW_UINT32 */
public NativeLong CurrentValue;
public TW_RANGE() {
super();
}
protected List<? > getFieldOrder() {
return Arrays.asList("ItemType", "MinValue", "MaxValue", "StepSize", "DefaultValue", "CurrentValue");
}
}
When I'm asking scanner for this entity, scanner sends me back filled object, but when I create it through 当我向扫描仪询问该实体时,扫描仪会向我发送回填充的对象,但是当我通过创建该对象时
new TW_RANGE(pointer)
it returns me some wired stuff 它给我一些有线的东西
TwaindsmLibrary$TW_RANGE(native@0x157c000c) (22 bytes) {
short ItemType@0=870
NativeLong MinValue@2=1572916
NativeLong MaxValue@6=5500
NativeLong StepSize@a=2097152
NativeLong DefaultValue@e=5500
NativeLong CurrentValue@12=2621440
}
Structure.read()
must be called in order to copy the native memory into your JNA Structure
fields. 必须调用Structure.read()
才能将本机内存复制到您的JNA Structure
字段中。
The easiest way to do this is to ensure Structure.read()
is called from your TW_RANGE(Pointer)
constructor as the very last statement. 最简单的方法是确保从TW_RANGE(Pointer)
构造函数中调用Structure.read()
作为最后一条语句。
JNA normally calls Structure.read()/write()
automatically when making function calls. 通常,JNA在进行函数调用时会自动调用Structure.read()/write()
。 It intentionally does not do so in Structure
constructors, leaving it up to the programmer to decide the best point at which to perform that synchronization (if at all). 它在Structure
构造函数中故意不这样做,由程序员决定执行同步的最佳点(如果有的话)。
I got it!
我知道了!
After little debugging C++ code, I found that (TW_RANGE*)pointer makes the same mess as Java does.
经过少量调试C ++代码后,我发现(TW_RANGE *)指针与Java一样造成混乱。
But (TW_RANGE**)pointer works like a charm.
但是(TW_RANGE **)指针的作用就像是一种魅力。
So
所以
new TW_RANGE(pointer.getPointer(0))
[APPLAUSE]
This solution lead me to random memory access errors.
[掌声]
此解决方案使我
遇到
随机内存访问错误。 I should work with Twain native MemAllocate/Lock/Free functions which returns right values from given pointer. 我应该使用Twain本机MemAllocate / Lock / Free函数,该函数从给定的指针返回正确的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.