简体   繁体   English

JNA返回字符*

[英]JNA return char*

how to work with Check type char*? 如何使用Check类型的char *? (Check fill in function and contains text >500 characters or pointer on memory with text) (检查填写功能,并包含> 500个字符的文本或带有文本的内存指针)

I have my_dll.dll. 我有my_dll.dll。 Description dll 说明dll

int my_function(char* param1, struct answer* ans);

#pragma pack(1)
struct answer{
  int TType;                   //IN
  unsigned long Amount;   //IN
  char          Rcode   [2+1];  //OUT
  char          AMessage[16 ]; //OUT
  int           CType;              //OUT
  char*         Check;            //OUT
};

In Java I have code: 在Java中,我有以下代码:

 public interface My_Dll extends Library {
        public static class answer extends Structure {
                    public static class ByReference extends answer 
                             implements Structure.ByReference {}

              public int  TType      = 0;                   
              public int  Amount     = 0;        
              public byte Rcode[]    = new byte[3]; //OUT: 
              public byte AMessage[] = new byte[16]; //OUT: 
              public int  CType      = 0;         //OUT: 
              public ??? Check;         //OUT: 
              protected List getFieldOrder() {
                      return Arrays.asList(new String[] {"TType", "Amount", 
                             "Rcode","AMessage", "CType","Check"});
              }
        }
        public int my_function(byte track2[], answer.ByReference ans);
  }

  public static void Start() {
      My_Dll test_dll = (My_Dll) Native.loadLibrary("my_dll", My_Dll.class);
      My_Dll.answer.ByReference aa = new My_Dll.answer.ByReference();
      //  In
      aa.Amount = 100;
      aa.TType =3;

      int result = test_dll.my_function(null,aa);
      //  OUT
      System.out.println("Result: "  + result);
      System.out.println("Rcode: "  + new String(aa.Rcode));
      System.out.println("Amessage: " + new String(aa.AMessage));
}

One thing I've tried is to use a pre-constructed array of byte, and then use Native.toString(...) to convert it to a Java String. 我尝试过的一件事是使用预先构造的字节数组,然后使用Native.toString(...)将其转换为Java String。 For example, 例如,

byte[] windowText = new byte[SOME_CONSTANT];
user32.GetWindowTextA(hWnd, windowText, SOME_CONSTANT);
String wText = Native.toString(windowText).trim();

In the code above, 512 worked well as my SOME_CONSTANT, but you will probably need to use a larger constant. 在上面的代码中,512作为我的SOME_CONSTANT可以很好地工作,但是您可能需要使用更大的常量。

Your Check field must be of pointer type. 您的“ Check字段必须是指针类型。 If you declare it as Pointer , you can use Pointer.getString(0) to extract the String value. 如果将其声明为Pointer ,则可以使用Pointer.getString(0)提取String值。

If it is up to the caller to allocate the memory, you can use com.sun.jna.Memory to initialize it; 如果由调用方分配内存,则可以使用com.sun.jna.Memory进行初始化;否则,可以使用com.sun.jna.Memory进行初始化。 if not, you will need to release the memory returned in the struct to avoid a leak. 如果没有,您将需要释放结构体中返回的内存,以避免泄漏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM