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. cast to c++ interface from C#

cast to c++ interface from C#

Scheduled Pinned Locked Moved Managed C++/CLI
helpcsharpc++question
6 Posts 2 Posters 2 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.
  • B Offline
    B Offline
    bwells
    wrote on last edited by
    #1

    I get a runtime error when I cast an object to an interface the class implments. The class and interface are both managed C++, and the cast in done in C#. I have narrowed the problem down to a simple case: 1) C# constructs an object defined in managed C++ 2) C# casts object to a managed C++ interface it implements 3) C# call method on the object after the cast. If I do this, I get a runtime exception. I have seen an invalid cast exception, and sometimes I get a format exception that says "the format specific is not allowed...". It seems like the runtime gets very confused. But instead if I do the following; 1) C# constructs a object defined in managed C++ 2) C# stores object in variable as its actual type; not the interface 3) C# calls method on class it all works. The problem comes when C# casts a managed C++ object to be its interface type (the interface is also defined as a managed C++ interface). I have not found anyone who has done what I am trying to do, and I have not found any documentaion that talks about this, so perhaps its not recommended. Does anyone have experience mixing managed C++ and C# with interfaces defined in C++ and casts to the interface being done form C#? thanks Bryan

    N 1 Reply Last reply
    0
    • B bwells

      I get a runtime error when I cast an object to an interface the class implments. The class and interface are both managed C++, and the cast in done in C#. I have narrowed the problem down to a simple case: 1) C# constructs an object defined in managed C++ 2) C# casts object to a managed C++ interface it implements 3) C# call method on the object after the cast. If I do this, I get a runtime exception. I have seen an invalid cast exception, and sometimes I get a format exception that says "the format specific is not allowed...". It seems like the runtime gets very confused. But instead if I do the following; 1) C# constructs a object defined in managed C++ 2) C# stores object in variable as its actual type; not the interface 3) C# calls method on class it all works. The problem comes when C# casts a managed C++ object to be its interface type (the interface is also defined as a managed C++ interface). I have not found anyone who has done what I am trying to do, and I have not found any documentaion that talks about this, so perhaps its not recommended. Does anyone have experience mixing managed C++ and C# with interfaces defined in C++ and casts to the interface being done form C#? thanks Bryan

      N Offline
      N Offline
      Natty Gur
      wrote on last edited by
      #2

      I got the same experience so i declared the interface in C# Dll.

      B 1 Reply Last reply
      0
      • N Natty Gur

        I got the same experience so i declared the interface in C# Dll.

        B Offline
        B Offline
        bwells
        wrote on last edited by
        #3

        So if I declare an interface in C#, can I implement the interface in managed C++ successfully?

        N 1 Reply Last reply
        0
        • B bwells

          So if I declare an interface in C#, can I implement the interface in managed C++ successfully?

          N Offline
          N Offline
          Natty Gur
          wrote on last edited by
          #4

          Yes, you can find a sample at http://www.codeproject.com/useritems/SessionState.asp. look at the source -> tmpSessionSync project.

          B 1 Reply Last reply
          0
          • N Natty Gur

            Yes, you can find a sample at http://www.codeproject.com/useritems/SessionState.asp. look at the source -> tmpSessionSync project.

            B Offline
            B Offline
            bwells
            wrote on last edited by
            #5

            I am trying your suggestion. But I have one problem. I have defined an C# interface with a method that returns an object. How do I define the implementation for this method in C++? public interface ICOSP_9054_Connection { object getConnection( ); } Originally, when I declared the interface in C++, the method returned a void*. Is there a way to return a void* from a C# method? I can do fine with either approach. The problem is that for some implementors of this method, they will be asking unmanaged C++ code to return an object that I have not defined an interface for so it is easier for me to return a void* (or object). thanks Bryan

            N 1 Reply Last reply
            0
            • B bwells

              I am trying your suggestion. But I have one problem. I have defined an C# interface with a method that returns an object. How do I define the implementation for this method in C++? public interface ICOSP_9054_Connection { object getConnection( ); } Originally, when I declared the interface in C++, the method returned a void*. Is there a way to return a void* from a C# method? I can do fine with either approach. The problem is that for some implementors of this method, they will be asking unmanaged C++ code to return an object that I have not defined an interface for so it is easier for me to return a void* (or object). thanks Bryan

              N Offline
              N Offline
              Natty Gur
              wrote on last edited by
              #6

              public interface ICOSP_9054_Connection { object getConnection( ); } public MyClass : public ICOSP_9054_Connection { public: Object __gc* getConnection() { } };

              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