2 messages in net.java.dev.jna.users[jna-users] Help Regarding Callbacks ...
FromSent OnAttachments
Raghavendra SeshadriApr 2, 2009 8:36 pm 
Timothy WallApr 3, 2009 10:03 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] Help Regarding Callbacks in JNAActions...
From:Raghavendra Seshadri (Ragh@mindtree.com)
Date:Apr 2, 2009 8:36:29 pm
List:net.java.dev.jna.users

Hi, thanks for your response, but actually I went with another approach, that is
invoking the Macros or C preprocessor statements.

But not able to get it working. We tried with InvocationMapper.

Here is the macro definition

#define AddParameter(array, i, c, a, s) { array[i].value = sizeof(PARAMETER);
array [i].code = c; array [i].size = s; array [i].att = a; i++; }

So I need to invoke this macro from my JNA code, so that the variables are set
properly, Could you just explain what may be the JNA mapping for the same.

Well we have tried with the following JNA code

As per the documentation given here
(https://jna.dev.java.net/javadoc/com/sun/jna/InvocationMapper.html) , I m
loading the library with options. Is this the right way of invoking the macro?
Actually I get an error saying there is no Function named "AddParameter" in the
library. I m not sure what is the behavior of this InvocationMapper.

ParameterInvocationMapper mapper = new ParameterInvocationMapper();

options.put(Library.OPTION_INVOCATION_MAPPER, mapper);

TestLibrary INSTANCE = (TestLibrary)com.sun.jna.Native.loadLibrary("myLibrary",
TestLibrary.class, options);

/*The invocation mapper is defined here*/

public class ParameterInvocationMapper implements InvocationMapper {

public InvocationHandler getInvocationHandler(NativeLibrary lib, Method m) {

if (m.getName().equals("AddParameter")) {

final Function f = lib.getFunction("AddParameter");

return new InvocationHandler() {

public Object invoke(Object proxy, Method method, Object[] args) {

f.invoke(args);

return null;

}

};

}

return null;

}

}

Thanks again! :)

Regards,

JK

M: 9845955933

"If you are good at something, never do it for free!"

-----Original Message----- From: Timothy Wall [mailto:twal@dev.java.net] Sent: Thursday, April 02, 2009 6:00 PM To: Venkatesh Krishnamurthy Joise Cc: Raghavendra Seshadri; use@jna.dev.java.net Subject: Re: IMP: Help Regarding Callbacks in JNA

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