5 messages in net.java.dev.jna.usersRe: [jna-users] pass three char data'...
FromSent OnAttachments
Landon WilliamsJul 15, 2008 12:26 pm 
Michael Brewer-DavisJul 16, 2008 9:24 am 
will...@gmail.comJul 16, 2008 1:15 pm 
Michael Brewer-DavisJul 16, 2008 1:48 pm 
Nikolas LotzJul 17, 2008 1:32 am 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:Re: [jna-users] pass three char data's back to javaActions...
From:Michael Brewer-Davis (mich@tech4learning.com)
Date:Jul 16, 2008 1:48:11 pm
List:net.java.dev.jna.users

I believe your issue here is that the array went out of scope, and hence became invalid. It shouldn't work in C either.

You'll need to return dynamically allocated memory (new or new[]):

char* getString() { char* ret = new char[5]; // remember null terminator char* ret = new char(5); // an alternative char* ret = (char*) malloc(5); // a pure C alternative strcpy(ret, "abcd"); return ret; }

In this case, you'd ideally delete/free the memory later on--not sure how that works from within JNA. On a small scale, you might be able to live with this memory leak. Others may have advice on delete/free?

You could also return memory owned by object that was going to stick around for the duration of your usage:

char* getString() { return myLongLastingVariable->getString(); }

will@gmail.com wrote:

I was able to get #1 working, passing a String TO a method.

I've been unable to get one back however.

<c_code> char* getString() { char ret[] = "abcd"; return ret; } </c_code>

<java> System.out.println(class.INSTANCE.getString()); </java>

It prints out garbage, sometime's random. I tried changing "return ret" to "return *ret", but that didn't help. I messed around as much as I could think of, but nothing seemed to work.

I'm pretty lost on the pointer's and everything with C. Its just been a long time, and I'm fully .converted to the Java mindset now. Thanks a lot for your help and time.

-Landon

Michael Brewer-Davis wrote:

It will help to provide a little more detail about what you want to do. Which C functions do you want to call from Java-land? (Or if you're rusty in C, what Java functions would you like to have available from C-land?)

That said, some basics (hopefully basic enough that I don't have to be corrected... =) ):

1. To pass a string to a C function: // C void setString(char* argument);

you can simply use String in the Java function: // Java void setString(String argument);

(For wchar_t*, use WString)

2. To receive a string as a return value, same deal: // C char* getString();

becomes // Java String getString();

(For wchar_t*, use WString)

3. To receive a string as a buffer value: // C void fillStringBuffer(char* buffer, int length);

you need to pass an appropriate buffer: // Java void fillStringBuffer(byte[] buffer, int length); In the caller: // Java int bufferLength = 100; byte[] buffer = new byte[bufferLength]; // C char -> Java byte fillStringBuffer(buffer, bufferLength); String bufferContents = Native.toString(buffer);

(For wchar_t*, use char[])

4. To receive a strin from a pointer to string: // C void fillStringPointer(char** pointerToString); void fillStringPointer(char* &pointerToString); // same thing

you'll pass a pointer by reference // Java void fillStringPointer(PointerByReference pointerToString);

and in the caller: // Java PointerByReference myPointerToString = new PointerByReference(); fillStringPointer(myPointerToString); Pointer stringBuffer = myPointerToString.getValue(); String bufferContents = stringBuffer.getString( /*offset*/ 0);

Landon Williams wrote:

I'm struggling here. I've gone though the site and mailing lists, and have a large portion of my application working, but am stumbling to connect the dots here.

First off, I'm a Java developer and haven't been in C in years, so I don't remember much of anything. I don't grasp the language well anymore, so any examples would be extremely helpfull.

I have three char *data types, and I need to get those back to my java app. They are Strings. Outside of that, I really don't care how they get back, such that it happens. This doesn't have to be the highest efficiency application in the world, so I have options.

Here's where I left off testing with my C app:

int readData(char *alpha, char *beta, char *gamma) {

executeCommand3(port,baud,&alpha); executeCommand3(port,baud,&beta); executeCommand3(port,baud,&gamma); }

I saw something in the forums about needing to make it a pointer with a size integer. I honestly don't know where to begin... Or how I would even get that back to a String on the Java side.

Thanks in advanced for everyone's help. -L