Carregando DLL em java com jna

Boa tarde pessoal! Estou fazendo uns testes com jna e estou com um problema.
Olhem meus arquivos:

Interface DLL:

package com.testes.interfaces;

import com.sun.jna.Library;

public interface DLL extends Library {
	void say(String frase);
}

Classe Main:

package com.testes.main;

import com.sun.jna.Native;
import com.testes.interfaces.DLL;

public class TestesDLL {
	public static void main(String[] args) {
		System.out.println("Carregando...");
		DLL lib = (DLL) Native.loadLibrary("TestesDLL", DLL.class);
		lib.say("Hello Word");
	}
}

Arquivo do delphi que gera o dll:

library TestesDLL;

{ Important note about DLL memory management: ShareMem must be the
  first unit in your library's USES clause AND your project's (select
  Project-View Source) USES clause if your DLL exports any procedures or
  functions that pass strings as parameters or function results. This
  applies to all strings passed to and from your DLL--even those that
  are nested in records and classes. ShareMem is the interface unit to
  the BORLNDMM.DLL shared memory manager, which must be deployed along
  with your DLL. To avoid using BORLNDMM.DLL, pass string information
  using PChar or ShortString parameters. }

uses
  SysUtils,
  Classes;

{$R *.res}

procedure say(var frase: String);
begin
  writeln('DLL say: ' + frase);
end;

exports
say;

begin
end.

A saída do programa é:

Carregando...
#
# An unexpected error has been detected by Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x18d82859, pid=5844, tid=5828
#
# Java VM: Java HotSpot(TM) Client VM (11.3-b02 mixed mode windows-x86)
# Problematic frame:
# C  [TestesDLL.dll+0x2859]
#
# An error report file with more information is saved as:
# C:\Programas\Java\TestesDLL\hs_err_pid5844.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.
#

Arquivo hs_err_pid5844.log:

#
# An unexpected error has been detected by Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x18d82859, pid=5844, tid=5828
#
# Java VM: Java HotSpot(TM) Client VM (11.3-b02 mixed mode windows-x86)
# Problematic frame:
# C  [TestesDLL.dll+0x2859]
#
# 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.
#

---------------  T H R E A D  ---------------

Current thread (0x02459800):  JavaThread "main" [_thread_in_native, id=5828, stack(0x02320000,0x02370000)]

siginfo: ExceptionCode=0xc0000005, reading address 0x0855fffe

Registers:
EAX=0x0d00b1b2, EBX=0x0236f818, ECX=0x0214f9dc, EDX=0x191a0aed
ESP=0x0236f7ec, EBP=0x0236f81c, ESI=0x0855fffe, EDI=0x216df25b
EIP=0x18d82859, EFLAGS=0x00010607

Top of Stack: (sp=0x0236f7ec)
0x0236f7ec:   00000009 00021890 18d8443c 0236f8f0
0x0236f7fc:   00000000 0236f824 18d98a31 0236fd9c
0x0236f80c:   18d98a5e 0236f81c 00000000 191a0ae4
0x0236f81c:   0236f828 1914cb67 00021890 0236f868
0x0236f82c:   1914c782 1914c560 0236f850 00000004
0x0236f83c:   00000000 0236faf0 18d98a0c 0236f868
0x0236f84c:   1914134b 0236fa90 0236faf0 0236f890
0x0236f85c:   0236f8b0 0236f8b0 00000001 0236fad8 

Instructions: (pc=0x18d82859)
0x18d82849:   5e c3 8d 74 31 fc 8d 7c 39 fc c1 f9 02 78 11 fd
0x18d82859:   f3 a5 89 c1 83 e1 03 83 c6 03 83 c7 03 f3 a4 fc 


