Making and Returning Java Byte Arrays in C++ via JNI (Help! Please!)

G

Gordon Beaton

passing arrays back and forth to C++ is tricky. You need a text book
to explain it all.

No it's not and no you don't.

If you have a moderate grasp of C or C++, then it's just an API like
any other. If you don't, then you probably shouldn't be using JNI.

/gordon
 
R

Roedy Green

If you have a moderate grasp of C or C++, then it's just an API like
any other. If you don't, then you probably shouldn't be using JNI.

It the time I was learning, it was an unusually poorly documented API
without any sample code or tutorials. It all made sense once I got a
text book that showed some sample code.
 
R

res7cxbi

PROBLEM SOLVED!!!

I simply memcpy'd twice on some temporary BYTE*s then copy them into
jbyte using a for loop

BYTE* tempMem;
DWORD len = 0;
hr = inssBuffer->GetBufferAndLength(&tempMem, &len);
BYTE* tempMem2 = new BYTE[len];
jbyteArray bArray = env->NewByteArray( len);
jbyte *jBytes = env->GetByteArrayElements( bArray, 0);
if(hr == S_OK) {
memcpy(tempMem2, tempMem, len);
for(DWORD i = 0; i < len; i++) {
jBytes = tempMem2;
}
}
//then clean up (cleanup code ommited for clarity
env->ReleaseByteArrayElements( bArray, jBytes, 0);
return bArray;

so now i am able to return the entire array

This works perfectly this time. Thanks for all who tried to help me.
It is very much appreciated - thaks for reminding me to put in
error-checking code and giving me some ways to test it out and giving
me tips

Thank you all again!
 
R

res7cxbi

Sorry, i copy and pasted the code all wrong

BYTE* tempMem;
BYTE* tempBuffer;
DWORD len = 0;
hr = inssBuffer->GetBufferAndLength(&tempBuffer, &len);
BYTE* tempMem2 = new BYTE[len];
jbyteArray bArray = env->NewByteArray( len);
jbyte *jBytes = env->GetByteArrayElements( bArray, 0);
if(hr == S_OK) {
memcpy(tempMem, tempBuffer, len);
memcpy(tempMem2, tempMem, len);
for(DWORD i = 0; i < len; i++) {
jBytes = tempMem2;
}


}
 
G

Gordon Beaton

Sorry, i copy and pasted the code all wrong

BYTE* tempMem;
BYTE* tempBuffer;
DWORD len = 0;
hr = inssBuffer->GetBufferAndLength(&tempBuffer, &len);
BYTE* tempMem2 = new BYTE[len];
jbyteArray bArray = env->NewByteArray( len);
jbyte *jBytes = env->GetByteArrayElements( bArray, 0);
if(hr == S_OK) {
memcpy(tempMem, tempBuffer, len);
memcpy(tempMem2, tempMem, len);
for(DWORD i = 0; i < len; i++) {
jBytes = tempMem2;
}
}


I hate to disappoint you, but I don't believe for a moment that
copying the data twice with memcpy (and a *third* time in the loop!)
actually solves anything:

http://catb.org/~esr/jargon/html/C/cargo-cult-programming.html

Also, where do you initialize tempMem? Where do you free tempMem2?

/gordon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,144
Latest member
KetoBaseReviews
Top