Hi,
I am having the same problem! I have a C++ DLL that I did not write and do not have the header file for, which I need to access from Java, so I built a C++ Wrapper dll with JNI function headers which calls the the functions in the origional dll. Then I can call the functions in the wrapper dll from Java using native functions. All well and good, until it suddenly stopped working and spits out an EXCEPTION_ACCESS_VIOLATION. I stripped both Java and C++ down as basic as possible, but the problem is here to stay it seems. The error occurs during the C++ to C++ dll function call but only when the WrapperClass dll is called from JNI. Using C++ to call the functions in the wrapper class does not cause any problems or errors. Only when JNI is involved do things get sticky. I have tried everything I can think of, if no one here can help I will have to replace the C++ wrapper dll with a C++ application and use a socket to comunicate with Java! Thats like going squirrel-hunting with hand-grenades but I dont have a choice! I will throw both Java and C++ code in here, as well as the error-message. Please help! All ideas/recommendations will be thankfully received and followed-up-on!
Thanks in advance
Dawud Gordon
WrapperClass.java:
package javapackage;
public class WrapperClass {
static {
System.loadLibrary("CanApiWrapper");
}
private native int ResetCanCard();
public static void main(String[] args) {
javapackage.WrapperClass CanBus = new WrapperClass();
System.out.print(CanBus.ResetCanCard());
}
}
CanApiWrapper.cpp:
#include "stdafx.h"
#include "CanApiWrapper.h"
#include "fstream.h"
// DLL initialization function
BOOL APIENTRY
DllMain(HANDLE hModule, DWORD dwReason, LPVOID lpReserved)
{
return TRUE;
}
/****************************************************************************
*
* ResetCanCard
* Resets the can card
*
*****************************************************************************/
JNIEXPORT jint JNICALL Java_javapackage_WrapperClass_ResetCanCard(
JNIEnv *env,
jobject obj)
{
int RetVal;
typedef int (*ca_ResetCanCard_1_type) (void);
ca_ResetCanCard_1_type _ca_ResetCanCard_1;
HINSTANCE hinstLib;
hinstLib = LoadLibrary("canapi.dll");
if (hinstLib == NULL)
{
RetVal = -1;
}
else
{
try
{
_ca_ResetCanCard_1 = (ca_ResetCanCard_1_type)GetProcAddress(hinstLib, "ca_ResetCanCard_1");
if (_ca_ResetCanCard_1 != NULL)
{
//Error occurs when this line is executed
RetVal = _ca_ResetCanCard_1();
}
else
RetVal = -1;
}
catch (int a)
{
RetVal = -a;
}
}
FreeLibrary(hinstLib);
return (jint) RetVal;
}
CanApiWrapper.h:
#define _Included_WrapperClass
#ifdef __cplusplus
extern "C" {
#endif
JNIEXPORT jint JNICALL Java_javapackage_WrapperClass_ResetCanCard
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
hs_err_pid1108.log:
#
# An unexpected error has been detected by HotSpot Virtual Machine:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00000000, pid=1108, tid=440
#
# Java VM: Java HotSpot(TM) Client VM (1.4.2_12-b03 mixed mode)
# Problematic frame:
# C 0x00000000
#
--------------- T H R E A D ---------------
Current thread (0x00735ce8): JavaThread "main" [_thread_in_native, id=440]
siginfo: ExceptionCode=0xc0000005, reading address 0x00000000
Registers:
EAX=0x00000000, EBX=0x140fdd48, ECX=0x0006fa44, EDX=0x778ce748
ESP=0x0006fa14, EBP=0x0006fa50, ESI=0x140fdd90, EDI=0x00735ce8
EIP=0x00000000, EFLAGS=0x00010206
Top of Stack: (sp=0x0006fa14)
0x0006fa14: 1829115c 00000000 1827106d 00735ce8
0x0006fa24: 140fdd90 140fdd48 080367ac 00736130
0x0006fa34: 18291150 08070341 18290000 0006fa20
0x0006fa44: 0006fb80 18275ee0 00000000 0006fa7c
0x0006fa54: 009f81a6 00735d84 0006fa8c 0006fa60
0x0006fa64: 140fdd90 0006fa8c 140fdf48 00000000
0x0006fa74: 140fdd48 0006fa8c 0006fab0 009f2d5f
0x0006fa84: 00000000 009f64a9 1002da38 10013ab0
Instructions: (pc=0x00000000)
0xfffffff0:
Stack: [0x00030000,0x00070000), sp=0x0006fa14, free space=254k
Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j javapackage.WrapperClass.ResetCanCard()I+0
j javapackage.WrapperClass.main([Ljava/lang/String

V+12
v ~StubRoutines::call_stub
--------------- P R O C E S S ---------------
Java Threads: ( => current thread )
0x0093abc0 JavaThread "CompilerThread0" daemon [_thread_blocked, id=560]
0x00939e00 JavaThread "Signal Dispatcher" daemon [_thread_blocked, id=1012]
0x009373c8 JavaThread "Finalizer" daemon [_thread_blocked, id=1080]
0x00935ff0 JavaThread "Reference Handler" daemon [_thread_blocked, id=696]
=>0x00735ce8 JavaThread "main" [_thread_in_native, id=440]
Other Threads:
0x009795c8 VMThread [id=788]
0x0093c848 WatcherThread [id=708]
VM state:not at safepoint (normal execution)
VM Mutex/Monitor currently owned by a thread: None
Heap
def new generation total 576K, used 118K [0x10010000, 0x100b0000, 0x104f0000)
eden space 512K, 23% used [0x10010000, 0x1002dac0, 0x10090000)
from space 64K, 0% used [0x10090000, 0x10090000, 0x100a0000)
to space 64K, 0% used [0x100a0000, 0x100a0000, 0x100b0000)
tenured generation total 1408K, used 0K [0x104f0000, 0x10650000, 0x14010000)
the space 1408K, 0% used [0x104f0000, 0x104f0000, 0x104f0200, 0x10650000)
compacting perm gen total 4096K, used 952K [0x14010000, 0x14410000, 0x18010000)
the space 4096K, 23% used [0x14010000, 0x140fe060, 0x140fe200, 0x14410000)
Dynamic libraries:
0x00400000 - 0x0040b000 C:\Programme\Java\j2re1.4.2_12\bin\javaw.exe
0x77880000 - 0x77902000 C:\WINNT\system32\ntdll.dll
0x79350000 - 0x793b5000 C:\WINNT\system32\ADVAPI32.dll
0x77e70000 - 0x77f31000 C:\WINNT\system32\KERNEL32.dll
0x77d20000 - 0x77d8f000 C:\WINNT\system32\RPCRT4.dll
0x77e00000 - 0x77e69000 C:\WINNT\system32\USER32.dll
0x77f40000 - 0x77f7c000 C:\WINNT\system32\GDI32.dll
0x78000000 - 0x78045000 C:\WINNT\system32\MSVCRT.dll
0x08000000 - 0x08140000 C:\Programme\Java\j2re1.4.2_12\bin\client\jvm.dll
0x77540000 - 0x77571000 C:\WINNT\system32\WINMM.dll
0x10000000 - 0x10007000 C:\Programme\Java\j2re1.4.2_12\bin\hpi.dll
0x68f30000 - 0x68f3b000 C:\WINNT\system32\PSAPI.DLL
0x007e0000 - 0x007ee000 C:\Programme\Java\j2re1.4.2_12\bin\verify.dll
0x007f0000 - 0x00809000 C:\Programme\Java\j2re1.4.2_12\bin\java.dll
0x00810000 - 0x0081e000 C:\Programme\Java\j2re1.4.2_12\bin\zip.dll
0x18270000 - 0x1827c000 C:\JavaWorkspaces\NakedWC\WrapperClass\CanApiWrapper.dll
0x18290000 - 0x1834b000 C:\WINNT\system32\canapi.dll
VM Arguments:
java_command: javapackage.WrapperClass
Launcher Type: SUN_STANDARD
Environment Variables:
PATH=C:\WINNT\system32;C:\WINNT;C:\WINNT\System32\Wbem;"C:\Programme\Symantec\Norton Ghost 2003\";C:\Dev-C++\Bin;C:\j2sdk1.4.2_12\bin;C:\Programme\Sontheim\MT_Api\;C:\Programme\Microsoft Visual Studio\Common\Tools\WinNT;C:\Programme\Microsoft Visual Studio\Common\MSDev98\Bin;C:\Programme\Microsoft Visual Studio\Common\Tools;C:\Programme\Microsoft Visual Studio\VC98\bin;C:\j2sdk1.4.2_12\include;C:\j2sdk1.4.2_12\include\win32;C:\Programme\eclipse\plugins
USERNAME=gordon
OS=Windows_NT
PROCESSOR_IDENTIFIER=x86 Family 6 Model 13 Stepping 8, GenuineIntel
--------------- S Y S T E M ---------------
OS: Windows 2000 Build 2195 Service Pack 4
CPU:total 1 family 6, cmov, cx8, fxsr, mmx, sse, sse2
Memory: 4k page, physical 515440k(186768k free), swap 1256692k(975540k free)
vm_info: Java HotSpot(TM) Client VM (1.4.2_12-b03) for windows-x86, built on May 9 2006 12:30:51 by "java_re" with MS VC++ 6.0