2017-04-25 23 views
-1

私はJavaで書き直さなければならないC#コードを持っています。 メソッドはdllにあります。JNA C構造からJava

結果

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 PointerByReferencegetJob()に渡そうとしましたが、結果は常に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であるが、情報フィールドがnull

+0

どのようにJavaメソッドdで 'long'を使用していますかeclarationですが、C#では 'int'ですか?サイズの問題。 – cubrr

+0

私がintを使用する場合、私は12(ERR_OPERATION_NOT_ALLOWED)を得ます。 – user7918877

+0

Cコードではなく、Cコードと一致する必要があります。あなたの 'Atr'と' JobName'変数 'byte []'配列を正しい長さに初期化しましょう。あなたは 'JobLong'をどこに定義したか教えてくれません。 'JobInfo'の' ByReference'マッピングを宣言する必要はなく、関数に渡された構造体は自動的にポインタと見なされます。 –

答えて

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