gpt4 book ai didi

java - JNA 通过引用 dll 传递字符串但不返回

转载 作者:行者123 更新时间:2023-12-01 07:23:57 27 4
gpt4 key购买 nike

您好,我遇到了 java 和 dll 问题。

我需要通过引用将字符串值传递给 dll,但是,它不成功。

public short ReadData(int block_id, int offset, int data_size,StringByReference dataBuf, IntByReference err_code);

问题是StringByReference dataBuf,我尝试了很多方法来找到解决方案,但它不起作用。

调用函数为:

ReadData(0, 4, 13, ??? , status);

因为它没有.dll文件。

功能

`public static short Read_Data(int card_type, int block_id, int offset, int data_size, String dataBuf, IntByReference err_code){
short rc;
IntByReference status = new IntByReference(0);
int len = data_size+16;
//StringByReference dataBuf_ref = new StringByReference( spaces(data_size+16));
//StringBuilder zText = new StringBuilder ();
//zText.append(dataBuf);
dataBuf = spaces(data_size+16);
//StringByReference a = new StringByReference(dataBuf);
// StringByReference a = new StringByReference(dataBuf);

//Pointer ppp = new Memory(dataBuf.length()+1);
//ppp.setByte(len, dataBuf.getBytes()[0]);
//PointerByReference p = new PointerByReference(ppp);


//Pointer pp = null ;
//pp.setString(len, dataBuf);
//p.setValue(a.getPointer());
StringBuffer_REF z = new StringBuffer_REF(dataBuf);

//byte[] b = dataBuf.getBytes();
//Memory m = new Memory(len+1);
//m.write(0, b, 0, len);




System.out.println("Read_Data");

System.out.println("status "+status.getValue());




rc = s_ope.ReadData(block_id, offset, data_size,?????, status); /// HERE NEED HELP
// System.out.println("dataBuf_ref"+ a.getValue().replace(" ", "*"));

if(rc != 0){
System.out.println("Err Read_Data : "+rc+" - "+status.getValue());
}
err_code = status;
return rc;
}`

最佳答案

我在 this 上找到了您问题的答案地点。只需创建新的 StringByReference java 类型,它就应该可以工作。

// This is a class that facilitates passing a String by reference to
// C library routines so that the string may be modified by the C
// routine and the modifications is reflected on JAVA side as well
// Save this in a file StringByReference.java in current directory

import com.sun.jna.ptr.ByReference;

public class StringByReference extends ByReference {
public StringByReference() {
this(0);
}

public StringByReference(int size) {
super(size < 4 ? 4 : size);
getPointer().clear(size < 4 ? 4 : size);
}

public StringByReference(String str) {
super(str.length() < 4 ? 4 : str.length() + 1);
setValue(str);
}

private void setValue(String str) {
getPointer().setString(0, str);
}

public String getValue() {
return getPointer().getString(0);
}
}

关于java - JNA 通过引用 dll 传递字符串但不返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29162569/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com