473,386 Members | 2,042 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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:
Expand|Select|Wrap|Line Numbers
  1. /***********************************************************************
  2. **  
  3. ** Function    : APH_RegTxTsHookCb
  4. **  
  5. ** Description : Registers a callback function that hooks the 
  6. **               Tx timestamps to the application. 
  7. **               Only one callback function can be registered.
  8. **
  9. **    See Also : APH_UnregTxTsHook()
  10. **  
  11. ** Parameter   : pf_cbTxTs (IN) - Tx timestamp callback function to register
  12. **               
  13. ** Returnvalue : -
  14. ** 
  15. ** Remarks     : -
  16. **  
  17. ***********************************************************************/
  18. void APH_RegTxTsHookCb( void (k_CB_CALL_CONV *pf_cbTxTs)(PTP_t_TmStmp      s_txTs,
  19.                                                          UINT16            w_seqId,
  20.                                                          UINT16            w_txIfIdx,
  21.                                                          PTP_t_msgTypeEnum e_mType));
  22.  
  23. /***********************************************************************
  24. **  
  25. ** Function    : APH_UnregTxTsHook
  26. **  
  27. ** Description : Unregisters the Tx timestamp hook function.
  28. **    
  29. **    See Also : APH_RegTxTsHookCb()
  30. **  
  31. ***********************************************************************/
  32. void APH_UnregTxTsHook( void );

C# Code:
Expand|Select|Wrap|Line Numbers
  1.  public delegate void TxTsHookCb([MarshalAs(UnmanagedType.Struct)] sTimeStamp s_rxTs, UInt16 w_seqId, UInt16 w_rxIfIdx, ePTPMsgTypes e_mType);
  2.  
  3. public class PTP
  4. {
  5. [DllImport("MyDll.dll")]
  6.         static extern void APH_RegTxTsHookCb([MarshalAs(UnmanagedType.FunctionPtr)]TxTsHookCb callback);
  7.         [DllImport("MyDll.dll")]
  8.         static extern void APH_UnregTxTsHook();
  9.  
  10.   public static void TxHookCbFunc([MarshalAs(UnmanagedType.Struct)]sTimeStamp s_rxTs, UInt16 w_seqId, UInt16 w_rxIfIdx, ePTPMsgTypes e_mType)
  11.         {
  12.             Console.WriteLine(w_seqId);
  13.             Console.WriteLine(s_rxTs.Print());
  14.             Console.WriteLine(w_rxIfIdx);
  15.             Console.WriteLine(e_mType);
  16.         }
  17.  
  18.  public static sMsgTimeStamp GetTransmittingTimestamp(ePTPMsgTypes msgType, uint timeoutSec)
  19.         {
  20.             TxTsHookCb callbackTx = new TxTsHookCb(TxHookCbFunc); 
  21.             int i = 0;
  22.  
  23.             timeStampBuffer = new sMsgTimeStamp();
  24.             timeStampBuffer.IsSet = false;
  25.             timeStampBuffer.srcPort = ownPortId;
  26.             timeStampBuffer.msgType = msgType;
  27.  
  28.             APH_RegTxTsHookCb(callbackTx);
  29.  
  30.             while (!timeStampBuffer.IsSet)
  31.             {
  32.                 System.Threading.Thread.Sleep(500);
  33.                 i++;
  34.                 if (i * 2 > timeoutSec) break;
  35.             }
  36.  
  37.             APH_UnregTxTsHook();
  38.  
  39.             GC.KeepAlive(callbackTx); 
  40.  
  41.             return timeStampBuffer;
  42.         }
  43. }
Sep 29 '08 #1
0 1244

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Huseyin Altun | last post by:
I am using VS.NET 2003 C# I am using a DLL. it has a method "GetSalesItems" This method takes a fuction pointer. and returns the Sales items via the callback function. public static extern void...
3
by: Michael | last post by:
Hi developers, I downloaded the Canon SDK Version 7.1 and tried to migrate the "RELCTRL" sample to C# (with PInvoke). Almost everything is working except the following callback function that...
3
by: msnews.microsoft.com | last post by:
Hi i am using User32.dll in Visual stdio 2005. public static extern long SetActiveWindow(long hwnd); public static extern long keybd_event(byte bVk, byte bScan, long dwFlags,
3
by: markb | last post by:
Hi My C# app is being called from a callback from an unmanaged DLL. One of the parameters of the callback is of type BOOL. I am using PInvoke to marshal this to a (managed) bool. The problem is...
2
by: MyCrystalGift | last post by:
Hi, I have an old C++ GUI Application CPPAPP.exe that calls a C DLL library RULE.DLL through a C++ class wrapper LoadRule.CPP. Now I need to call the C DLL RULE.DLL from C# GUI application...
6
by: smmk25 | last post by:
Before I state the problem, I just want to let the readers know, I am knew to C++\CLI and interop so please forgive any newbie questions. I have a huge C library which I want to be able to use in...
3
by: =?Utf-8?B?VG9tIEFsbGVu?= | last post by:
We are using a 3rd party API that processes video stream files to update a data structure in the file. I am writting a C# facade for this API but I am having a hard time getting it to pass data...
10
by: SQACPP | last post by:
Hi, I try to figure out how to use Callback procedure in a C++ form project The following code *work* perfectly on a console project #include "Windows.h" BOOL CALLBACK...
1
by: eastlands | last post by:
I need to use an unmanaged c++ dll which uses structs that contain callbacks and also functions. I have included the appropriate c++ definitials and my c# translations below. I first defined the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.