Question

I am calling a DLL with passing a callback functio object to it. One of the functions is simple print. I have then, a loop of 100 iterations, just printing the index and a few prints after the loop.


Here is the C code

extern "C" int Start(void* callback(CString))
{
   for(int j=0; j<100; j++)
    callback(AConvertToString(j));

   callback("in Start called from Java");
   callback("another line");
}

Here is the Java code

public interface MyDll extends Library{
  MyDll INSTANCE = (MyDll) Native.loadLibrary("MyDll",MyDll.class);
     public interface MyCallback extends StdCallCallback {
            public boolean callback(String msg);
     }
     public int Start(MyCallback callback);
  }

//in main:
...
  MyDll myDll = (MyDll)MyDll.INSTANCE;
  myDll.Start(new MyDll.MyCallback() {
      public boolean callback(String msg) {
         System.out.println(msg);
          return true;          
      }
});

The output is numbers 0..41 (YES 41!!! not 99) and then "in Start called from Java" followed by a horrible crash:

#
# An unexpected error has been detected by Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x7c809823, pid=468, tid=2636
#
# Java VM: Java HotSpot(TM) Client VM (10.0-b23 mixed mode, sharing windows-x86)
# Problematic frame:
# C  [kernel32.dll+0x9823]

I've read alot (here as well) yet I cannot find the problem. I am running JRE of Java6. I have 1.5GB of memory on my machine. The DLL is not used by any other process (no concurrency issues).

Thanks, Azriel

Was it helpful?

Solution

Try to write your MyCallback as an com.sun.jna.Callback instead as an com.sun.jna.win32.StdCallLibrary.StdCallCallback :

public interface MyDll extends Library{
  MyDll INSTANCE = (MyDll) Native.loadLibrary("MyDll",MyDll.class);
     public interface MyCallback extends Callback {
            public boolean callback(String msg);
     }
     public int Start(MyCallback callback);
  }

Regards, Emmanuel Girard

OTHER TIPS

In my case developing a telecom application that uses a win32 dll it was the opposite case.

Using com.sun.jna.Library and com.sun.jna.Callback it always crashed the VM when returning from the callback call.

After changing to com.sun.jna.win32.StdCallLibrary and com.sun.jna.win32.StdCallLibrary.StdCallCallback the problem was solved.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top