如题,我在java中使用了jna调用dll,dll中有个结构体有一些unsigned int类型参数,在java中我尝试用int接收,在我的demo.dll中调用可以获取正确的值
代码如下:
demo.dll 的头文件
#pragma once
#define DECLSPEC extern "C" _declspec(dllexport)
typedef struct demoStruct
{
unsigned int flag;
}demoStruct;
typedef struct demoStruct2
{
demoStruct * demoStruct;
}demoStruct2;
DECLSPEC unsigned int getData();
DECLSPEC demoStruct2 * getStruct();
demo.dll的 源文件:
#include "pch.h"
#include "demo.h"
#include <stdio.h>
unsigned int getData() {
unsigned int a = 43534;
return a;
}
demoStruct2 * getStruct() {
struct demoStruct * strut;
strut = new demoStruct();
unsigned int a = 43534;
strut->flag = a;
struct demoStruct2 *strut2;
strut2 = new demoStruct2();
strut2->demoStruct = strut;
return strut2;
}
然而在实际项目中使用int获取到的值是不正确的
实际项目dll 部分源码:
/*所有获取信息的结构体*/
typedef struct SqSdpcInfo
{
char *fileName;
SqPicHead *picHead;
}SqSdpcInfo;
typedef struct SqPicHead
{
unsigned int flag;
}SqPicHead;
java中对应结构体:
public class SqSdpcInfo extends Structure {
public String fileName;
public SqPicHead.ByReference picHead;
public SqSdpcInfo() {
super();
}
protected List<String> getFieldOrder() {
return Arrays.asList("fileName", "picHead");
}
public SqSdpcInfo(Pointer peer) {
super(peer);
}
public static class ByReference extends SqSdpcInfo implements Structure.ByReference {
};
public static class ByValue extends SqSdpcInfo implements Structure.ByValue {
};
public class SqPicHead extends Structure {
public int flag;
public SqPicHead() {
super();
}
protected List<String> getFieldOrder() {
return Arrays.asList("flag");
}
public SqPicHead(Pointer peer) {
super(peer);
}
public static class ByReference extends SqPicHead implements Structure.ByReference {
};
public static class ByValue extends SqPicHead implements Structure.ByValue {
};
以下是java调用dll代码
SqSdpcInfo.ByReference sqOpenSdpc = DecodeSdpc.Instance.SqOpenSdpc(filepath);
总结:demo中使用int可以正确的获取dll中 unsigned int 对应的值,而正式项目中int获取的数值不对