Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8228415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:26:55+00:00 2026-06-07T16:26:55+00:00

Here is the native C++ method. JNIEXPORT jboolean JNICALL Java_xpnp_XpNamedPipe_readBytes (JNIEnv* pEnv, jclass cls,

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T16:26:59+00:00Added an answer on June 7, 2026 at 4:26 pm

    Look up ReleaseByteArrayElements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm getting in trouble with a native library for windows. Here is the error
I am trying to bypass the call JNIEnv->DefineClass method with all necessary parameter but
Here is the Mp_DB class and the method getMPName() and delete() method are posted.
// example-1: GetStringUTFChars() public class Prompt { // native method that prints a prompt
I'm trying to call a java method from native C code in an Android
I have designed a component for J2Me, and here is the paint method: import
I'm trying to mock the java.lang.reflect.Method, here my code: @RunWith(PowerMockRunner.class) @PrepareForTest(Method.class) public class AuditDataTest
That might be a naive git question but here it is: Before doing a
Here's the view: @if (stream.StreamSourceId == 1) { <img class=source src=@Url.Content(~/Public/assets/images/own3dlogo.png) alt= /> }
Here's my code in the <head></head> : <link rel=stylesheet href=http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css /> <script type=text/javascript src=http://code.jquery.com/jquery-1.7.1.min.js></script>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.