Mapeamento Struct em C x Structure em Java com JNA

Boa noite a todos.

Estou com problema de mapeamento ao copiar dados de uma struct contida em uma DLL para uma classe estendida de Structure, da API JNA. A DLL, chamada testeEstrutura, apenas atribui valores inteiros para os campos e retorna a estrutura. Os campos são setados corretamente, mas na linha de retorno da estrutura, ocorre a a exceção interna.

  • Seguem códigos do programa:

1 - Classe estendida de Structure:

import com.sun.jna.Structure;

public class Str1 extends Structure {
	public int a;
	public int b;
}

2 - Interface EstruturaDLL:

import com.sun.jna.win32.StdCallLibrary;

public interface EstruturaDLL extends StdCallLibrary{
	public Str1 FuncaoTeste();
}

3 - Classe responsável por carregar a DLL e acessar a função:

import com.sun.jna.Native;

public class AcessoEstruturaDLL {
	private static EstruturaDLL estruturaDLL = null;
	
	public AcessoEstruturaDLL(){
		estruturaDLL = (EstruturaDLL) Native.loadLibrary("testeEstrutura", EstruturaDLL.class);
	}
	
	public Str1 chamarFuncaoTeste(){
		return estruturaDLL.FuncaoTeste();
	}
}

4 - Classe de teste da DLL:

public class TesteEstruturaDLL {
	private static AcessoEstruturaDLL acessoEstruturaDLL = new AcessoEstruturaDLL();
	
	public static void main(String[] args) {
		//Aqui ocorre exceção interna (VIOLATION_EXCEPTION)
		Str1 str1 = acessoEstruturaDLL.chamarFuncaoTeste();
		
		System.out.println(str1.a);
		System.out.println(str1.b);
	}
}
  • Código da DLL

1- Struct:

struct Str1{
       int a,b;
}; 

2 - Função que utiliza a struct:

Str1 FuncaoTeste() { 
   struct Str1 info;
   info.a=1;
   Beep(500,1000);
   info.b=2;
   Beep(1000,1000);
   return info; 
}
  • Log da exceção:
#
# An unexpected error has been detected by HotSpot Virtual Machine:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x75f49922, pid=4528, tid=5708
#
# Java VM: Java HotSpot(TM) Client VM (1.5.0_12-b04 mixed mode, sharing)
# Problematic frame:
# C  [MSVCRT.dll+0x9922]
#
# An error report file with more information is saved as hs_err_pid4528.log
#
# If you would like to submit a bug report, please visit:
#   http://java.sun.com/webapps/bugreport/crash.jsp
#

Como podem ver, a classe Str1 que corresponde a struct da DLL é simples e possui apenas dois atributos inteiros. Por que está havendo incompatibilidade entre as estruturas durante o mapeamento?

Obrigado a todos.

Flávio

Bom dia Flávio!

Eu não entendo desse esquema de JNA, mas dê um olhada nisso.

struct Str1 info; info.a=1; Beep(500,1000); info.b=2; Beep(1000,1000); return info;

Você não precisaria dar um create para o info?

Att,
Patricia