Invoke C# delegate asynchronously from unmanaged C++

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Yoni Rabinovitch

    Invoke C# delegate asynchronously from unmanaged C++

    Is it possible to invoke a C# delegate/event handler asynchronously,
    from unmanaged C++ ?

    I assume this requires a Managed C++ wrapper, which would call
    BeginInvoke on the delegate ?

    Is this correct, and if so, does anyone have an example ?

    Thanks !!
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Invoke C# delegate asynchronously from unmanaged C++

    Yoni,

    It is not possible from unmanaged C. You will either have to use the
    #pragma managed and #pragma unmanaged directives to enter into managed code
    and make the call, or you will have to marshal the delegate as a function
    pointer, in which case you would have to call it asynchronously, creating
    your own thread, etc, etc. Or, you could export the type as a COM object
    and use that, calling the BeginInvoke method yourself.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - nicholas.paldin o@exisconsultin g.com

    "Yoni Rabinovitch" <yoni.rabinovit ch@adc.com> wrote in message
    news:d9c51d85.0 309090511.60494 a75@posting.goo gle.com...[color=blue]
    > Is it possible to invoke a C# delegate/event handler asynchronously,
    > from unmanaged C++ ?
    >
    > I assume this requires a Managed C++ wrapper, which would call
    > BeginInvoke on the delegate ?
    >
    > Is this correct, and if so, does anyone have an example ?
    >
    > Thanks !![/color]


    Comment

    • Guest's Avatar

      #3
      Re: Invoke C# delegate asynchronously from unmanaged C++

      Nicholas, maybe i don't understand you,

      but, I'm doing it!

      However, my app crashes after about 15 minutes of doing it intesively.

      I fear that since i am not locking down the manged code in any way,
      it gets moved around.
      The Unmanaged code not being aware of this change, invokes the Managed code.
      However it is using the wrong address. And this leads to a crash -
      "ExecutionEngin eException".

      assaf




      "Nicholas Paldino [.NET/C# MVP]" <nicholas.paldi no@exisconsulti ng.com> wrote
      in message news:eSJ4EbtdDH A.416@tk2msftng p13.phx.gbl...[color=blue]
      > Yoni,
      >
      > It is not possible from unmanaged C. You will either have to use the
      > #pragma managed and #pragma unmanaged directives to enter into managed[/color]
      code[color=blue]
      > and make the call, or you will have to marshal the delegate as a function
      > pointer, in which case you would have to call it asynchronously, creating
      > your own thread, etc, etc. Or, you could export the type as a COM object
      > and use that, calling the BeginInvoke method yourself.
      >
      > Hope this helps.
      >
      >
      > --
      > - Nicholas Paldino [.NET/C# MVP]
      > - nicholas.paldin o@exisconsultin g.com
      >
      > "Yoni Rabinovitch" <yoni.rabinovit ch@adc.com> wrote in message
      > news:d9c51d85.0 309090511.60494 a75@posting.goo gle.com...[color=green]
      > > Is it possible to invoke a C# delegate/event handler asynchronously,
      > > from unmanaged C++ ?
      > >
      > > I assume this requires a Managed C++ wrapper, which would call
      > > BeginInvoke on the delegate ?
      > >
      > > Is this correct, and if so, does anyone have an example ?
      > >
      > > Thanks !![/color]
      >
      >[/color]


      Comment

      • Nicholas Paldino [.NET/C# MVP]

        #4
        Re: Invoke C# delegate asynchronously from unmanaged C++

        Assaf,

        How are you doing it? Which method are you using?


        --
        - Nicholas Paldino [.NET/C# MVP]
        - nicholas.paldin o@exisconsultin g.com

        <assafwo@hotmai l.com> wrote in message
        news:ub1dlevdDH A.3876@TK2MSFTN GP11.phx.gbl...[color=blue]
        > Nicholas, maybe i don't understand you,
        >
        > but, I'm doing it!
        >
        > However, my app crashes after about 15 minutes of doing it intesively.
        >
        > I fear that since i am not locking down the manged code in any way,
        > it gets moved around.
        > The Unmanaged code not being aware of this change, invokes the Managed[/color]
        code.[color=blue]
        > However it is using the wrong address. And this leads to a crash -
        > "ExecutionEngin eException".
        >
        > assaf
        >
        >
        >
        >
        > "Nicholas Paldino [.NET/C# MVP]" <nicholas.paldi no@exisconsulti ng.com>[/color]
        wrote[color=blue]
        > in message news:eSJ4EbtdDH A.416@tk2msftng p13.phx.gbl...[color=green]
        > > Yoni,
        > >
        > > It is not possible from unmanaged C. You will either have to use[/color][/color]
        the[color=blue][color=green]
        > > #pragma managed and #pragma unmanaged directives to enter into managed[/color]
        > code[color=green]
        > > and make the call, or you will have to marshal the delegate as a[/color][/color]
        function[color=blue][color=green]
        > > pointer, in which case you would have to call it asynchronously,[/color][/color]
        creating[color=blue][color=green]
        > > your own thread, etc, etc. Or, you could export the type as a COM[/color][/color]
        object[color=blue][color=green]
        > > and use that, calling the BeginInvoke method yourself.
        > >
        > > Hope this helps.
        > >
        > >
        > > --
        > > - Nicholas Paldino [.NET/C# MVP]
        > > - nicholas.paldin o@exisconsultin g.com
        > >
        > > "Yoni Rabinovitch" <yoni.rabinovit ch@adc.com> wrote in message
        > > news:d9c51d85.0 309090511.60494 a75@posting.goo gle.com...[color=darkred]
        > > > Is it possible to invoke a C# delegate/event handler asynchronously,
        > > > from unmanaged C++ ?
        > > >
        > > > I assume this requires a Managed C++ wrapper, which would call
        > > > BeginInvoke on the delegate ?
        > > >
        > > > Is this correct, and if so, does anyone have an example ?
        > > >
        > > > Thanks !![/color]
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Guest's Avatar

          #5
          Re: Invoke C# delegate asynchronously from unmanaged C++

          check this out.
          it has some code.

          in the visual studio .net:
          ms-help://MS.VSCC.2003/MS.MSDNQTR.2003 FEB.1033/cpguide/html/cpconcallbacksa m
          ple.htm

          assaf

          "Nicholas Paldino [.NET/C# MVP]" <nicholas.paldi no@exisconsulti ng.com> wrote
          in message news:uKWpOzvdDH A.3240@TK2MSFTN GP11.phx.gbl...[color=blue]
          > Assaf,
          >
          > How are you doing it? Which method are you using?
          >
          >
          > --
          > - Nicholas Paldino [.NET/C# MVP]
          > - nicholas.paldin o@exisconsultin g.com
          >
          > <assafwo@hotmai l.com> wrote in message
          > news:ub1dlevdDH A.3876@TK2MSFTN GP11.phx.gbl...[color=green]
          > > Nicholas, maybe i don't understand you,
          > >
          > > but, I'm doing it!
          > >
          > > However, my app crashes after about 15 minutes of doing it intesively.
          > >
          > > I fear that since i am not locking down the manged code in any way,
          > > it gets moved around.
          > > The Unmanaged code not being aware of this change, invokes the Managed[/color]
          > code.[color=green]
          > > However it is using the wrong address. And this leads to a crash -
          > > "ExecutionEngin eException".
          > >
          > > assaf
          > >
          > >
          > >
          > >
          > > "Nicholas Paldino [.NET/C# MVP]" <nicholas.paldi no@exisconsulti ng.com>[/color]
          > wrote[color=green]
          > > in message news:eSJ4EbtdDH A.416@tk2msftng p13.phx.gbl...[color=darkred]
          > > > Yoni,
          > > >
          > > > It is not possible from unmanaged C. You will either have to use[/color][/color]
          > the[color=green][color=darkred]
          > > > #pragma managed and #pragma unmanaged directives to enter into managed[/color]
          > > code[color=darkred]
          > > > and make the call, or you will have to marshal the delegate as a[/color][/color]
          > function[color=green][color=darkred]
          > > > pointer, in which case you would have to call it asynchronously,[/color][/color]
          > creating[color=green][color=darkred]
          > > > your own thread, etc, etc. Or, you could export the type as a COM[/color][/color]
          > object[color=green][color=darkred]
          > > > and use that, calling the BeginInvoke method yourself.
          > > >
          > > > Hope this helps.
          > > >
          > > >
          > > > --
          > > > - Nicholas Paldino [.NET/C# MVP]
          > > > - nicholas.paldin o@exisconsultin g.com
          > > >
          > > > "Yoni Rabinovitch" <yoni.rabinovit ch@adc.com> wrote in message
          > > > news:d9c51d85.0 309090511.60494 a75@posting.goo gle.com...
          > > > > Is it possible to invoke a C# delegate/event handler asynchronously,
          > > > > from unmanaged C++ ?
          > > > >
          > > > > I assume this requires a Managed C++ wrapper, which would call
          > > > > BeginInvoke on the delegate ?
          > > > >
          > > > > Is this correct, and if so, does anyone have an example ?
          > > > >
          > > > > Thanks !!
          > > >
          > > >[/color]
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • José Joye

            #6
            Re: Invoke C# delegate asynchronously from unmanaged C++

            I do not know if I'm off topic. However, I had a crash problem (after a
            about 1000 calls) when using a callback from C to C# (having delegate in
            C#).
            The problem was in the C declaration. Here is a correct sample (watch for
            the 2nd WINAPI)

            // in C header file:
            DLLEXPORT void WINAPI PassExitFnPtr (void (WINAPI * pExit)(int exit_code));



            José

            <assafwo@hotmai l.com> a écrit dans le message de
            news:ub1dlevdDH A.3876@TK2MSFTN GP11.phx.gbl...[color=blue]
            > Nicholas, maybe i don't understand you,
            >
            > but, I'm doing it!
            >
            > However, my app crashes after about 15 minutes of doing it intesively.
            >
            > I fear that since i am not locking down the manged code in any way,
            > it gets moved around.
            > The Unmanaged code not being aware of this change, invokes the Managed[/color]
            code.[color=blue]
            > However it is using the wrong address. And this leads to a crash -
            > "ExecutionEngin eException".
            >
            > assaf
            >
            >
            >
            >
            > "Nicholas Paldino [.NET/C# MVP]" <nicholas.paldi no@exisconsulti ng.com>[/color]
            wrote[color=blue]
            > in message news:eSJ4EbtdDH A.416@tk2msftng p13.phx.gbl...[color=green]
            > > Yoni,
            > >
            > > It is not possible from unmanaged C. You will either have to use[/color][/color]
            the[color=blue][color=green]
            > > #pragma managed and #pragma unmanaged directives to enter into managed[/color]
            > code[color=green]
            > > and make the call, or you will have to marshal the delegate as a[/color][/color]
            function[color=blue][color=green]
            > > pointer, in which case you would have to call it asynchronously,[/color][/color]
            creating[color=blue][color=green]
            > > your own thread, etc, etc. Or, you could export the type as a COM[/color][/color]
            object[color=blue][color=green]
            > > and use that, calling the BeginInvoke method yourself.
            > >
            > > Hope this helps.
            > >
            > >
            > > --
            > > - Nicholas Paldino [.NET/C# MVP]
            > > - nicholas.paldin o@exisconsultin g.com
            > >
            > > "Yoni Rabinovitch" <yoni.rabinovit ch@adc.com> wrote in message
            > > news:d9c51d85.0 309090511.60494 a75@posting.goo gle.com...[color=darkred]
            > > > Is it possible to invoke a C# delegate/event handler asynchronously,
            > > > from unmanaged C++ ?
            > > >
            > > > I assume this requires a Managed C++ wrapper, which would call
            > > > BeginInvoke on the delegate ?
            > > >
            > > > Is this correct, and if so, does anyone have an example ?
            > > >
            > > > Thanks !![/color]
            > >
            > >[/color]
            >
            >[/color]


            Comment

            • José Joye

              #7
              Re: Invoke C# delegate asynchronously from unmanaged C++

              BTW, I had a second problem as well.
              You have to make sure that you keep a live reference to your delegate in
              your C# code (this has to be kept as long as the callback could be fired)

              José

              "José Joye" <jose.joye@__No _SPam__bluewin_ _maPS_oN__.ch> a écrit dans le
              message de news:e51jdGwdDH A.736@TK2MSFTNG P09.phx.gbl...[color=blue]
              > I do not know if I'm off topic. However, I had a crash problem (after a
              > about 1000 calls) when using a callback from C to C# (having delegate in
              > C#).
              > The problem was in the C declaration. Here is a correct sample (watch for
              > the 2nd WINAPI)
              >
              > // in C header file:
              > DLLEXPORT void WINAPI PassExitFnPtr (void (WINAPI * pExit)(int[/color]
              exit_code));[color=blue]
              >
              >
              >
              > José
              >
              > <assafwo@hotmai l.com> a écrit dans le message de
              > news:ub1dlevdDH A.3876@TK2MSFTN GP11.phx.gbl...[color=green]
              > > Nicholas, maybe i don't understand you,
              > >
              > > but, I'm doing it!
              > >
              > > However, my app crashes after about 15 minutes of doing it intesively.
              > >
              > > I fear that since i am not locking down the manged code in any way,
              > > it gets moved around.
              > > The Unmanaged code not being aware of this change, invokes the Managed[/color]
              > code.[color=green]
              > > However it is using the wrong address. And this leads to a crash -
              > > "ExecutionEngin eException".
              > >
              > > assaf
              > >
              > >
              > >
              > >
              > > "Nicholas Paldino [.NET/C# MVP]" <nicholas.paldi no@exisconsulti ng.com>[/color]
              > wrote[color=green]
              > > in message news:eSJ4EbtdDH A.416@tk2msftng p13.phx.gbl...[color=darkred]
              > > > Yoni,
              > > >
              > > > It is not possible from unmanaged C. You will either have to use[/color][/color]
              > the[color=green][color=darkred]
              > > > #pragma managed and #pragma unmanaged directives to enter into managed[/color]
              > > code[color=darkred]
              > > > and make the call, or you will have to marshal the delegate as a[/color][/color]
              > function[color=green][color=darkred]
              > > > pointer, in which case you would have to call it asynchronously,[/color][/color]
              > creating[color=green][color=darkred]
              > > > your own thread, etc, etc. Or, you could export the type as a COM[/color][/color]
              > object[color=green][color=darkred]
              > > > and use that, calling the BeginInvoke method yourself.
              > > >
              > > > Hope this helps.
              > > >
              > > >
              > > > --
              > > > - Nicholas Paldino [.NET/C# MVP]
              > > > - nicholas.paldin o@exisconsultin g.com
              > > >
              > > > "Yoni Rabinovitch" <yoni.rabinovit ch@adc.com> wrote in message
              > > > news:d9c51d85.0 309090511.60494 a75@posting.goo gle.com...
              > > > > Is it possible to invoke a C# delegate/event handler asynchronously,
              > > > > from unmanaged C++ ?
              > > > >
              > > > > I assume this requires a Managed C++ wrapper, which would call
              > > > > BeginInvoke on the delegate ?
              > > > >
              > > > > Is this correct, and if so, does anyone have an example ?
              > > > >
              > > > > Thanks !!
              > > >
              > > >[/color]
              > >
              > >[/color]
              >
              >[/color]


              Comment

              • Guest's Avatar

                #8
                Re: Invoke C# delegate asynchronously from unmanaged C++

                actually your problem sounds very similar to mine.
                i am actually running a test now.
                with your modification!
                if this works, it will end 3 months of chasing this bug.

                tnx

                assaf

                "José Joye" <jose.joye@__No _SPam__bluewin_ _maPS_oN__.ch> wrote in message
                news:e51jdGwdDH A.736@TK2MSFTNG P09.phx.gbl...[color=blue]
                > I do not know if I'm off topic. However, I had a crash problem (after a
                > about 1000 calls) when using a callback from C to C# (having delegate in
                > C#).
                > The problem was in the C declaration. Here is a correct sample (watch for
                > the 2nd WINAPI)
                >
                > // in C header file:
                > DLLEXPORT void WINAPI PassExitFnPtr (void (WINAPI * pExit)(int[/color]
                exit_code));[color=blue]
                >
                >
                >
                > José
                >
                > <assafwo@hotmai l.com> a écrit dans le message de
                > news:ub1dlevdDH A.3876@TK2MSFTN GP11.phx.gbl...[color=green]
                > > Nicholas, maybe i don't understand you,
                > >
                > > but, I'm doing it!
                > >
                > > However, my app crashes after about 15 minutes of doing it intesively.
                > >
                > > I fear that since i am not locking down the manged code in any way,
                > > it gets moved around.
                > > The Unmanaged code not being aware of this change, invokes the Managed[/color]
                > code.[color=green]
                > > However it is using the wrong address. And this leads to a crash -
                > > "ExecutionEngin eException".
                > >
                > > assaf
                > >
                > >
                > >
                > >
                > > "Nicholas Paldino [.NET/C# MVP]" <nicholas.paldi no@exisconsulti ng.com>[/color]
                > wrote[color=green]
                > > in message news:eSJ4EbtdDH A.416@tk2msftng p13.phx.gbl...[color=darkred]
                > > > Yoni,
                > > >
                > > > It is not possible from unmanaged C. You will either have to use[/color][/color]
                > the[color=green][color=darkred]
                > > > #pragma managed and #pragma unmanaged directives to enter into managed[/color]
                > > code[color=darkred]
                > > > and make the call, or you will have to marshal the delegate as a[/color][/color]
                > function[color=green][color=darkred]
                > > > pointer, in which case you would have to call it asynchronously,[/color][/color]
                > creating[color=green][color=darkred]
                > > > your own thread, etc, etc. Or, you could export the type as a COM[/color][/color]
                > object[color=green][color=darkred]
                > > > and use that, calling the BeginInvoke method yourself.
                > > >
                > > > Hope this helps.
                > > >
                > > >
                > > > --
                > > > - Nicholas Paldino [.NET/C# MVP]
                > > > - nicholas.paldin o@exisconsultin g.com
                > > >
                > > > "Yoni Rabinovitch" <yoni.rabinovit ch@adc.com> wrote in message
                > > > news:d9c51d85.0 309090511.60494 a75@posting.goo gle.com...
                > > > > Is it possible to invoke a C# delegate/event handler asynchronously,
                > > > > from unmanaged C++ ?
                > > > >
                > > > > I assume this requires a Managed C++ wrapper, which would call
                > > > > BeginInvoke on the delegate ?
                > > > >
                > > > > Is this correct, and if so, does anyone have an example ?
                > > > >
                > > > > Thanks !!
                > > >
                > > >[/color]
                > >
                > >[/color]
                >
                >[/color]


                Comment

                • Leon Finker

                  #9
                  Re: Invoke C# delegate asynchronously from unmanaged C++

                  Hello,

                  Whenever doing p/invoke or interop under .NET 1.1+,
                  I suggest to use CLR SPY:


                  This uses CDP - Customer Debug Probes. More information about these probes
                  can be found at:
                  X:\...\SDK\v1.1 \Tool Developers Guide\Samples\c dp\Managed\Read Me.txt
                  X:\...\SDK\v1.1 \Tool Developers Guide\Samples\c dp\

                  These probes will help you find marshalling related bugs much faster. For
                  example,
                  let's say there is a calling convention mismatch. Here is what you can get
                  with CLR SPY
                  logging:
                  "...PInvoke Calling Convention Mismatch in ConsoleApplicat ion.exe...: Stack
                  imbalance
                  may be caused by incorrect calling convention for method
                  TestFunc(win32d ll.dll)"

                  I hope it helps

                  <assafwo@hotmai l.com> wrote in message
                  news:ub1dlevdDH A.3876@TK2MSFTN GP11.phx.gbl...[color=blue]
                  > Nicholas, maybe i don't understand you,
                  >
                  > but, I'm doing it!
                  >
                  > However, my app crashes after about 15 minutes of doing it intesively.
                  >
                  > I fear that since i am not locking down the manged code in any way,
                  > it gets moved around.
                  > The Unmanaged code not being aware of this change, invokes the Managed[/color]
                  code.[color=blue]
                  > However it is using the wrong address. And this leads to a crash -
                  > "ExecutionEngin eException".
                  >
                  > assaf
                  >
                  >
                  >
                  >
                  > "Nicholas Paldino [.NET/C# MVP]" <nicholas.paldi no@exisconsulti ng.com>[/color]
                  wrote[color=blue]
                  > in message news:eSJ4EbtdDH A.416@tk2msftng p13.phx.gbl...[color=green]
                  > > Yoni,
                  > >
                  > > It is not possible from unmanaged C. You will either have to use[/color][/color]
                  the[color=blue][color=green]
                  > > #pragma managed and #pragma unmanaged directives to enter into managed[/color]
                  > code[color=green]
                  > > and make the call, or you will have to marshal the delegate as a[/color][/color]
                  function[color=blue][color=green]
                  > > pointer, in which case you would have to call it asynchronously,[/color][/color]
                  creating[color=blue][color=green]
                  > > your own thread, etc, etc. Or, you could export the type as a COM[/color][/color]
                  object[color=blue][color=green]
                  > > and use that, calling the BeginInvoke method yourself.
                  > >
                  > > Hope this helps.
                  > >
                  > >
                  > > --
                  > > - Nicholas Paldino [.NET/C# MVP]
                  > > - nicholas.paldin o@exisconsultin g.com
                  > >
                  > > "Yoni Rabinovitch" <yoni.rabinovit ch@adc.com> wrote in message
                  > > news:d9c51d85.0 309090511.60494 a75@posting.goo gle.com...[color=darkred]
                  > > > Is it possible to invoke a C# delegate/event handler asynchronously,
                  > > > from unmanaged C++ ?
                  > > >
                  > > > I assume this requires a Managed C++ wrapper, which would call
                  > > > BeginInvoke on the delegate ?
                  > > >
                  > > > Is this correct, and if so, does anyone have an example ?
                  > > >
                  > > > Thanks !![/color]
                  > >
                  > >[/color]
                  >
                  >[/color]



                  Comment

                  • Yoni Rabinovitch

                    #10
                    Re: Invoke C# delegate asynchronously from unmanaged C++

                    Thanks, everyone, for your input.

                    I should have mentioned that a central requirement of mine was to be
                    able to do this without any DllImport stuff, just straight IJW.

                    In the end, I found an (albeit convoluted) way of doing this, at
                    http://www.codeproject.com/managedcpp/cbwijw.asp.

                    This shows how to synchronously invoke a C# delegate via a callback
                    from unmanaged C++, without any DllImports.

                    It is reasonably straightforward to extend this example to cover
                    asynchronous invocation (BeginInvoke... EndInvoke).

                    Comment

                    • José Joye

                      #11
                      Re: Invoke C# delegate asynchronously from unmanaged C++

                      I'm not too clear with what you said. To me, IJW is not available to C#.
                      Only to managed C++.
                      Am I wrong?

                      José
                      "Yoni Rabinovitch" <yoni.rabinovit ch@adc.com> a écrit dans le message de
                      news:d9c51d85.0 309110723.4dcaa d26@posting.goo gle.com...[color=blue]
                      > Thanks, everyone, for your input.
                      >
                      > I should have mentioned that a central requirement of mine was to be
                      > able to do this without any DllImport stuff, just straight IJW.
                      >
                      > In the end, I found an (albeit convoluted) way of doing this, at
                      > http://www.codeproject.com/managedcpp/cbwijw.asp.
                      >
                      > This shows how to synchronously invoke a C# delegate via a callback
                      > from unmanaged C++, without any DllImports.
                      >
                      > It is reasonably straightforward to extend this example to cover
                      > asynchronous invocation (BeginInvoke... EndInvoke).[/color]


                      Comment

                      • Yoni Rabinovitch

                        #12
                        Re: Invoke C# delegate asynchronously from unmanaged C++

                        "José Joye" <jose.joye@__No _SPam__bluewin_ _maPS_oN__.ch> wrote in message news:<eY4O9UJeD HA.1736@TK2MSFT NGP12.phx.gbl>. ..[color=blue]
                        > I'm not too clear with what you said. To me, IJW is not available to C#.
                        > Only to managed C++.
                        > Am I wrong?
                        >[/color]

                        No, you're right. However, what I meant is that I have unmanaged C++,
                        which I wrap with managed C++. The C# is only "aware" of the managed
                        C++.

                        Now, essentially what I'm doing is to get the unmanaged C++ to
                        callback the C# delegate asynchronously, without using any DllImports
                        in the plumbing for all this.

                        Comment

                        Working...