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.