Stack: [0x02320000,0x02370000],  sp=0x0236f7ec,  free space=317k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
C  [TestesDLL.dll+0x2859]
C  [jna2385734997951731215.dll+0xcb67]
C  [jna2385734997951731215.dll+0xc782]
C  [jna2385734997951731215.dll+0x44ae]
C  [jna2385734997951731215.dll+0x4c8e]
j  com.sun.jna.Native.invokeVoid(JI[Ljava/lang/Object;)V+0
j  com.sun.jna.Function.invoke([Ljava/lang/Object;Ljava/lang/Class;Z)Ljava/lang/Object;+48
j  com.sun.jna.Function.invoke(Ljava/lang/Class;[Ljava/lang/Object;Ljava/util/Map;)Ljava/lang/Object;+214
j  com.sun.jna.Library$Handler.invoke(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;+341
j  $Proxy0.say(Ljava/lang/String;)V+16
j  com.testes.main.TestesDLL.main([Ljava/lang/String;)V+22
v  ~StubRoutines::call_stub

Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j  com.sun.jna.Native.invokeVoid(JI[Ljava/lang/Object;)V+0
j  com.sun.jna.Function.invoke([Ljava/lang/Object;Ljava/lang/Class;Z)Ljava/lang/Object;+48
j  com.sun.jna.Function.invoke(Ljava/lang/Class;[Ljava/lang/Object;Ljava/util/Map;)Ljava/lang/Object;+214
j  com.sun.jna.Library$Handler.invoke(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;+341
j  $Proxy0.say(Ljava/lang/String;)V+16
j  com.testes.main.TestesDLL.main([Ljava/lang/String;)V+22
v  ~StubRoutines::call_stub

---------------  P R O C E S S  ---------------

Java Threads: ( => current thread )
  0x02516c00 JavaThread "Low Memory Detector" daemon [_thread_blocked, id=1352, stack(0x189b0000,0x18a00000)]
  0x0250f800 JavaThread "CompilerThread0" daemon [_thread_blocked, id=5192, stack(0x18920000,0x18970000)]
  0x0250d000 JavaThread "Attach Listener" daemon [_thread_blocked, id=1484, stack(0x18890000,0x188e0000)]
  0x02502800 JavaThread "Signal Dispatcher" daemon [_thread_blocked, id=4460, stack(0x18800000,0x18850000)]
  0x024f1400 JavaThread "Finalizer" daemon [_thread_blocked, id=824, stack(0x18770000,0x187c0000)]
  0x024ecc00 JavaThread "Reference Handler" daemon [_thread_blocked, id=5964, stack(0x186e0000,0x18730000)]
=>0x02459800 JavaThread "main" [_thread_in_native, id=5828, stack(0x02320000,0x02370000)]

Other Threads:
  0x024e9400 VMThread [stack: 0x023f0000,0x02440000] [id=5020]
  0x02517800 WatcherThread [stack: 0x18a40000,0x18a90000] [id=1200]

VM state:not at safepoint (normal execution)

VM Mutex/Monitor currently owned by a thread: None

Heap
 def new generation   total 960K, used 580K [0x04560000, 0x04660000, 0x04a40000)
  eden space 896K,  57% used [0x04560000, 0x045e10c0, 0x04640000)
  from space 64K, 100% used [0x04650000, 0x04660000, 0x04660000)
  to   space 64K,   0% used [0x04640000, 0x04640000, 0x04650000)
 tenured generation   total 4096K, used 122K [0x04a40000, 0x04e40000, 0x08560000)
   the space 4096K,   2% used [0x04a40000, 0x04a5ea78, 0x04a5ec00, 0x04e40000)
 compacting perm gen  total 262144K, used 3028K [0x08560000, 0x18560000, 0x18560000)
   the space 262144K,   1% used [0x08560000, 0x088552a0, 0x08855400, 0x18560000)
No shared spaces configured.

Dynamic libraries:
0x00400000 - 0x00424000 	C:\Users\occ\AppData\Local\Genuitec\Common\binary\com.sun.java.jdk.win32.x86_1.6.0.013\bin\javaw.exe
0x77240000 - 0x773c0000 	C:\Windows\SysWOW64\ntdll.dll
0x74ac0000 - 0x74bd0000 	C:\Windows\syswow64\kernel32.dll
0x76930000 - 0x76976000 	C:\Windows\syswow64\KERNELBASE.dll
0x75230000 - 0x752d0000 	C:\Windows\syswow64\ADVAPI32.dll
0x763f0000 - 0x7649c000 	C:\Windows\syswow64\msvcrt.dll
0x764a0000 - 0x764b9000 	C:\Windows\SysWOW64\sechost.dll
0x75350000 - 0x75440000 	C:\Windows\syswow64\RPCRT4.dll
0x74920000 - 0x74980000 	C:\Windows\syswow64\SspiCli.dll
0x74910000 - 0x7491c000 	C:\Windows\syswow64\CRYPTBASE.dll
0x74c20000 - 0x74d20000 	C:\Windows\syswow64\USER32.dll
0x75040000 - 0x750d0000 	C:\Windows\syswow64\GDI32.dll
0x74d50000 - 0x74d5a000 	C:\Windows\syswow64\LPK.dll
0x74d60000 - 0x74dfd000 	C:\Windows\syswow64\USP10.dll
0x76390000 - 0x763f0000 	C:\Windows\system32\IMM32.DLL
0x76830000 - 0x768fc000 	C:\Windows\syswow64\MSCTF.dll
0x7c340000 - 0x7c396000 	C:\Users\occ\AppData\Local\Genuitec\Common\binary\com.sun.java.jdk.win32.x86_1.6.0.013\jre\bin\msvcr71.dll
0x6d8b0000 - 0x6db06000 	C:\Users\occ\AppData\Local\Genuitec\Common\binary\com.sun.java.jdk.win32.x86_1.6.0.013\jre\bin\client\jvm.dll
0x72940000 - 0x72972000 	C:\Windows\system32\WINMM.dll
0x73ca0000 - 0x73cec000 	C:\Windows\system32\apphelp.dll
0x6d340000 - 0x6d348000 	C:\Users\occ\AppData\Local\Genuitec\Common\binary\com.sun.java.jdk.win32.x86_1.6.0.013\jre\bin\hpi.dll
0x76700000 - 0x76705000 	C:\Windows\syswow64\PSAPI.DLL
0x6d860000 - 0x6d86c000 	C:\Users\occ\AppData\Local\Genuitec\Common\binary\com.sun.java.jdk.win32.x86_1.6.0.013\jre\bin\verify.dll
0x6d3e0000 - 0x6d3ff000 	C:\Users\occ\AppData\Local\Genuitec\Common\binary\com.sun.java.jdk.win32.x86_1.6.0.013\jre\bin\java.dll
0x6d8a0000 - 0x6d8af000 	C:\Users\occ\AppData\Local\Genuitec\Common\binary\com.sun.java.jdk.win32.x86_1.6.0.013\jre\bin\zip.dll
0x743d0000 - 0x743e6000 	C:\Windows\system32\CRYPTSP.dll
0x74390000 - 0x743cb000 	C:\Windows\system32\rsaenh.dll
0x71fe0000 - 0x71ff7000 	C:\Windows\system32\USERENV.dll
0x72040000 - 0x7204b000 	C:\Windows\system32\profapi.dll
0x6d6c0000 - 0x6d6d3000 	C:\Users\occ\AppData\Local\Genuitec\Common\binary\com.sun.java.jdk.win32.x86_1.6.0.013\jre\bin\net.dll
0x74fa0000 - 0x74fd5000 	C:\Windows\syswow64\WS2_32.dll
0x75720000 - 0x75726000 	C:\Windows\syswow64\NSI.dll
0x71f70000 - 0x71fac000 	C:\Windows\system32\mswsock.dll
0x73540000 - 0x73546000 	C:\Windows\System32\wship6.dll
0x73ab0000 - 0x73ac0000 	C:\Windows\system32\NLAapi.dll
0x73aa0000 - 0x73ab0000 	C:\Windows\system32\napinsp.dll
0x73a80000 - 0x73a92000 	C:\Windows\system32\pnrpnsp.dll
0x73a70000 - 0x73a7d000 	C:\Windows\system32\wshbth.dll
0x72d70000 - 0x72db4000 	C:\Windows\system32\DNSAPI.dll
0x73a60000 - 0x73a68000 	C:\Windows\System32\winrnr.dll
0x73a30000 - 0x73a57000 	C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live\WLIDNSP.DLL
0x74fe0000 - 0x75037000 	C:\Windows\syswow64\SHLWAPI.dll
0x73550000 - 0x73571000 	C:\Program Files (x86)\Bonjour\mdnsNSP.dll
0x74830000 - 0x7484c000 	C:\Windows\system32\Iphlpapi.DLL
0x74820000 - 0x74827000 	C:\Windows\system32\WINNSI.DLL
0x71f60000 - 0x71f65000 	C:\Windows\System32\wshtcpip.dll
0x73530000 - 0x73536000 	C:\Windows\system32\rasadhlp.dll
0x726a0000 - 0x726d8000 	C:\Windows\System32\fwpuclnt.dll
0x19140000 - 0x19195000 	C:\Users\occ\AppData\Local\Temp\jna2385734997951731215.dll
0x18d80000 - 0x18da1000 	C:\Programas\Java\TestesDLL\TestesDLL.dll
0x76670000 - 0x766ff000 	C:\Windows\syswow64\oleaut32.dll
0x750d0000 - 0x7522c000 	C:\Windows\syswow64\ole32.dll

VM Arguments:
jvm_args: -XX:PermSize=256m -XX:MaxPermSize=256m -Dfile.encoding=Cp1252 
java_command: com.testes.main.TestesDLL
Launcher Type: SUN_STANDARD

Environment Variables:
JAVA_HOME=C:\Program Files\Java\jdk1.7.0
CLASSPATH=.;C:\Program Files (x86)\Java\jre6\lib\ext\QTJava.zip
PATH=C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\Borland\Delphi7\Bin;C:\Program Files (x86)\Borland\Delphi7\Projects\Bpl\;C:\Program Files (x86)\CodeGear\RAD Studio\6.0\bin;C:\Users\Public\Documents\RAD Studio\6.0\Bpl;C:\Inprise\vbroker\bin;C:\Windows\SYSTEM32;C:\Windows;C:\Windows\SYSTEM32\WBEM;C:\Windows\SYSTEM32\WINDOWSPOWERSHELL\V1.0\;C:\PROGRA~2\Borland\Delphi6\Bin;C:\PROGRA~2\Borland\Delphi6\Projects\Bpl;C:\Program Files (x86)\Windows Live\Shared;C:\Program Files\TortoiseSVN\bin;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Borland\BCC55\bin;C:\Program Files (x86)\Microsoft Visual C++ Toolkit 2003\bin;C:\Program Files (x86)\Groovy\Groovy-1.8.4\bin;C:\Program Files (x86)\OpenSSH\bin;C:\Ruby193\bin;C:\Users\Public\Documents\RAD Studio\6.0\Bpl;C:\grails\bin;C:\Program Files\Java\jdk1.7.0\bin;C:\Program Files (x86)\Microsoft Visual C++ Toolkit 2003\bin
USERNAME=occ
OS=Windows_NT
PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 23 Stepping 10, GenuineIntel



---------------  S Y S T E M  ---------------

OS: Windows NT 6.1 Build 7601 Service Pack 1

CPU:total 2 (2 cores per cpu, 1 threads per core) family 6 model 7 stepping 10, cmov, cx8, fxsr, mmx, sse, sse2, sse3, ssse3

Memory: 4k page, physical 4160544k(1547424k free), swap 4194303k(3547040k free)

vm_info: Java HotSpot(TM) Client VM (11.3-b02) for windows-x86 JRE (1.6.0_13-b03), built on Mar  9 2009 01:15:24 by "java_re" with MS VC++ 7.1

time: Mon Jan 09 15:08:01 2012
elapsed time: 0 seconds

Alguma idéia do que pode estar causando o problema??

Obrigado!

Estou começando a achar que esse erro está ocorrendo pela diferença de tamanhos da string em pascal e em java. Acho que teria que utilizar um outro tipo em pascal.

problema é sua dll, no delphi você cria dll que podem somente ser acessadas pelo mesmo, ou por outros app. se você quer aprender jna que é muito simples, pegue uma dll de terceiros ou acesse a win32 api

obs : sua dll tem que estar na c:\windows\system32

Na verdade dá certo o mesmo código só que recebendo um word em delphi e passando int em java com jna.
O problema está na string.

Olá,

estou tendo o mesmo problema. Como você resolver?

obrigado.

também não consegui passa um String para a DLL…