JNA C 结构到 Java

JNA C structure to Java

我有一段 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#代码

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
        public struct jobInfo
        {
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
            public string Atr;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
            public string JobName;
            [MarshalAs(UnmanagedType.I1)]
            public bool IsSupported;
            public int Status;


        }

public static extern int search(string atr, out int job);
public static extern int getInfo(int job, ref JobInfo jobInfo);

int result=0;
int job=0;
result = search(null, out job); // result=0
JobInfo info=new JobInfo();
result=getInfo(job,ref info); // result=0
//info.Atr=354BBG
//info.JobName=Java developer
//info.IsSupported=true
//info.Status=active

职位信息

 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" });
        }

Java代码

    public int search(String atr, IntByReference card);

    public int getInfo(long card, ByReference jobInfo);

    int result=0;
    IntByReference job = new IntByReference(0);
    result = Native.search(null, job); // result=0
    JobInfo.ByReference info = new JobInfo.ByReference();
    result = Native.getInfo(job, info); // result=5

我试过 info.getPointer(),我试图将 new PointerByReference 传递给 getJob(),但结果总是 5 (ERR_INVALID_HANDLE)。

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" });
        }
  }