2 messages in net.java.dev.jna.users[jna-users] Structure and JNA
FromSent OnAttachments
Avinash KachhyMar 17, 2009 6:27 am 
Timothy WallMar 17, 2009 7:59 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:[jna-users] Structure and JNAActions...
From:Avinash Kachhy (akac@yahoo.com)
Date:Mar 17, 2009 6:27:32 am
List:net.java.dev.jna.users

Hello

I corrected an error I had before so now I am getting the correct return codes
from the DLL. But, I feel that I am still not passing the pointer to struct
param correctly. Which may be causing the DLL function to fail. Below is the original C def of the function and the structure in the .H file. typedef struct _SETUPPR { CHAR Port[5]; WORD Baud; BYTE Parity; BYTE Data; BYTE Stop; } SETUPPR; typedef SETUPPR FAR *PSETUPPR; WORD WINAPI PRT_OPENCOM (PSETUPPR);

My Java code public class SetupPR extends Structure { public byte[] port = "COM1".getBytes(); public short baud = 9600; public byte parity = 'N'; public byte data = 8; public byte stop = 1; } public interface PR2Interface extends Library { public static SetupPR setup = new SetupPR(); PR2Interface INSTANCE = (PR2Interface) Native.loadLibrary("wpr50nt",
PR2Interface.class); public short PRT_OPENCOM(SetupPR spr); }

in Main() public static void main(String[] args) { short retval;

PR2Interface.INSTANCE.setup.port = "COM1".getBytes(); PR2Interface.INSTANCE.setup.data = 8; PR2Interface.INSTANCE.setup.parity = 'N'; PR2Interface.INSTANCE.setup.stop = 1;

retval = PR2Interface.INSTANCE.PRT_OPENCOM(PR2Interface.INSTANCE.setup); System.out.println("PRT_OPENCOM returned "+retval); }

The last line above prints the error code 610, which means some error in opening
com port. It should have been 0. I do have a com1 port. I am using a USB to
serial adapter and a com1 defined in Win Vista 32 bit. I wonder if there is
anything I did wrong in the above code, in mapping to JNA that would cause the
failure. I have tried the port as a String, too and it has the same result.

I asked this before but then I did not even get valid returns as I mapped the
return code to a byte instead of short.

In addition, I also need confirmation that passing the structure variable setup
is OK and it will be received as a pointer to the PR2Setup structure in the C
DLL. Is it necessary to declare the var. setup in the interface as public static or
could it be declared in main() as a normal var.?

Thanks a lot in advance. Avinash