locked
Using .NET from C++ RRS feed

  • Question

  • After a lot of reading, I'm still confused about using .NET from a C++ Metro style app.

    Many documents note that Metro apps can use a subset of .NET (as well as WinRT, of course). How does this work? Does C++/CX handle all the plumbing, or what?

    Are there any samples?

    Tuesday, February 7, 2012 3:07 PM

Answers

  • No public samples that I know of. If you create a C# Metro Class Library (this is different from the older C# Class Library) you'll see in the Project properties 'Output type' drop down a 'WinMD File' selection. You can use this to build C# to a winmd. Note that this winmd contains both windows metadata and IL (so no need for an assembly dll).

    In the C++/CX project, add the winmd file generated above and then just ref new the class:

    App::App()
    {
        InitializeComponent();
    
    	ClassLibrary1::Class1^ c = ref new ClassLibrary1::Class1();
    	String^ s = c->GetAString();
    }

    Make sure in the Add References dialog that you use the browse button and point directly to the ClassLibrary1.winmd file -- a project reference to ClassLibrary1.csproj will not work.

    You'll also need to add the 'sealed' keyword to the C# class when building as a winmd:

    namespace ClassLibrary1
    {
        public sealed class Class1
        {
            public string GetAString() { return "Hello World"; }
        }
    }

    Thursday, February 9, 2012 11:28 PM
    Moderator

All replies

  • A Metro style app written using C++ does not utilize the .Net framework. It is compiled to native code. No CLR involved. If you are wanting to access the .Net namespaces avalable to a Metro style app, you could build a VB or C# Class Library to consume from your C++ app. This talk demonstrates mixing the two in the same solution (but the reverse of your ask, using C# app with C++ WinMD). I would suggest this talk for any C++ developer getting started with Metro style app development.

    Under the covers with C++ for Metro style apps
    TOOL-690C
    Speakers: Deon Brewis

    Thanks,

    -David

    Tuesday, February 7, 2012 7:02 PM
    Moderator
  • No public samples that I know of. If you create a C# Metro Class Library (this is different from the older C# Class Library) you'll see in the Project properties 'Output type' drop down a 'WinMD File' selection. You can use this to build C# to a winmd. Note that this winmd contains both windows metadata and IL (so no need for an assembly dll).

    In the C++/CX project, add the winmd file generated above and then just ref new the class:

    App::App()
    {
        InitializeComponent();
    
    	ClassLibrary1::Class1^ c = ref new ClassLibrary1::Class1();
    	String^ s = c->GetAString();
    }

    Make sure in the Add References dialog that you use the browse button and point directly to the ClassLibrary1.winmd file -- a project reference to ClassLibrary1.csproj will not work.

    You'll also need to add the 'sealed' keyword to the C# class when building as a winmd:

    namespace ClassLibrary1
    {
        public sealed class Class1
        {
            public string GetAString() { return "Hello World"; }
        }
    }

    Thursday, February 9, 2012 11:28 PM
    Moderator