Mapeamento Structs em C x Structure em Java com JNA

1 resposta
F

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.

1 Resposta

F

Olá, pessoal.

Consegui encontrar uma alternativa para contornar o problema. Seguem as alterações:

1 - FuncaoTeste agora recebe parâmetro e passa a retornar void

import com.sun.jna.win32.StdCallLibrary;

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

2 - O mesmo ocorre com a DLL em C

void FuncaoTeste(&info) {   
      info.a=1;  
      Beep(500,1000);  
      info.b=2;  
      Beep(1000,1000);  
}

3 - Por fim, posso acessar sem problemas a estrutura modificada dentro da DLL

public class TesteEstruturaDLL {
	private static AcessoEstruturaDLL acessoDLL = new AcessoEstruturaDLL();
	
	public static void main(String[] args) {
		Str1 str1 = new Str1();
			
		acessoDLL.chamarFuncaoTeste(str1);
		
		System.out.println(str1.a);
		System.out.println(str1.b);
	}
}

Espero poder retribuir a ajuda que sempre tenho aqui ajudando alguém que venha a passar pelo mesmo problema.

Abraço e obrigado.

Criado 29 de abril de 2008
Ultima resposta 1 de mai. de 2008
Respostas 1
Participantes 1