cast to c++ interface from C#
-
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
-
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
-
So if I declare an interface in C#, can I implement the interface in managed C++ successfully?
Yes, you can find a sample at http://www.codeproject.com/useritems/SessionState.asp. look at the source -> tmpSessionSync project.
-
Yes, you can find a sample at http://www.codeproject.com/useritems/SessionState.asp. look at the source -> tmpSessionSync project.
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
-
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