我正在尝试通过 JNA 从 Java 调用 C++ 函数。我想传入一个字符串,然后返回一个字符串。这是通过使用 in 参数和 out 参数来完成的。我用outPointerByReference
来表示char**
参数。对 C++ 的调用有效,但PointerByReference
调用后为 null。
我的代码基于 PointerByReference文档。任何想法我做错了什么?
我在 C++ 中添加了 print 语句,以确保它没有将指针设置为 null,而且绝对不是。所以我对 JNA 的一些使用一定是错误的。但是什么?
C++ 代码
void processRequest(char* input, char** output, int* outputLength)
{
// Variable output size from processInput
std::string sOutput = processInput(input);
char* results = new char[sOutput.length() + 1];
strncpy(results, sOutput.c_str(), sOutput.length());
results[sOutput.length()] = '\0';
output = &results;
outputLength = new int(strlen(results) + 1);
}
Java 代码
像这样被java调用:
public class RunRequestT1 {
private static final Logger log = LoggerFactory.getLogger(MyLibWrapperImpl.class);
static boolean envVarsSetupDone = false;
static interface MyLib extends Library {
MyLib INSTANCE = (MyLib) Native.loadLibrary("N:\\sys1\\sys1_dist\\MyLib\\MyLib9.8.Q.P3.G15.T\\bin\\MyLib.dll", MyLib.class);
public void processRequest(String request, PointerByReference bufp, IntByReference lenp);
public void clearMemoryPtr(PointerByReference bufp, IntByReference lenp);
}
public static void main(String[] args) throws IOException {
System.out.println("sys1: Reading request");
String request = FileUtils.readFileToString(new File("C:\\dev\\prj\\single-request.xml"), "UTF-8");
System.out.println("sys1: Pricing request");
String response = processRequest(request);
System.out.println(response);
}
public static String processRequest(String request) {
System.out.println("sys1: prepare args");
// code based on https://github.com/twall/jna/blob/master/www/ByRefArguments.md
PointerByReference bufp = new PointerByReference();
IntByReference lenp = new IntByReference();
System.out.println("sys1: making call");
MyLib.INSTANCE.processRequest(request, bufp, lenp);
System.out.println("reading response");
System.out.println("bufp: " + bufp.getValue());
System.out.println("lenp: " + lenp.getValue());
Pointer p = bufp.getValue();
byte[] buffer = p.getByteArray(0, lenp.getValue());
String response = Native.toString(buffer);
//String response = p.getString(0);
// de-allocate memory buffer
System.out.println("cleaning memory");
MyLib.INSTANCE.clearMemoryPtr(bufp, lenp);
return response;
}
}
标准输出
当我调用它时,我得到这样的结果:
sys1: Reading request
sys1: Pricing request
sys1: prepare args
sys1: making call
reading response
bufp: null
lenp: 0
Exception in thread "main" java.lang.NullPointerException
at com.calyon.gcm.MyLibwrapper.main.RunRequestT1.priceRequest()
at com.calyon.gcm.MyLibwrapper.main.RunRequestT1.main()
我试图将其保持在最低限度,实际上并没有发生太多事情。
我已经使用 JNA 版本 3.4 和 3.5 进行了测试