Callback function using PInvoke

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Michibeck
    New Member
    • Sep 2008
    • 1

    Callback function using PInvoke

    Hi,

    I need some help regarding callback function written in c# and called from unmanaged c code. (c dll).
    The problem is that the callback function crashs after the first call with a memory access exception. I tried to protect the callback function to be handle by the GC but it doesn't work. What could I do to fix that problem??

    Thanks, Michibeck

    C Code:
    Code:
    /***********************************************************************
    **  
    ** Function    : APH_RegTxTsHookCb
    **  
    ** Description : Registers a callback function that hooks the 
    **               Tx timestamps to the application. 
    **               Only one callback function can be registered.
    **
    **    See Also : APH_UnregTxTsHook()
    **  
    ** Parameter   : pf_cbTxTs (IN) - Tx timestamp callback function to register
    **               
    ** Returnvalue : -
    ** 
    ** Remarks     : -
    **  
    ***********************************************************************/
    void APH_RegTxTsHookCb( void (k_CB_CALL_CONV *pf_cbTxTs)(PTP_t_TmStmp      s_txTs,
                                                             UINT16            w_seqId,
                                                             UINT16            w_txIfIdx,
                                                             PTP_t_msgTypeEnum e_mType));
    
    /***********************************************************************
    **  
    ** Function    : APH_UnregTxTsHook
    **  
    ** Description : Unregisters the Tx timestamp hook function.
    **    
    **    See Also : APH_RegTxTsHookCb()
    **  
    ***********************************************************************/
    void APH_UnregTxTsHook( void );

    C# Code:
    Code:
     public delegate void TxTsHookCb([MarshalAs(UnmanagedType.Struct)] sTimeStamp s_rxTs, UInt16 w_seqId, UInt16 w_rxIfIdx, ePTPMsgTypes e_mType);
    
    public class PTP
    {
    [DllImport("MyDll.dll")]
            static extern void APH_RegTxTsHookCb([MarshalAs(UnmanagedType.FunctionPtr)]TxTsHookCb callback);
            [DllImport("MyDll.dll")]
            static extern void APH_UnregTxTsHook();
    
      public static void TxHookCbFunc([MarshalAs(UnmanagedType.Struct)]sTimeStamp s_rxTs, UInt16 w_seqId, UInt16 w_rxIfIdx, ePTPMsgTypes e_mType)
            {
                Console.WriteLine(w_seqId);
                Console.WriteLine(s_rxTs.Print());
                Console.WriteLine(w_rxIfIdx);
                Console.WriteLine(e_mType);
            }
    
     public static sMsgTimeStamp GetTransmittingTimestamp(ePTPMsgTypes msgType, uint timeoutSec)
            {
                TxTsHookCb callbackTx = new TxTsHookCb(TxHookCbFunc); 
                int i = 0;
                 
                timeStampBuffer = new sMsgTimeStamp();
                timeStampBuffer.IsSet = false;
                timeStampBuffer.srcPort = ownPortId;
                timeStampBuffer.msgType = msgType;
    
                APH_RegTxTsHookCb(callbackTx);
    
                while (!timeStampBuffer.IsSet)
                {
                    System.Threading.Thread.Sleep(500);
                    i++;
                    if (i * 2 > timeoutSec) break;
                }
    
                APH_UnregTxTsHook();
    
                GC.KeepAlive(callbackTx); 
    
                return timeStampBuffer;
            }
    }
Working...