Reputation: 1006
Here is the native C++ method.
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;
}
This method prints the correct text of readBuffer
that it reads from the call XPNP_readBytes
, but passes an array of all zeros to Java! Any idea why that happens? Am I doing something wrong in passing the pointer or converting it to Java?
Here is the declaration of the native C++ method in the Java file.
private static native boolean readBytes(long pipeHandle, byte[] buffer, int bytesToRead, int timeoutMsecs);
This is where I'm calling the native method.
boolean b = readBytes(namedPipeHandle, buffer, bytesToRead, timeoutMsecs);
String a = new String(buffer);
The buffer
I read after the call is all 0's, even though it prints the right text in the native code!
Upvotes: 1
Views: 3052