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 8895667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:50:32+00:00 2026-06-14T23:50:32+00:00

I have a C++ method with the following signature: int cppGetData(string& data); It should

  • 0

I have a C++ method with the following signature:

int cppGetData(string& data);

It should be called from a Java method.

Version I:

byte[] javaGetData();

Which should return null if cppGetData returns a non zero return code.

I understand that I have to write something like this:

JNIEXPORT jbyteArray JNICALL jniGetData(JNIEnv * env, jobject thisObj) 
(
  string data;
  if (cppGetData(data))
  {
    return NULL;
  }

  jbyteArray jData = env->NewByteArray(data.size());
  env->SetByteArrayRegion(jData, 0, data.size(), (jbyte *)data.c_str());

  return jData;
}

My problem is that I loose the status returned by cppGetData. On the other hand, Java does not have return by reference, so I am puzzled. How do I return byte[] and an integer status?

Of course, I could have a wrapper, something like this:

public class ByteArrayWrapper{
  public byte[] bytes;
}

And then javaGetData becomes:

int javaGetData(ByteArrayWrapper wrapper);

This is awful. Both in terms of the API signature and in terms of the jniGetData method complexity.

Is there a better way?

  • 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-14T23:50:33+00:00Added an answer on June 14, 2026 at 11:50 pm

    The closest to what you have that will work is

    native static int cppGetData(ByteBuffer bb);
    

    This is not a lot better than what you have in that ByteBuffer is a wrapper for a byte[], but it uses built in class to do it. A limitation is that the underlying byte[] is not resizable but you can make it smaller by setting the limit.

    Another option is to use

    native static int cppGetData(byte[][] bytes)
    

    This allows you to return a different byte[].

    Or you could

    int[] num = { 0 };
    
    native static byte[] cppGetData(int[] num)
    

    or update fields of the object like

    class CallsCppGetData {
        int num;
        byte[] bytes;
    
        native void cppGetData(); // sets this.num and this.bytes.
    }
    

    The solution to returning more than one value is to use an object.

    Either you

    • update multiple fields of the object you are calling the method on.
    • return an object when wraps multiple fields.
    • pass a mutable object to be set. e.g. you can pass an int[] which has one element.

    While creating new classes is awful, so is having a method without than one result. 😉

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

Sidebar

Related Questions

I have the following method signature: IAsyncResult BeginWrite(byte[] buffer, int offset, int size, AsyncCallback
I have a method with the following signature. Foo GetFooById( int id, params string[]
Suppose I have the following method signature int f (int[] values) and I call
I have a controller method with the following signature: [AcceptVerbs(HttpVerbs.Post)] public ActionResult UpdateValues(int id,
I have a method with the following signature: public ActionResult RenderFamilyTree(string name, Dictionary<string, string>
I have a general Java method with the following method signature: private static ResultSet
I have the following method signature: internal static int[] GetStudentIDsThatAreNotLinked(PrimaryKeyDataV1[] existingStudents, IQueryable<Student> linkedStudents) PrimaryKeyData
problem I have the following C callback signature from a C library called Foo
I have a method inside a webservice, with the following signature: @WebResult(name=purchaseId) public int
I have created a repository method with the following signature: T Get<TProperty>(int id, Expression<Func<T,

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.