tags:

views:

116

answers:

0

Is there a simple example of how to pass messages from unsafe callback to managed code?

I have a proprietary dll which receives some messages packed in structs and all is coming to a callback function.

The example of usage is as follows but it calls unsafe code too. I want to pass the messages into my application which is all managed code.

*P.S. I have no experience in interop or unsafe code. I used to develop in C++ 8 yrs ago but remember very little from that nightmarish times :)

P.P.S. The application is loaded as hell, the original devs claim it processes 2mil messages per sec.. I need a most efficient solution.*

static unsafe int OnCoreCallback(IntPtr pSys, IntPtr pMsg)
{
  // Alias structure pointers to the pointers passed in.
  CoreSystem* pCoreSys = (CoreSystem*)pSys;
  CoreMessage* pCoreMsg = (CoreMessage*)pMsg;

  // message handler function.
  if (pCoreMsg->MessageType == Core.MSG_STATUS)
    OnCoreStatus(pCoreSys, pCoreMsg);

  // Continue running
  return (int)Core.CALLBACKRETURN_CONTINUE;
}

Thank you.