1 message in net.java.dev.jna.users[jna-users] Re: IMP: Help Regarding C...
FromSent OnAttachments
Timothy WallApr 2, 2009 5:29 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] Re: IMP: Help Regarding Callbacks in JNAActions...
From:Timothy Wall (twal@dev.java.net)
Date:Apr 2, 2009 5:29:04 am
List:net.java.dev.jna.users

Simply change the type of the "attributes" field to be the same type as your callback and assign it directly. If you need to call methods with different variations of the PARAMETER struct, you can define multiple versions of the method, each of which takes a different variation of the PARAMETER struct.

Or you can define a base PARAMETER struct which omits the "attributes" field and make subclasses which define only the "attributes" field.

On Apr 2, 2009, at 1:51 AM, Venkatesh Krishnamurthy Joise wrote:

Hi, Sorry for spamming, I got your email Id through JNA user group, my scenario is a bit more complex thought of writing to you directly, It would be great if you could give some hints on how to resolve this issue:-

Right now facing problems with Callbacks, we have the following scenario:-

----------------------------------------------------------- C Code

------------------------------------------------------------

typedef struct { WORD structure_size; WORD parameter_code; /* Parameter code number. */ DWORD attributes_size; /* Size of following attributes. */ void *attributes; /* Pointer to attributes. Type of attributes inferred from parameter_code. */ } PARAMETER;

There is a callback which is globally declared in the JNI/C code:-

WORD LIBFUNC API_Callback(HSCANNER, MESSAGE, PARAM1, PARAM2 ); WORD LIBFUNC API_Callback(HSCANNER hengine, MESSAGE message, PARAM1 p1, PARAM2 p2) { WORD returnvalue = 0; engineOutput * myEngineOutput; printf("Message in Callback: %d" , message);

return returnvalue; }

----------------------------------------------------------- JNA MAPPING Code

------------------------------------------------------------ public static class PARAMETER extends com.sun.jna.Structure { /// Allocate a new PARAMETER struct on the heap public PARAMETER () {} /// Cast data at given memory location (pointer + offset) as an existing PARAMETER struct public PARAMETER (com.sun.jna.Pointer pointer, int offset) { super(); useMemory(pointer, offset); read(); } /// Create an instance that shares its memory with another PARAMETER instance public PARAMETER (PARAMETER struct) { this(struct.getPointer(), 0); } public static class ByReference extends PARAMETER implements com.sun.jna.Structure.ByReference { /// Allocate a new PARAMETER.ByRef struct on the heap public ByReference() {} /// Create an instance that shares its memory with another PARAMETER instance public ByReference(PARAMETER struct) { super(struct.getPointer(), 0); } } public static class ByValue extends PARAMETER implements com.sun.jna.Structure.ByValue { /// Allocate a new PARAMETER.ByVal struct on the heap public ByValue() {} /// Create an instance that shares its memory with another PARAMETER instance public ByValue(PARAMETER struct) { super(struct.getPointer(), 0); } } public short structure_size; /// Parameter code number. public short parameter_code; /// Size of following attributes. public com.sun.jna.NativeLong attributes_size; /// Pointer to attributes. Type of attributes inferred from parameter_code. public com.sun.jna.Pointer attributes; // Type this can hold any data } /** * Type of scanning engine callback function.<br> */ public interface CALLBACKTYPE_V1_callback extends com.sun.jna.Callback { /// Original signature : <code>LIBRET CALLBACKTYPE_V1_callback(HSCANNER, MESSAGE, PARAM1, PARAM2_V1)</code> short invoke(com.sun.jna.Pointer HSCANNER11, short MESSAGE12, short PARAM113, com.sun.jna.NativeLong PARAM2_V114); } public class APICallbackImpl implements TestLibrary.CALLBACKTYPE_V1_callback { public short invoke(Pointer HSCANNER11, short MESSAGE12, short PARAM113, NativeLong PARAM2_V114) { return 123;///just for testing } }

Question: How to set the pointer to the callback directly into PARAMETER structure dynamically, we have a void* attributes field which is used, in the current JNI implementation we have a MACRO which actually adds the callback method pointer to this attributes field. But how can we replicate this in JNA implementation? Right now, it is a very critical issue and it is a showstopper for us to proceed further, we browsed over and over again, but couldn’t find anything useful.

And also if possible, if you could give us how to debug JNA/C code through eclipse or someother tool it would be helpful.

Thanks in advance! J

Regards, JK M: 9845955933 "If you are good at something, never do it for free!"

http://www.mindtree.com/email/disclaimer.html