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. Managed and unmanaged Classes

Managed and unmanaged Classes

Scheduled Pinned Locked Moved Managed C++/CLI
csharpc++question
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.
  • M Offline
    M Offline
    Marko B L
    wrote on last edited by
    #1

    Hi, I have a question regarding C++/CLI and classes. I can declare a managed class like this: #pragma managed public ref class A { public: void DoSomething(); }; The compiler will translate this one into MSIL or CIL. And I can use this class in any .Net language by adding my compiled dll/exe as a reference e.g. in a C# project. And here's the unmanaged pendant: #pragma unmanaged class B { public: void DoSomething(); }; This is code gets translated into native code. But what happens, if I have the following: #pragma managed class C { public: void DoSomething(); }; What does the compiler exactly with this code? I'm confused :~

    M 1 Reply Last reply
    0
    • M Marko B L

      Hi, I have a question regarding C++/CLI and classes. I can declare a managed class like this: #pragma managed public ref class A { public: void DoSomething(); }; The compiler will translate this one into MSIL or CIL. And I can use this class in any .Net language by adding my compiled dll/exe as a reference e.g. in a C# project. And here's the unmanaged pendant: #pragma unmanaged class B { public: void DoSomething(); }; This is code gets translated into native code. But what happens, if I have the following: #pragma managed class C { public: void DoSomething(); }; What does the compiler exactly with this code? I'm confused :~

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      class C gets compiled to MSIL. Check it out in the disassembler :)

      "Great job, team. Head back to base for debriefing and cocktails." (Spottswoode "Team America")

      M 1 Reply Last reply
      0
      • M Mark Salsbery

        class C gets compiled to MSIL. Check it out in the disassembler :)

        "Great job, team. Head back to base for debriefing and cocktails." (Spottswoode "Team America")

        M Offline
        M Offline
        Marko B L
        wrote on last edited by
        #3

        Hi Marc, thank you for the information. Well, I used Reflector.Net and didn't see these "hyrid" classes. But how is it possible that e.g. the managed class C could inherit from native class B? And if I call call a native function in class C, does it use internally PInvoke to call it? So, is it basically faster to call managed functions/methods from class C than calling native functions?

        M 1 Reply Last reply
        0
        • M Marko B L

          Hi Marc, thank you for the information. Well, I used Reflector.Net and didn't see these "hyrid" classes. But how is it possible that e.g. the managed class C could inherit from native class B? And if I call call a native function in class C, does it use internally PInvoke to call it? So, is it basically faster to call managed functions/methods from class C than calling native functions?

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          class C is not a managed class. It is compiled to MSIL because of the #pragma managed (or by default if no #pragma used and the /CLR compiler switch is used) but that doesn't make it a managed class. I'm not sure about the specifics of how native calls get done from MSIL but I don't think it involves pinvoke since it's not a managed class.

          Ludi83 wrote:

          So, is it basically faster to call managed functions/methods from class C than calling native functions?

          Whether you compile your unmanaged classes to MSIL or native, all the same rules apply when mixing managed and unmanaged classes. Intuitively, any MSIL code should be slightly slower than native C++ code but since it's JIT compiled (not interpreted) there's no noticable difference in speed that I've seen. I've never benchmarked it but I bet the difference is minimal. If performance is an issue, marshalling between managed and unmanaged code is a way bigger issue than how the unmanaged code is compiled. Mark

          "Great job, team. Head back to base for debriefing and cocktails." (Spottswoode "Team America")

          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