Problem passing non-static function to delegate (delegate defined in C#)

    General discussion

  • Hi,

    I have instantiated a class referenced in a C# assembly in managed C++. The C# class in the assembly has an event, and delegate defined like so:

        [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, UseSynchronizationContext = false)]
        public class BrowserWebServiceLite : IBrowserWebServiceLite
            public static BrowserWebServiceLite instance;
            private string jobPath;
            public delegate void GetPageImageEventHandler( int buildPageNumber );
            public event GetPageImageEventHandler GetPageImageEvent;
            public BrowserWebServiceLite()
                this.pageTemplates = new Collection<PixelPowerPageTemplate>();
    I understand from the documentation that, in managed C++, if I wish to pass a non-static function to this delegate as a handler function, I do thus:

      dele = gcnew BrowserWebServiceLite::BrowserWebServiceLite::GetPageImageEventHandler(this, &CPixelPowerControlCentreApp::OnGetPageImageEvent);

    Where my referenced C# assembly is in the namespace 'BrowserWebServiceLite'.

    However, this produces the following error:
    Error 11 error C3350: 'BrowserWebServiceLite::BrowserWebServiceLite::GetPageImageEventHandler' : a delegate constructor expects 1 argument(s)
    I have successfully passed a static function by passing just the function reference (&), however it doesn't recognise this non-static form, and I need a non-static function.

    I notice that all examples I have seen involve instantiating a delegate defined in managed C++ in the first place, but I don't know if this makes any difference. The documentation for this side of things seems very poor.

    Any help would be appreciated

    Many thanks

    Wednesday, June 17, 2009 2:56 PM

All replies

  • The C++/CLI compiler produces poor error messages for delegate constructor calls.  It looks like you did it right, please post the declaration of your OnGetPageImageEvent method.

    Hans Passant.
    Wednesday, June 17, 2009 6:49 PM
  • I think I may have been using an old syntax for managed C++. The fix is simply this (this is a non-static handler function):

    BlahNamespace::BlahClass::GetInstance()->MyEvent += gcnew BlahNamespace::BlahClass::MyEventHandler(OnMyEvent);

    Where OnMyEvent is a non-static function. This is using VS2008.
    Monday, July 06, 2009 1:53 PM