tags:

views:

444

answers:

2

Hi

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

+3  A: 

Hi Azriel,

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

Emmanuel Girard
Thank you, that did the trick. Such a minor thing...
aabramovich
A: 

i am facing the same problem, i used Callback instead of StdCallCallback but still have the same problem. any tips why? Regards

Sameeh Harfoush
Sorry, I haven't been using JNA since... too fragile (note to self: check if improved). So I can't help. I found that you really need to be grainy with this. You can't pass an object with several function but a single function for each thing. A bit annoying. Maybe this would help.
aabramovich