C# jnc结构到Java
我有一个C代码,我必须用Java重新编写。 方法在dll中 结果C# jnc结构到Java,c#,pointers,memory,jna,C#,Pointers,Memory,Jna,我有一个C代码,我必须用Java重新编写。 方法在dll中 结果 OK = 0, ERR_GENERAL = 1, ERR_INVALID_HANDLE = 5, ERR_OUT_OF_MEMORY = 11, ERR_OPERATION_NOT_ALLOWED = 12, ERR_OPERATION_NOT_SUPPORTED = 13, ERR_BUFFER_TOO_SMALL = 14 C#代码 工作信息 public class JobInfo
OK = 0,
ERR_GENERAL = 1,
ERR_INVALID_HANDLE = 5,
ERR_OUT_OF_MEMORY = 11,
ERR_OPERATION_NOT_ALLOWED = 12,
ERR_OPERATION_NOT_SUPPORTED = 13,
ERR_BUFFER_TOO_SMALL = 14
C#代码
工作信息
public class JobInfo extends Structure {
public String Atr;
public String JobName;
public boolean IsSupported;
public long Status;
public JobInfo() {
}
public JobInfo(Pointer pointer) {
super(pointer);
}
public static class ByReference extends JobInfo implements Structure.ByReference {
public ByReference() {
}
public ByReference(Pointer p) {
super(p);
read();
}
}
public static class ByValue extends JobInfo implements Structure.ByValue {
public ByValue() {
}
public ByValue(Pointer p) {
super(p);
read();
}
}
@Override
protected List<String> getFieldOrder() {
return Arrays.asList(new String[] { "Atr", "JobName", "IsSupported", "Status" });
}
我尝试了info.getPointer()
,我尝试将newpointerbyreference
传递给getJob()
,但结果总是5(ERR\u无效\u句柄)
C代码
typedef void* Job;
typedef unsigned long int JOBLong;
struct JobInfo
{
char Atr[64];
char JobName[64];
bool IsSupported;
JOBLong Status;
};
FUNCTION(search)
(
char atr[64],
Job* Job
);
FUNCTION(getInfo)
(
Job job,
JobInfo* jobInfo
);
我有其他的方法,但如果我能解决这个问题,我会问另一个问题
编辑:
如果我这样做
Pointer job;
PointerByReference handle = new PointerByReference();
result = Native.search(null, handle);
job = handle.getValue();
JobInfo info = new JobInfo();
result = Native.getInfo(job, info.getPointer()); // result=0
结果是0,但信息字段为空我已将字符串字段更改为字节数组,现在它可以工作了
public class JobInfo extends Structure {
public byte[] Atr=new byte[64];
public byte[] JobName=new byte[64];
public int IsSupported;
public int Status;
public JobInfo() {}
@Override
protected List<String> getFieldOrder() {
return Arrays.asList(new String[] { "Atr", "JobName", "IsSupported", "Status" });
}
}
公共类JobInfo扩展结构{
公共字节[]Atr=新字节[64];
公共字节[]作业名=新字节[64];
支持公共信息发布;
公众地位;
public JobInfo(){}
@凌驾
受保护列表getFieldOrder(){
返回Arrays.asList(新字符串[]{“Atr”、“JobName”、“IsSupported”、“Status”});
}
}
为什么在Java方法声明中使用long
,而在C#中使用int
?大小很重要。因为如果我使用int我得到12(不允许错误操作),你应该匹配C代码,而不是C代码。将Atr
和JobName
变量byte[]
数组初始化为正确的长度。您没有告诉我们您在哪里定义了JobLong
。不需要通过引用声明JobInfo
的ByReference
映射,传递给函数的结构将自动视为指针。
Pointer job;
PointerByReference handle = new PointerByReference();
result = Native.search(null, handle);
job = handle.getValue();
JobInfo info = new JobInfo();
result = Native.getInfo(job, info.getPointer()); // result=0
public class JobInfo extends Structure {
public byte[] Atr=new byte[64];
public byte[] JobName=new byte[64];
public int IsSupported;
public int Status;
public JobInfo() {}
@Override
protected List<String> getFieldOrder() {
return Arrays.asList(new String[] { "Atr", "JobName", "IsSupported", "Status" });
}
}