This Question is Possibly Answered

1 "correct" answer available (4 pts) 2 "helpful" answers available (2 pts)
11 Replies Last post: Apr 16, 2008 4:01 PM by Serge Piletsky  
calumm   6 posts since
Apr 3, 2008
Currently Being Moderated

Apr 3, 2008 3:54 PM

Callbacks and pointers to arrays

I'm integrating with an external DLL.  One of the functions has a callback:

 

typedef int (*PROC_DOWNLOAD)(DWORD DataType, DWORD Size, BYTE *pData, DWORD UserDefined);

WORD CALL StartDownload (BYTE CplNum, PROC_DOWNLOAD pfunc, DWORD reserved, DWORD UserDefined);

 

So the callback passes data, of length SIZE, into pData.

 

My  Callback implementation is as follows:

 

 

 

private static class StartDownloadCallback extends Callback

{

private final UInt32 dataType = DWORD();

private final UInt32 size = DWORD();

private final PrimitiveArray pDataArray = new PrimitiveArray(Int8.class, 0);

private final ExternalArrayPointer pData = new ExternalArrayPointer(pDataArray);

private final UInt32 userDefined = DWORD();

private final Int32 returnValue = INT();

 

private StartDownloadCallback()

{

init(new Parameter[] {dataType, size, pData, userDefined}, returnValue);

}

 

public void callback()

{

System.out.println("Callback invoked.");

// extract data

returnValue.setValue(0);

}

}

 

The first time that it gets called, it receives the correct data (extracted from pData).

However, after that, I get a VM error reported by Windows, with Exception Code c0000005.

Am I taking the correct approach to this? I've looked at the documentation, samples, and forum, and I still wasn't quite sure what to do.

Note that I also tried the same technique as in the sample file CallbackWithFillExternalArraySample.java - i.e. pass in a Pointer.Void instance, and use castTo() on a Pointer to a PrimitiveArray. This had

the same effect as above - worked first time, then failed after that.

 

Thanks,

Calum

Serge Piletsky TeamDev Ltd. 670 posts since
Apr 24, 2006
Currently Being Moderated
1. Apr 4, 2008 4:03 PM in response to: calumm
Re: Callbacks and pointers to arrays

Hi Calum,

 

The declaration of your callback seems to be correct. But you have missed quite important fact in your description of the problem: you did not specify how you read the data in the .callback() method. Or it's just empty?

 

My point is that callbacks are working in general (you can simply verify that by another examples or for example by WinPack), therefore there must be something in your code (in the callback() method) or in a native code which produces such c0000005 exception (a null pointer exception) on a native side.

 

So if you can provide more details on this or a test case which reproduces this issue then we can provide a more precise solution.

 

-Serge

Serge Piletsky TeamDev Ltd. 670 posts since
Apr 24, 2006
Currently Being Moderated
4. Apr 4, 2008 4:38 PM in response to: calumm
Re: Callbacks and pointers to arrays

Hi Calum,

 

It's still difficult to say what might cause such exception. In order to verify that this is not a problem of callback() method implementation you need to do the following:

1) use try/catch for handling any exception in callback method;

2) check the pData pointer for null before reading data from it, for example:

// if pointer is valid
if (!pData.isNull()) {
   pData.readArray((int)size.getValue());
   System.out.println("pData = " + HexStrings.toHexString(pDataArray.getBytes()));
   System.out.println("userDefined = " + userDefined.getValue());
}

 

-Serge

Serge Piletsky TeamDev Ltd. 670 posts since
Apr 24, 2006
Currently Being Moderated
5. Apr 4, 2008 4:41 PM in response to: calumm
Re: Callbacks and pointers to arrays

Hi,

 

Yes, such declaration of an array of unknown size is quite correct and all callback parameters can be reused for all subsequent calls. So, you do not need to reinitialize it every time.

 

Though please try my suggestion that I posted before.

 

-Serge

Serge Piletsky TeamDev Ltd. 670 posts since
Apr 24, 2006
Currently Being Moderated
8. Apr 10, 2008 3:08 PM in response to: calumm
Re: Callbacks and pointers to arrays

Hi,

 

We have not seen such problem with callbacks before. Please send me a simple test case (library plus java code) that reproduces it. I will investigate this issue and let you know the solution.

 

-Serge

Serge Piletsky TeamDev Ltd. 670 posts since
Apr 24, 2006
Currently Being Moderated
10. Apr 14, 2008 4:33 PM in response to: calumm
Re: Callbacks and pointers to arrays

Hi,

 

Thanks for the sample code and additional information. We will investigate this issue and send you the solution soon.

 

-Serge

Serge Piletsky TeamDev Ltd. 670 posts since
Apr 24, 2006
Currently Being Moderated
11. Apr 16, 2008 4:01 PM in response to: calumm
Re: Callbacks and pointers to arrays

Hi,

 

I found the problem. It was incorrect calling convention of the MyCallback class

 

Here is the fix:

private MyCallback() {
  init(new Parameter[] {type, size, pData, userDefined}, returnValue);
  setCallingConvention(Function.CDECL_CALLING_CONVENTION); // <- just add this line 
}

 

Please try this solution in your real application and let me know the results.

 

-Serge

More Like This

  • Retrieving data ...