14 messages in net.java.dev.jna.usersRe: [jna-users] Problem Regarding Str...
FromSent OnAttachments
Muhammad NoorApr 6, 2009 12:16 am.dll, .log
Timothy WallApr 6, 2009 3:49 am 
Muhammad NoorApr 6, 2009 4:11 am 
Timothy WallApr 6, 2009 5:32 am 
Muhammad NoorApr 6, 2009 5:45 am 
Timothy WallApr 6, 2009 6:13 am 
Muhammad NoorApr 6, 2009 6:22 am 
Timothy WallApr 6, 2009 7:01 am 
Hristo NovatchkovMay 3, 2009 11:55 am 
Timothy WallMay 3, 2009 12:56 pm 
Hristo NovatchkovMay 3, 2009 1:17 pm 
Timothy WallMay 3, 2009 5:57 pm 
Novatchkov HristoMay 4, 2009 4:05 am 
Timothy WallMay 4, 2009 4:21 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] Problem Regarding Structure that includes Function PointersActions...
From:Timothy Wall (twal@dev.java.net)
Date:Apr 6, 2009 6:13:27 am
List:net.java.dev.jna.users

Why are you requesting a specific structure size? Let JNA do the calculation.

If you need the Structure to be a specific size, use Structure.useMemory(new Memory(size)) instead. It appears that the size-based ctors of Structure are non-functional with anything other than Structure.CALCULATE_SIZE.

On Apr 6, 2009, at 8:45 AM, Muhammad Noor wrote:

I have tested my application by implementing StdCallLibrary and StdCallCallback. Moreover i have increased the size of my structure to 1024 bytes but still get same error. It would be more appriciated if you can provide me a working example.

On 4/6/09, Timothy Wall <twal@dev.java.net> wrote: While some things may happen to work using the incorrect calling convention, this is the sort of behavior you are likely to get.

You library should implement StdCallLibrary and your callback StdCallCallback.

Your structure is only two bytes in size, which is not large enough to hold its two fields. The behavior of a structure in such an instance is undefined.

On Apr 6, 2009, at 7:11 AM, Muhammad Noor wrote:

Hi Mr. Timothy Wall

I have tried to my example by using StdCallLibrary and StdCallLibrary but I am still facing same problem. Please note my last exposed method which has been executed successfully. I just want to know that why it does not works when I pass structure as argument.

Regards Noor.

On 4/6/09, Timothy Wall <twal@dev.java.net> wrote: Your C code uses the stdcall calling convention. Your JNA library mapping and its callbacks need to do the same (https://jna.dev.java.net/ #w32crash).

You're also unnecessarily assigning your structure a size of two bytes, which is not enough for two function pointers.

On Apr 6, 2009, at 3:17 AM, Muhammad Noor wrote:

To whom it may concern.

Hi,

I am using JNA for accessing DLL functions directly from JAVA, but I am stuck in a situation when I have to access a method that takes structure as a parameter which includes Function Pointers in it.

My problem is that JNA does not call my callback functions when their function pointers are part of a structure which is then passed as an argument to a C++ function.

In order to get a clearer picture of my problem please have a look at the code.

===================C++ Header File=================== typedef int (__stdcall *StringRevFunction) (); typedef int (__stdcall *StringAscFunction) (int val);

typedef struct TUssdCallbacks { StringAscFunction stringAscFunction; StringRevFunction stringRevFunction; };

extern "C" __declspec(dllexport) void RegisterStringCallbackFunc(TUssdCallbacks tUssdCallbacks);

===================C++ Implementation File=================== #include "stdafx.h" #include "CCallbackFunction.h"

extern "C" __declspec(dllexport) void RegisterStringCallbackFunc(TUssdCallbacks tUssdCallbacks){

(*tUssdCallbacks.stringAscFunction)((int)8);

(*tUssdCallbacks.stringRevFunction)(); }

===================Java Interface definition file===================

import com.sun.jna.Callback; import com.sun.jna.Library; import com.sun.jna.Native; import com.sun.jna.Structure;

public interface CCallbackFunction extends Library{

CCallbackFunction INSTANCE = (CCallbackFunction) Native.loadLibrary("CCallbackFunction",CCallbackFunction.class);

public static class TUssdCallbacks extends Structure{

public TUssdCallbacks (StringRevFunction stringRevFunction,StringAscFunction stringAscFunction){ super(2,ALIGN_DEFAULT); this.stringAscFunction = stringAscFunction; this.stringRevFunction = stringRevFunction; }

public StringRevFunction stringRevFunction; public StringAscFunction stringAscFunction; }

public void RegisterStringCallbackFunc(TUssdCallbacks tUssdCallbacks);

}

===================Java Main File===================

import com.sun.jna.Function;

public class Test {

public static void main(String[] args) { System.setProperty("jna.library.path","C:\\lib\\"); String s = System.getProperty("jna.library.path"); System.out.println( "Jna.library.path = "+s );

CCallbackFunction.StringAscFunction stringAscFunction = new CCallbackFunction.StringAscFunction(){

public int invoke() { System.out.println("------**-------"); return 0; }

};

CCallbackFunction.StringRevFunction stringRevFunction = new CCallbackFunction.StringRevFunction(){

public int invoke(int val) { System.out.println("-------**------"+val); return 0; }

};

CCallbackFunction.TUssdCallbacks callbacks = new CCallbackFunction.TUssdCallbacks(stringRevFunction,stringAscFunction);

CCallbackFunction.INSTANCE.RegisterStringCallbackFunc(callbacks);

}

}

The last highlighted line results in following error.

# # An unexpected error has been detected by Java Runtime Environment: # # EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x02dc2610, pid=3380, tid=5456 # # Java VM: Java HotSpot(TM) Client VM (11.0-b15 mixed mode, sharing windows-x86) # Problematic frame: # C 0x02dc2610 # # An error report file with more information is saved as: # D:\workspace\JavaJNA\hs_err_pid3380.log # # If you would like to submit a bug report, please visit: # http://java.sun.com/webapps/bugreport/crash.jsp # The crash happened outside the Java Virtual Machine in native code. # See problematic frame for where to report the bug. #

NOTE:

I have accessed same DLL in C# and it successfully calls my call back function. Moreover I have exposed another function in DLL with all the Function pointers as method parameter and then within the function I have initialized the structure with Function Pointer values and called same method which results in successful call to my callback functions that I have defined in JAVA.

The new exposed function is as follows.

extern "C" __declspec(dllexport) void RegisterStrCallbackFunc(StringRevFunction stringRevFunction,StringAscFunction stringAscFunction){ struct TUssdCallbacks tUssdCallbacks = { stringAscFunction,stringRevFunction};

RegisterStringCallbackFunc(tUssdCallbacks); }

Kindly guied me is it possible to register my callback functions using structure. If it is then please tell where am I doing wrong.

Thanking you in anticipation.

Regards,

Muhammad Noor-ul-haque

You can call me just noor.

< CCallbackFunction .dll

< hs_err_pid3380 .log

To unsubscribe, e-mail: user@jna.dev.java.net For additional commands, e-mail: user@jna.dev.java.net