14 messages in net.java.dev.jna.users[jna-users] Problem Regarding Structu...
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:[jna-users] Problem Regarding Structure that includes Function PointersActions...
From:Muhammad Noor (muha@raseen.ae)
Date:Apr 6, 2009 12:16:38 am
List:net.java.dev.jna.users
Attachments:
CCallbackFunction.dll - 51k

*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) voidRegisterStringCallbackFunc(TUssdCallbacks tUssdCallbacks);

===================C++ Implementation File===================

#include "stdafx.h"

#include "CCallbackFunction.h"

extern "C" __declspec(dllexport) voidRegisterStringCallbackFunc(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) voidRegisterStrCallbackFunc(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,

You can call me just noor.