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. Managed C++/CLI
  4. Execute managed function from unmanaged

Execute managed function from unmanaged

Scheduled Pinned Locked Moved Managed C++/CLI
helpcomdotnetdata-structuresquestion
5 Posts 2 Posters 1 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

    Hello, I’m trying to send delegate to unmanaged code, so when the unmanaged class will catch an event it will execute the proper function in the managed class. I used GCHandle::Alloc() to prevent from the GC to change the position on the heap and Marshel::GetFunctionPointerForDelegate()to convert the delegate to unmanaged function pointer. The main problem is that an error occurred while I tries to execute the function pointer in the unmanage code to the managed code I get that error. “ Managed Debugging Assistant 'FatalExecutionEngineError' has detected a problem in 'D:\Dev\Test_TA.exe'. Additional Information: The runtime has encountered a fatal error. The address of the error was at 0x79f1c189, on thread 0x910. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack. “ Did anyone encounter this problem? Is it a problem with the Marshel::GetFunctionPointerForDelegate()? Or with the GC? Thanks,

    L 1 Reply Last reply
    0
    • Y yoti11

      Hello, I’m trying to send delegate to unmanaged code, so when the unmanaged class will catch an event it will execute the proper function in the managed class. I used GCHandle::Alloc() to prevent from the GC to change the position on the heap and Marshel::GetFunctionPointerForDelegate()to convert the delegate to unmanaged function pointer. The main problem is that an error occurred while I tries to execute the function pointer in the unmanage code to the managed code I get that error. “ Managed Debugging Assistant 'FatalExecutionEngineError' has detected a problem in 'D:\Dev\Test_TA.exe'. Additional Information: The runtime has encountered a fatal error. The address of the error was at 0x79f1c189, on thread 0x910. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack. “ Did anyone encounter this problem? Is it a problem with the Marshel::GetFunctionPointerForDelegate()? Or with the GC? Thanks,

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

      http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1034501&SiteID=1[^]

      Y 1 Reply Last reply
      0
      • L Lost User

        http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1034501&SiteID=1[^]

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

        Hi, Thanks for your reply. I already try that before, but it my case the program structure is little different. The problem is that the program has 2 phases: 1. Managed class creates a delegate and sends it to unmanaged class which assigns it to a function pointer member. 2. The unmanaged class listens to events from other place and when a specific event occurred then the unmanaged class should call the function in the managed class. (From the first phase) Maybe there is no different between cases but I still get this error. Thank you,

        L 1 Reply Last reply
        0
        • Y yoti11

          Hi, Thanks for your reply. I already try that before, but it my case the program structure is little different. The problem is that the program has 2 phases: 1. Managed class creates a delegate and sends it to unmanaged class which assigns it to a function pointer member. 2. The unmanaged class listens to events from other place and when a specific event occurred then the unmanaged class should call the function in the managed class. (From the first phase) Maybe there is no different between cases but I still get this error. Thank you,

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

          This is almost same case. The only difference that EnumWindows calls managed callback number of times inside of method, and keeping local delegate instance is enough. In your case you need to keep delegate as class member. I think the problem is in function prototype. What is required unmanaged prototype and managed callback prototype? In my program I use another method for managed callback - because I didn't know about this Marshal function when started to write it. Code fragments: GCHandle gcHandle; // managed class member // Constructor: gcHandle = GCHandle::Alloc(this); void* callbackPtr = GCHandle::ToIntPtr(gcHandle).ToPointer(); pUnmanagedClass->SetCallbackAddress(callbackPtr); // Destructor: gcHandle.Free(); Pass callbackPtr value to unmanaged class. This is simple unmanaged void* pointer, which allows to call managed class function when necessary. // Unmanaged class calls managed class: GCHandle h = GCHandle::FromIntPtr(IntPtr(callbackPtr)); // callbackPtr is passed from managed class YourManagedClass^ pThis = (YourManagedClass^)h.Target; pThis->CallbackFunction();

          Y 1 Reply Last reply
          0
          • L Lost User

            This is almost same case. The only difference that EnumWindows calls managed callback number of times inside of method, and keeping local delegate instance is enough. In your case you need to keep delegate as class member. I think the problem is in function prototype. What is required unmanaged prototype and managed callback prototype? In my program I use another method for managed callback - because I didn't know about this Marshal function when started to write it. Code fragments: GCHandle gcHandle; // managed class member // Constructor: gcHandle = GCHandle::Alloc(this); void* callbackPtr = GCHandle::ToIntPtr(gcHandle).ToPointer(); pUnmanagedClass->SetCallbackAddress(callbackPtr); // Destructor: gcHandle.Free(); Pass callbackPtr value to unmanaged class. This is simple unmanaged void* pointer, which allows to call managed class function when necessary. // Unmanaged class calls managed class: GCHandle h = GCHandle::FromIntPtr(IntPtr(callbackPtr)); // callbackPtr is passed from managed class YourManagedClass^ pThis = (YourManagedClass^)h.Target; pThis->CallbackFunction();

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

            I still get the error Managed Debugging Assistant 'FatalExecutionEngineError' has detected a problem in 'D:\Dev\Test_TA.exe'. Additional Information: The runtime has encountered a fatal error. The address of the error was at 0x79f1c189, on thread 0x910. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack. This error ocured when the function pointer in the unmanaged code call to the function in the managed code. In the managed code I uesd GCHandle::Aloc(this). thanks

            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