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. Visual Studio 2015 & .NET 4.6
  4. WinRT: C++ C# interop grief

WinRT: C++ C# interop grief

Scheduled Pinned Locked Moved Visual Studio 2015 & .NET 4.6
helpcsharpc++wpfcom
3 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.
  • S Offline
    S Offline
    Super Lloyd
    wrote on last edited by
    #1

    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.

    S 1 Reply Last reply
    0
    • S Super Lloyd

      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.

      S Offline
      S Offline
      Super Lloyd
      wrote on last edited by
      #2

      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.

      F 1 Reply Last reply
      0
      • S Super Lloyd

        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.

        F Offline
        F Offline
        FrankLavigne
        wrote on last edited by
        #3

        Interop between languages (aka WinMD libraries/WinRT components) have to use native Windows Runtime types on publicly exposed members and methods.

        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