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 9:24:59 am
List:net.java.dev.jna.users

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