これがネイティブC++メソッドです。
JNIEXPORT jboolean JNICALL Java_xpnp_XpNamedPipe_readBytes
(JNIEnv* pEnv, jclass cls, jlong pipeHandle, jbyteArray readBufferJava, jint bytestoread, jint timeoutMsecs){
jbyte* readBuffer = NULL;
try {
readBuffer = pEnv->GetByteArrayElements(readBufferJava, NULL);
if (readBuffer == NULL) {
throw std::bad_alloc();
}
int retval = XPNP_readBytes ((XPNP_PipeHandle)pipeHandle, (char*)readBuffer, bytestoread, timeoutMsecs);
std::cout<<"this is what I read: " << readBuffer << "\n";
std::flush(std::cout);
return (retval <= 0) ? 0 : retval;
}catch (std::exception& except) {
// setErrorInfo(except.what());
}
return 0;
}
readBuffer
このメソッドは、呼び出しから読み取った正しいテキストを出力しますXPNP_readBytes
が、すべてゼロの配列をJavaに渡します。なぜそれが起こるのか考えていますか?ポインタを渡すか、Javaに変換する際に何か問題がありますか?
これは、JavaファイルでのネイティブC++メソッドの宣言です。
private static native boolean readBytes(long pipeHandle, byte[] buffer, int bytesToRead, int timeoutMsecs);
これは、私がネイティブメソッドを呼び出しているところです。
boolean b = readBytes(namedPipeHandle, buffer, bytesToRead, timeoutMsecs);
String a = new String(buffer);
buffer
ネイティブコードで正しいテキストを出力しているにもかかわらず、呼び出し後に読んだのはすべて0です。