[JNA] - Pointeur de structure dans une autre structure
Bonjour à tous :),
Je suis sûr que vous avez tout compris en lisant le titre :D.
Je vais vous mettre d'abord le code c et java pour situer d'abord l'endroit de mon problème.
Ne faites pas attention au commentaire, nom, ect... On me donne un Header que je dois respecter et j'ai juste un peu modifier pour faire de simple test ;).
Mon.h
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
#ifndef TESTSTRUCT_H_INCLUDED
#define TESTSTRUCT_H_INCLUDED
#include <stdlib.h>
#include <stdio.h>
typedef signed char INT8;
typedef short INT16;
typedef int INT32;
typedef unsigned char UINT8;
typedef unsigned short UINT16;
typedef unsigned int UINT32;
/*typedef union {
INT32 i;
UINT32 u;
float f;
char * s;
} Value;*/
typedef struct IoDyn {
UINT8 def:1, // io defined
init:1; // when server: value received from client
//Value value;
} IoDyn;
typedef struct {
UINT8 out:1; // direction in=0, out=1 (in=server->client; out=client->server)
UINT32 ioAddress; // protocol address (TGAM/USC 0x0000ggio (gg=group=1..64; io=1..))
UINT8 ioCount;
UINT8 ioPerAddress;
UINT8 ioFunction;
IoDyn *ioDynP; // !!! Mon problème !!!
} IoBloc;
// access low level driver functions
char* DrvTx (IoBloc * rtuDP, UINT8 *txbuf,int txlen);
#endif // TESTSTRUCT_H_INCLUDED |
Le point c
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#include "TestStruct.h"
char* DrvTx (IoBloc *rtuDP, UINT8 *txbuf,int txlen){
char* temp = (char*)malloc(sizeof(char)*255);
sprintf(temp,"\n test %u , %d , %d",rtuDP->ioAddress,rtuDP->out,rtuDP->ioDynP->init);//
printf("\n\n c %s \n",temp);
rtuDP->ioAddress = 2;
return temp;
}
/*int main (void){
IoBloc rt;
rt.ioAddress = 5;
rt.out = 0x00;
IoDyn io;
rt.ioDynP = io;
rt.ioDynP.init = 0xFF;
char *temp = DrvTx(&rt,NULL,5);
printf("%u",rt.ioAddress);
return 1;
}*/ |
Mtn code java :
Class contenant mon interface
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
public class StructureJna {
static {
System.setProperty("jna.library.path","Libraries");
}
private MyLibrary lib ;
public StructureJna(){
lib = (MyLibrary) Native.loadLibrary("Structure",Platform.isWindows() ? MyWin.class : MyLibrary.class);
}
public MyLibrary getLib() {
return lib;
}
public interface MyLibrary extends com.sun.jna.Library {
public String DrvTx(IoBloc rtuDP,byte txbuf, int txlen);
/*public static interface Value{
public int i = 0;
public int u = 0;
public float f = (float) 0.0;
public String s = "";
}*/
public static class IoDyn extends com.sun.jna.Structure{
//public static class ByIoDyn extends IoDyn implements com.sun.jna.Structure.ByValue { };
public static class ByReferenceDyn extends IoDyn implements com.sun.jna.Structure.ByReference{};
public boolean def, init;
// public String value = Value.s;
}
public static class IoBloc extends com.sun.jna.Structure{
//public static class ByIoBloc extends IoBloc implements com.sun.jna.Structure.ByValue { };
public static class ByReferenceBloc extends IoBloc implements com.sun.jna.Structure.ByReference{};
public boolean out;
public int ioAddress = 0;
public byte ioCount = 0;
public byte ioPerAddress = 0;
public byte ioFunction = 0;
public ByIoDyn ioDynp = new ByIoDyn();
}
public interface MyWin extends MyLibrary{}
}
} |
Class contenant le main
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
public class Structure {
public static void main(String[] args){
StructureJna s = new StructureJna();
ByReferenceBloc ioBloc = new ByReferenceBloc ();
ioBloc.ioAddress = 5;
ioBloc.out = true;
ioBloc.ioDynp.init = true;
System.out.print("\n\n " + s.getLib().DrvTx(ioBloc, ioBloc.ioPerAddress, 5) + "\n");
System.out.print("\n\n " + ioBloc.ioAddress + "\n");
}
} |
Donc voilà mon problème :
La Structure IOBloc contient un pointeur vers la structure IoDyn.
Alors Si je ne le mes plus IoDyn en pointeur ou si je ne touche pas à la variable, cela marche mais si je laisse en pointeur, j'ai le droit à une erreur détècté par la JRE :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x662011f3, pid=2332, tid=3264
#
# JRE version: 6.0_25-b06
# Java VM: Java HotSpot(TM) Client VM (20.0-b11 mixed mode, sharing windows-x86 )
# Problematic frame:
# C [Structure.dll+0x11f3]
#
# If you would like to submit a bug report, please visit:
# <a href="http://java.sun.com/webapps/bugreport/crash.jsp" target="_blank">http://java.sun.com/webapps/bugreport/crash.jsp</a>
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
# |
Alors voilà, je dois respecter le header, donc est-ce que quelqu'un à une idée?
Sinon aussi c'est dommage que quand je passe en paramètre ma structure par pointeur et que je modifie cette structure dans une fonction C, je ne retrouve pas les modifications après en Java. C'est sans doute normal, faut peut-être pas que j'en demande de trop ^^. ou non?
Merci d'avance à tous.
Et j'espère que je me suis bien fait comprendre, sinon veuillez m'en excuser :)