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 / C++ / MFC
  4. Raise events from unmanaged to managed

Raise events from unmanaged to managed

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
4 Posts 2 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.
  • Y Offline
    Y Offline
    yoti11
    wrote on last edited by
    #1

    Hi, I tried to raise an event from unmanaged code class to a class in managed code. I passed a delegate pointer (using Marshal) to the unmanaged class but I got an error when the unmanaged class tried to execute the function. I tried to use pin_ptr to the delegate pointer with no results. How can I raise event from unmanaged code class to a managed code class using delegate (managed) and function pointer(unmanaged)? Thanks

    L 1 Reply Last reply
    0
    • Y yoti11

      Hi, I tried to raise an event from unmanaged code class to a class in managed code. I passed a delegate pointer (using Marshal) to the unmanaged class but I got an error when the unmanaged class tried to execute the function. I tried to use pin_ptr to the delegate pointer with no results. How can I raise event from unmanaged code class to a managed code class using delegate (managed) and function pointer(unmanaged)? Thanks

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      I don't think that it is possible to raise event directly from unmanaged code, but it is possible to call managed callback function which does this. Use Marshal.GetFunctionPointerForDelegate method to pass managed callbak function address to unmanaged code. Keep delegate instance alive all time when callback function can be called.

      Y 1 Reply Last reply
      0
      • L Lost User

        I don't think that it is possible to raise event directly from unmanaged code, but it is possible to call managed callback function which does this. Use Marshal.GetFunctionPointerForDelegate method to pass managed callbak function address to unmanaged code. Keep delegate instance alive all time when callback function can be called.

        Y Offline
        Y Offline
        yoti11
        wrote on last edited by
        #3

        Thanks for your reply, I've a delegate instance that lives all the time and I sent it to the unmanaged code using Marshal.GetFunctionPointerForDelegate. But I think there is a problem with the GC since it can be reallocated the delegate on the heap. How can it be handle? Thanks

        L 1 Reply Last reply
        0
        • Y yoti11

          Thanks for your reply, I've a delegate instance that lives all the time and I sent it to the unmanaged code using Marshal.GetFunctionPointerForDelegate. But I think there is a problem with the GC since it can be reallocated the delegate on the heap. How can it be handle? Thanks

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          I think that that delegate is pinned internally by this function. .NET doesn't allow to take unmanaged pointers to managed unpinned objects. You can do this manually using GCHandle, as shown in MSDN topic "GCHandle.Alloc Method (Object)", but I think this is exactly what GetFunctionPointerForDelegate does.

          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