Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Managed C# DLL called from native C main with callbacks

Managed C# DLL called from native C main with callbacks

Scheduled Pinned Locked Moved C#
csharpc++comhardwaretutorial
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Stuart Rubin
    wrote on last edited by
    #1

    I have been struggling HARD to find a solution to this. In order to get some long-term platform compatibility (PC simulator, embedded target, etc.) I am implementing my main code in C, and a GUI emulator in C# as a DLL. I am able to make calls into the C# DLL from my native code by exposing the DLL functions as COM objects (I think...). Data are passed in and out of the library functions just fine. I would like to have callbacks made back to the native code when events are triggered in the C# code. You can imagine that when a button is clicked on my fancy C# GUI, my native code gets called. I cannot seem to get this callback mechanism working. Can anyone please give an example, syntax, etc. for assigning a native callback to a managed C# event (or something like that)? There are TONS of examples of using managed code to call un-managed DLLs, but very little going to the other way. Thanks in advance. -Stuart

    P 1 Reply Last reply
    0
    • S Stuart Rubin

      I have been struggling HARD to find a solution to this. In order to get some long-term platform compatibility (PC simulator, embedded target, etc.) I am implementing my main code in C, and a GUI emulator in C# as a DLL. I am able to make calls into the C# DLL from my native code by exposing the DLL functions as COM objects (I think...). Data are passed in and out of the library functions just fine. I would like to have callbacks made back to the native code when events are triggered in the C# code. You can imagine that when a button is clicked on my fancy C# GUI, my native code gets called. I cannot seem to get this callback mechanism working. Can anyone please give an example, syntax, etc. for assigning a native callback to a managed C# event (or something like that)? There are TONS of examples of using managed code to call un-managed DLLs, but very little going to the other way. Thanks in advance. -Stuart

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      Stuart, you need to expose your event as described in this[^] example. This creates the COM hookup for the event handler that you can reference through your C code.

      Forgive your enemies - it messes with their heads

      My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

      G S 2 Replies Last reply
      0
      • P Pete OHanlon

        Stuart, you need to expose your event as described in this[^] example. This creates the COM hookup for the event handler that you can reference through your C code.

        Forgive your enemies - it messes with their heads

        My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

        G Offline
        G Offline
        GuyThiebaut
        wrote on last edited by
        #3

        Nice one - I think that is going to come in handy to me some day too.

        Continuous effort - not strength or intelligence - is the key to unlocking our potential.(Winston Churchill)
        1 Reply Last reply
        0
        • P Pete OHanlon

          Stuart, you need to expose your event as described in this[^] example. This creates the COM hookup for the event handler that you can reference through your C code.

          Forgive your enemies - it messes with their heads

          My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

          S Offline
          S Offline
          Stuart Rubin
          wrote on last edited by
          #4

          Peter, this is helpful, but at least half of the challenge is on the native (un-managed) side. I think now I have a syntactic problem with registering my native C function with the managed event. Any more tips? Thanks!

          P 1 Reply Last reply
          0
          • S Stuart Rubin

            Peter, this is helpful, but at least half of the challenge is on the native (un-managed) side. I think now I have a syntactic problem with registering my native C function with the managed event. Any more tips? Thanks!

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            That side is going to require you to hook up to the COM event in your C code. You might want to read this[^] article to see how C++ handles it (it should be easy enough to apply this to C).

            Forgive your enemies - it messes with their heads

            My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

            S 1 Reply Last reply
            0
            • P Pete OHanlon

              That side is going to require you to hook up to the COM event in your C code. You might want to read this[^] article to see how C++ handles it (it should be easy enough to apply this to C).

              Forgive your enemies - it messes with their heads

              My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

              S Offline
              S Offline
              Stuart Rubin
              wrote on last edited by
              #6

              "Calling" the managed functions isn't really a problem anymore. The real problem is passing in a reference to the unamanged event handler function into the managed event. Sorry to keep pushing on this, but does sound like you know what you're doing! Thanks again.

              P 1 Reply Last reply
              0
              • S Stuart Rubin

                "Calling" the managed functions isn't really a problem anymore. The real problem is passing in a reference to the unamanged event handler function into the managed event. Sorry to keep pushing on this, but does sound like you know what you're doing! Thanks again.

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #7

                Have you assigned the IConnectionPoint interface[^]? As you've implemented the event interface in your C#, the CLR Interop automatically applies the juice that hooks this together.

                Forgive your enemies - it messes with their heads

                My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups