Jesper Axelsen

Jesper Axelsen

  • NA
  • 1
  • 2.1k

Calling function in unmanaged library from c# returns an error

Jan 13 2012 4:15 PM
Hi
I hope someone will be able to help me solve this issue.

The  following two functions are given from a DLL documentation. The DLL is an  unmanaged library. The CCC_EventHandlerFunc is an eventhandler wich is  registered using the CCC_RegisterEventHandler function.
long  CCC_RegisterEventHandler( CCC_EventHandler handler, BYTE evmask, LONG param );
void CCC_EventHandlerFunc(WORD event_id, LPVOID data, LONG param);

My  problem is, that when I call CCC_RegisterEventHandler the function doesn't  return 0, which it should to indicate no errors.
Here is what I have so far  (in c#):

1. Declared a delegate that matches the  CCC_EventHandlerFunc
  [UnmanagedFunctionPointer(CallingConvention.Cdecl)]  <--- tried both  with an without this line
  public delegate void  CCC_EventHandlerFunc(
  UInt16 event_id,
  IntPtr data,
  Int32 param
  );

2. Used DllImport to access  CCC_RegisterEventHandler in the unmanaged library.
  [DllImport("ccc.dll")]
  public static extern Int32  CCC_RegisterEventHandler(
  [MarshalAs(UnmanagedType.FunctionPtr)]  CCC_EventHandlerFunc handler,
  byte evmask,
  Int32  param
  );



3. Made an instance of the delegate
  private static CCC_EventHandlerFunc m_CCC_EventHandlerFunc = new  CCC_EventHandlerFunc(Fnc_CCC_EventHandlerFunc);


4. When calling the  sequence:
 
  if  (CCC_ProtocolSelect("pro:arq;cha:tcp;192.168.10.150:0") != 0)
  return;

  int version = CCC_ProtocolStartV(-1, -1);
  Int32  res = CCC_RegisterEventHandler(m_CCC_EventHandlerFunc, 0, 0);
  if (res  != 0)
  return;

res ends up being nonzero which indicates that  an error has occured.

Any suggestions are  appreciated.
/Jesper




Answers (1)