0

I'm trying to marshall the following C++ code to C#. I have a AddMessageListener where:

/** This function is used to register a listener for the messages.

  • nConId is Connection Identifier

  • pCallBack is a pointer to a function of type \ref MsgListenerCallBack

  • pUserData is a general purpose pointer; when a new messagge happens the callback is called with the same pointed data.
  • @return Number of message listener actives after the last function call
  • @retval >=1 The function succesfully added a new listener and returns the number of currently active listeners.
  • @retval <=0 The function failed in adding the new listener */
extern "C" MY_API int AddMessageListener(const ConnectionId_T nConId, MsgListenerCallBack pCallBack, void *pUserData);

... and a pointer to a callback function:

/** * Pointer to a callback function used when a new message is generated.

  • MsgListenerCallBack is a pointer to a function that takes one char * and a void * as input.
  • The first char * is the generated message, the second is a general purpose void * .

  • Example: void OnMessage(const char pMessage, void *pUserData) @endverbatim

  • @see AddMessageListener() */
typedef void (*MsgListenerCallBack)(const char *,void *);

The C++ code calls it as follows:

# nConId is returned from another function and the his value is 0
const int nAddMsgList(AddMessageListener(nConId, OnMessage,(void *)10));
printf("RETURNED TAG OF MESSAGE LISTENERS: %d\n",nAddMsgList);

This uses the function OnMessage:

void OnMessage(const char *pMsg,void *pUserData)
{
    int nMyData=(int)pUserData;
    printf("MSG:%s DATA:%d\n",pMsg,nMyData);
}

So, my try to marshall the AddMessageListener is:

[DllImport("my.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern int AddMessageListener(ConnectionId_T pConId, MsgListenerCallBack pCallBack, IntPtr pUserData);   

And the OnMessage:

void OnMessage(string pMsg, IntPtr pUserData)
{
     int nMyData=(int)pUserData;
     Console.Write("MSG:%s DATA:%d\n",pMsg,nMyData);
}

The problem is that I dont know how to marshall typedef void (*MsgListenerCallBack)(const char *,void *);. Can someone give me a hand? Plus, the two marshall I already done (AddMessageListener and OnMessage) are correct?

Thanks in advance.

Avión
  • 7,963
  • 11
  • 64
  • 105
  • http://stackoverflow.com/a/15481130/17034 – Hans Passant May 31 '16 at 10:28
  • Can you be more specific please? Im quite new at marshalling and this is the most complicated task I've ever done in C#. The `typedef void (*MsgListenerCallBack)(const char *,void *);` should be `public delegate void MsgListenerCallBack(X, Y);`. But which are the values of `X` and `Y`? `const char *` should be a `string`? And `void *`? Thanks. – Avión May 31 '16 at 10:43
  • You already got that. You cannot ignore the attribute. Only thing you can do wrong is not trying it. – Hans Passant May 31 '16 at 10:45
  • I already tried all that I've done in the post, plus the comment answer, But I dont know how to marshall the arguments `(const char *,void *)` of `MsgListenerCallBack`. – Avión May 31 '16 at 10:46

0 Answers0