WinRT: C++ C# interop grief
-
I created a simple WinRT C++ class to help having meaningful exception, it looks like that
public enum class ErrorCodes { CodeStart = 0x0200, CodeEnd = CodeStart + 100, NotErrorCode = CodeEnd - 1, StupideError1 = CodeStart + 1, StupideError2 = CodeStart + 2, }; public ref class Class1 sealed { private: Class1() {} public: static void Throw(ErrorCodes c); static ErrorCodes GetCode(Platform::Exception^ ex); };
However, when I try to use it in C#, with code like that
try { Class1.Throw(ErrorCodes.StupideError1); } catch (Exception ex) { var c = Class1.GetCode(ex); }
I got the following compiler error in my C# project: 2>C:\Dev\W8\App1\App1\App.xaml.cs(42,5,42,8): error CS0012: The type 'Platform.Exception' is defined in an assembly that is not referenced. You must add a reference to assembly 'platform, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime'. 2>C:\Dev\W8\App1\App1\App.xaml.cs(42,13,42,31): error CS1502: The best overloaded method match for 'CppComp.Class1.GetCode(Platform.Exception)' has some invalid arguments 2>C:\Dev\W8\App1\App1\App.xaml.cs(42,28,42,30): error CS1503: Argument 1: cannot convert from 'System.Exception' to 'Platform.Exception' How could I fix that?
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.
-
I created a simple WinRT C++ class to help having meaningful exception, it looks like that
public enum class ErrorCodes { CodeStart = 0x0200, CodeEnd = CodeStart + 100, NotErrorCode = CodeEnd - 1, StupideError1 = CodeStart + 1, StupideError2 = CodeStart + 2, }; public ref class Class1 sealed { private: Class1() {} public: static void Throw(ErrorCodes c); static ErrorCodes GetCode(Platform::Exception^ ex); };
However, when I try to use it in C#, with code like that
try { Class1.Throw(ErrorCodes.StupideError1); } catch (Exception ex) { var c = Class1.GetCode(ex); }
I got the following compiler error in my C# project: 2>C:\Dev\W8\App1\App1\App.xaml.cs(42,5,42,8): error CS0012: The type 'Platform.Exception' is defined in an assembly that is not referenced. You must add a reference to assembly 'platform, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime'. 2>C:\Dev\W8\App1\App1\App.xaml.cs(42,13,42,31): error CS1502: The best overloaded method match for 'CppComp.Class1.GetCode(Platform.Exception)' has some invalid arguments 2>C:\Dev\W8\App1\App1\App.xaml.cs(42,28,42,30): error CS1503: Argument 1: cannot convert from 'System.Exception' to 'Platform.Exception' How could I fix that?
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.
Worked around code by not exposing method using Platform::Exception(), just so:
public enum class ErrorCodes { CodeStart = 0x0200, CodeEnd = CodeStart + 100, NotErrorCode = CodeEnd - 1, NullContext = CodeStart + 1, ContextNotReady = CodeStart + 2, }; public ref class ExHelper sealed { public: static void Throw(ErrorCodes c); static ErrorCodes GetCode(int hr); internal: static Platform::Exception^ CreateException(ErrorCodes c); static ErrorCodes GetCode(Platform::Exception^ ex); private: ExHelper() {} };
Remark Once I referenced platform.winmd in my C# project (from: C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\vcpackages) I got too many compilation errors suddenly appearing!! :wtf:
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.
-
Worked around code by not exposing method using Platform::Exception(), just so:
public enum class ErrorCodes { CodeStart = 0x0200, CodeEnd = CodeStart + 100, NotErrorCode = CodeEnd - 1, NullContext = CodeStart + 1, ContextNotReady = CodeStart + 2, }; public ref class ExHelper sealed { public: static void Throw(ErrorCodes c); static ErrorCodes GetCode(int hr); internal: static Platform::Exception^ CreateException(ErrorCodes c); static ErrorCodes GetCode(Platform::Exception^ ex); private: ExHelper() {} };
Remark Once I referenced platform.winmd in my C# project (from: C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\vcpackages) I got too many compilation errors suddenly appearing!! :wtf:
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.
Interop between languages (aka WinMD libraries/WinRT components) have to use native Windows Runtime types on publicly exposed members and methods.