Calling Delegates From Unmanaged C++/CLI functions

by Codewiz51 July 10, 2008 12:23

I've been working on ways to handle the DccManSink class defintion, and how I will raise events to managed classes that subscribe to the events. After experimenting with gcroot, I was able to prototype a working model.

It turns out that you can declare  code similar to the following:

// Delegate definition
delegate void DelegateDefinition(UInt32 x);

// In your class you can do something like this:
msclr::gcroot<DelegateDefinition^> _MyDelegate;

// However the most important thing I found out is that
// I can construct code like the following in my unmanaged
// method
        unsigned int i = 0;
        for each(DelegateDefinition^ dd in _MyDelegate->GetInvocationList())
        {
            dd->Invoke(++i);
        }

Armed with this knowledge, I was able to build a working protype for calling multicast delegates from a C++/CLI native class. I've posted a console application on my wiki: Experiments in calling managed delegates

 

Comments are closed

Powered by BlogEngine.NET 1.6.0.0
Theme by Mads Kristensen | Modified by Mooglegiant

Disclaimer

This blog represents my personal hobby, observations and views. It does not represent the views of my employer, clients, especially my wife, children, in-laws, clergy, the dog, the cats or my daughter's horse. In fact, I am not even sure it represents my views when I take the time to reread postings.

© Copyright 2008-2011