JNA。 char *它不包含结果

时间:2016-04-18 08:20:01

标签: java c++ c reference jna

我有外部C方法的dll:

extern "C" {
  int ADAPTERSHARED_EXPORT full_hash(unsigned char* data,
    uint64_t size,
    int algorithm,
    char* result,
    int *res_size
   );
}

从java中调用此方法 接口

public interface CA extends Library {
        CA INSTANCE = (CA) Native.loadLibrary(
                (Platform.isWindows() ? "HashAdapterC" : "libHAL"), CA.class);
        int full_hash(byte[] data, long size, int algorithm, String result, IntByReference res_size);
}

和主要方法

public static void main(String[] args) {
        logger.debug("being started");
        try {
            CA lib = CA.INSTANCE;
            String str = "1234567";
            String res = "";
            IntByReference in = new IntByReference(32);
            byte[] data = str.getBytes();
            int i = lib.full_hash(data, str.length(), 3, res, in);
            logger.debug("result = " + res);
            logger.debug("error = " + i);
            return;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("err");
            return;
        }
    }

调用空字符串的结果后。

如果从C

调用它
 unsigned char data[] = {
            0xB1, 0x94, 0xBA, 0xC8, 0x0A, 0x08, 0xF5, 0x3B,
            0x36, 0x6D, 0x00, 0x8E, 0x58
        };
        uint64_t size = sizeof(data);
    int res_size = 32;
    char* result = (char*)malloc(res_size);
    int is_success = hash(data, size, 3, result, &res_size);
    if (is_success == 0)
    {
        printf("Success\n");
        int i;
        for (i = 0; i < res_size; ++i)
        {
            printf("%X", (unsigned char)result[i]);
        }
        printf("\n");
    }

我得到结果字符串。

不是char * - 字符串?或者我会使用其他类型吗?

1 个答案:

答案 0 :(得分:1)

必须将结果声明为byte []

public interface CA extends Library {
    CA INSTANCE = (CA) Native.loadLibrary(
            (Platform.isWindows() ? "HashAdapterC" : "libHAL"), CA.class);
    int full_hash(byte[] data, long size, int algorithm, byte[] result, IntByReference res_size);
}