Paul Wilhelm
Paul Wilhelm

Reputation: 53

C++/CLI: Queue of member function pointers

I want to use a queue to synchronize access to a serial port that is being shared between several independant blocks of code. Every block provides a callback function which, when needed, will be enqueued and takes care of exactly one atomic operation on the serial port. A timer then periodically executes all pending operations, hopefully with no interferance.

Since I am relatively new to C++/CLI programming, I was able to create a System::Collections::Queue and enqueued some strings; but I cannot, for the life of me, figure out how to do the same with pointers to my callback functions (void Test()).

I googled intensively, but even the most simple examples did not work for me. All this delegate, Boost, Marshal, gcnew stuff is confusing me a bit right now.

I am using Microsoft Visual C++ 2010 Express. Hope you can help!

Regards from Germany,

Paul

Upvotes: 2

Views: 1204

Answers (1)

Viktor Latypov
Viktor Latypov

Reputation: 14467

In .NET/CLR world the System.Delegate class is an analogue of the function pointer.

Since you are doing some low-level stuff and the callbacks are written in high-level managed environment, the need for marshaling arises.

To use managed function pointers as native callbacks you need this MSDN article

http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.getdelegateforfunctionpointer(v=vs.80).aspx

If you have a queue of System.Delegate instances, then just do the D.DynamicInvoke() call with a list of appropriate arguments.

Upvotes: 1

Related Questions