1

如何使用检查类型 char*?(检查填充功能并包含文本> 500个字符或带有文本的内存指针)

我有 my_dll.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
};

在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));
}
4

2 回答 2

1

您的Check字段必须是指针类型。如果将其声明为Pointer,则可以Pointer.getString(0)用于提取String值。

如果由调用者分配内存,您可以使用com.sun.jna.Memory它来初始化它;如果没有,您将需要释放结构中返回的内存以避免泄漏。

于 2013-06-26T18:26:56.140 回答
1

我尝试过的一件事是使用预先构造的字节数组,然后Native.toString(...)将其转换为 Java 字符串。例如,

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

在上面的代码中,512 作为我的 SOME_CONSTANT 效果很好,但您可能需要使用更大的常量。

于 2013-06-26T14:54:11.773 回答