• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 515
  • Last Modified:

problem with callback function using unmanaged dll

Hi all,

I have a C++ dll i will be using in C#.  Proceeded with using dllImport to access the dll's functions.
DLL has function Write() and SetCallback(BOOL (*pCallBack)(WORD, DWORD, char*))
SetCallback() function is called when Write() is currently writing to be able to show progress.

I do this:

//initializations
SetCallback(CallBackFunction);
try
{
   Write();
}
catch(Exception err)
{
   //handle err
}

This code catches NullReferenceException. :(
When i comment out SetCallBack() function, everything works fine.

Anyone have any ideas as to the cause?


Thanks
0
3Mann
Asked:
3Mann
  • 8
  • 4
1 Solution
 
3MannAuthor Commented:
hi, as i see it, i think i already implemented my callback function as to what was stated in the link...
i cant seem to find the cause of the problem...
anyone have any ideas?
0
 
3MannAuthor Commented:
must the callback function be static? :-/
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
CeiledCommented:
No, the callback function definitely doesn't need to be static...out of curiousity, do you hold onto any references to CallBackFunction? The code you posted doesn't really say what type CallBackFunction is or how you created it, so I'm assuming it's a shortcut for "new SomeDelegateType(CallBackFunction)" or something similar -- please correct me if I'm wrong.

I've seen problems in the past where delegates are marshalled to unmanaged code and no managed references are maintained and then GC collects the delegate, and after that, invocations throw NullRefException. It looks like you're calling SetCallback() and then Write() in the same function, so try just caching CallBackFunction in a local variable and see if that helps. In fact, just to be completely safe, why not go the next step and pin the delegate? Here's the modified version of your code...

using System.Runtime.InteropServices;
// ...

//initializations
GCHandle callbackHandle = GCHandle.Allocate(CallBackFunction, GCHandleType.Pinned);
SetCallback((CallBackFunctionDelegate)callbackHandle.Target);
try
{
   Write();
}
catch(Exception err)
{
   //handle err
}
finally
{
   callbackHandle.Free(); // Make sure to free the handle in the finally block, or you may end up with uncollectable memory for the life of the AppDomain
}

If you're already holding onto a reference, try adding the pinning part...if none of that helps, you might try providing more details (such as how you're obtaining CallBackFunction and what type it is, etc).
0
 
3MannAuthor Commented:
i did below code but exception at GCHandle initialization.  What did i do wrong?
exception is: "Object contains non-primitive or non-blittable data."
BTW, my delegate has a marshalled parameter.

//initializations
CallBackFunctionDelegate objCallback = new CallBackFunctionDelegate(CallBackFunction);
GCHandle callbackHandle = GCHandle.Alloc(objCallback, GCHandleType.Pinned);   //exception here
SetCallback((CallBackFunctionDelegate)callbackHandle.Target);
try
{
   Write();
}
catch(Exception err)
{
   //handle err
}
finally
{
   callbackHandle.Free(); // Make sure to free the handle in the finally block, or you may end up with uncollectable memory for the life of the AppDomain
}
0
 
CeiledCommented:
Hmmm...that's an interesting error. Can you post the signature of your delegate?
0
 
3MannAuthor Commented:
here's the signature of my delegate...

public delegate bool WriteProgressDelegate(int intCmd, uint intParam, uint intUser, [MarshalAs(UnmanagedType.LPArray)]byte[] byData);

callback function signature in dll is below:
BOOL (*pCall)(WORD wCmd,DWORD dwParam,DWORD dwUser,char *)

did i do my C# right?
0
 
3MannAuthor Commented:
i also tried using this but still same error:

public unsafe delegate bool WriteProgressDelegate(int intCmd, uint intParam, uint intUser, byte *pbyData);
0
 
3MannAuthor Commented:
BTW, the SetCallback function has parameters as below:

BOOL SetCallback(HANDLE hRec,BOOL (*pCall)(WORD wCmd,DWORD dwParam),DWORD dwUser)

and i invoke in C# like below:
[DllImport("external.dll")]
private static extern bool SetCallback(IntPtr hRec, BHAProgressDelegate callback, uint intUser);


in C++, the hWnd of current window is passed to paramater dwUser.  in my C# prog, i pass 0 as intUser.
Does this parameter have an effect on the callback function?
0
 
CeiledCommented:
Wait...are you sure you have the SetCallback() signature right? Because the signature of the function pointer it's taking doesn't match the signature of the delegate you've defined, or the signature you described two posts above, or the signature you gave when you first posted this problem. You've now posted a total of three different signatures for that callback function...what is the right signature? Having the wrong signature shouldn't cause the pinining to fail, but it *might*, potentially, cause the original problem.
0
 
3MannAuthor Commented:
ceiled, thanx for the reply.  i was thinking i got it right :) now im not very sure...
anyway, in my previous posts, there are only 2 functions... 1 for setting the callback, nd the other a pointer to the call back function.  pls refer below:

signatures from C++ dll:
BOOL SetCallback(HANDLE hRec,BOOL (*pCall)(WORD wCmd,DWORD dwParam),DWORD dwUser); //this fcn sets the pointer to the callbackfunction
BOOL (*pCall)(WORD wCmd,DWORD dwParam,DWORD dwUser,char *);  //this is the signature for the callback function

what i did in C#:
//declarations
[DllImport("external.dll")]
private static extern bool SetCallback(IntPtr hRec, BHAProgressDelegate callback, uint intUser);
public delegate bool WriteProgressDelegate(int intCmd, uint intParam, uint intUser, [MarshalAs(UnmanagedType.LPArray)]byte[] byData);

//using in code
WriteProgressDelegate Callbackfcn = new WriteProgressDelegate(this.myCallback);  //declare callback function instance
private bool myCallback(int intCmd, uint intParam, uint intUser, [MarshalAs(UnmanagedType.LPArray)]byte[] byData){ /*handlers here*/ }
SetCallback(m_hnd, Callbackfcn, 0);  //set callback function to local callback handler

pls enlighten me, what did i do wrong?
0
 
3MannAuthor Commented:
oh... i just saw what you mean about diff signatures.... this is a typo...

BOOL SetCallback(HANDLE hRec,BOOL (*pCall)(WORD wCmd,DWORD dwParam),DWORD dwUser);
is supposed to be

BOOL SetCallback(HANDLE hRec,BOOL (*pCall)(WORD wCmd,DWORD dwParam,DWORD dwUser,char *),DWORD dwUser);

0
 
CeiledCommented:
The only thing I see that might be a problem is that a WORD is actually an unsigned short. Try changing the first parameter in your delegate to a ushort and see if that helps...I don't think that would give the error you're seeing, but it's worth a shot. You might also try putting [MarshalAs(UnmanagedType.FunctionPtr)] on the delegate parameter, just in case.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 8
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now