24

So I look at thisWindows build keynote (Windows build keynote 1:42:56) And I just do not get it - what I can use to create GUI from C++ and/or GUI language that will be capable to call functions from my C++ code? HTML, XAML or what? And where to see code sample of doing markup call code and code create GUI sample with C++ for Windows 8 Metro apps?

Pavel Minaev
  • 99,783
  • 25
  • 219
  • 289
Rella
  • 65,003
  • 109
  • 363
  • 636
  • I think the C and C++ in the picture are to indicate that if you build your GUI using WPF then you can P/Invoke native code, or build COM interfaces to call the native code. – Praetorian Sep 14 '11 at 14:57
  • 3
    FYI there are upcoming BUILD presentations with titles like 'Under the covers with C++ for Metro style apps' 'http://channel9.msdn.com/Events/BUILD/BUILD2011/TOOL-690C – Alex K. Sep 14 '11 at 14:59
  • see also http://stackoverflow.com/questions/7416826/how-does-the-new-windows-8-runtime-compare-to-silverlight-and-wpf – Ian Ringrose Sep 14 '11 at 16:04

3 Answers3

18

Sample code in C++ and other languages is at http://code.msdn.microsoft.com/windowsapps. You can take a look at how it's done.

Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
13

If you want to call C++ code the easiest way will be to use C++ with Component Extensions. This is just plain C++ that compiles to native code however it has a few extensions (reminiscent of C++/CLI) that let you use the WinRT COM components without worrying quite so much about the COM plumbing.

With C++ and WinRT you can actually use XAML like the managed languages to define your user interface. It's pretty neat, see the documentation here:

I haven't looked into it but you may still be able to use P/Invoke or COM interop in the managed languages to call C++ code for a Metro style app however this is unconfirmed. Obviously a desktop app can do all the things it normally would.

Ron Warholic
  • 9,994
  • 31
  • 47
  • 1
    You can use P/Invoke to go from .NET to C++, provided that your C++ function doesn't do anything prohibited by app sandbox. However, a better way to do this kind of thing is to create your own WinRT component in C++ - which is a breeze with the new language extensions - and then consume it from .NET. Try this: create a new C# app solution, then add a C++ project of type "WinRT Component DLL" to it. You can now add a project reference from C# project to C++ one, and all public classes defined using `ref class` will be directly consumable. For the kicks, ILDASM your C# assembly and look inside! – Pavel Minaev Sep 14 '11 at 16:54
  • Definitely cool. Although I have mixed feelings about non-standard C++ extensions the way all the languages worth with WinRT seamlessly is pretty nice. Databinding to XAML from native C++ code is just crazy. – Ron Warholic Sep 14 '11 at 18:09
  • 4
    You don't really need the extensions to write or use WinRT components in C++, they just help a lot compared to something like ATL with not-quite-seamless smart pointers and loads of macros for QueryInterface and friends. Personally, I rather prefer to consider them not as language extensions, but to consider the whole thing as a new language that includes C++ as a complete subset - kinda like how Objective-C is to C. – Pavel Minaev Sep 14 '11 at 18:13
  • you dont need to use P/Invoke to go from .Net to C++. From Documentation " One of the primary features of the new model is the abstract binary interface, or ABI, which defines an interface for inter-language communication. In Windows Developer Preview, native C++ can communicate across the ABI with JavaScript and with the managed .NET languages C# and Visual Basic." – Tinku Sep 15 '11 at 13:24
  • 3
    @Pavel Minaev - exactly. After looking at the documentation and being angry for a moment at the fact that essentially using C++/CLI will be required to write native WinRT apps, I came to the conclusion that _that is_ what .NET could have looked like from the beginning and in this world C# would be completely unnecessary. You can now write cross-platform native code by simply _not_ using the WinRT components and objects. You don't have to change _the language_ completely for that (and don't get me started on Mono begin cross-platform and all - try KeePass2 on OS X). I'm warming up to the idea. – macbirdie Sep 16 '11 at 13:55
  • @macbirdie Indeed, that is the idea. In fact, Herb Sutter in his //BUILD talk specifically recommended that all code that is not directly working with WinRT be written using standard ISO C++ to maximize portability. Use the extensions when you are either invoking into the libraries, extending them (derived controls etc), or writing C++ components for a Metro web app. – Pavel Minaev Sep 17 '11 at 04:14
6

You can use C++ code to write metro style applications. You can also write applications in Javascript/HTML/CSS and call APIs that you write in C++ or C#/VB from those JavaScript applications.

Larry Osterman
  • 16,086
  • 32
  • 60
  • Calling C++ API's from HTML+JS ,if API's are not being JSON/XML server-client oriented, but rather native for windows, would be fun thing to do!) Could you provide any links on that via Metro? – Rella Sep 14 '11 at 21:11
  • 1
    See this talk: http://channel9.msdn.com/Events/BUILD/BUILD2011/TOOL-531T for more information. The link should go live sometime on 9/15/2011 – Larry Osterman Sep 15 '11 at 07:17
  • 1
    Better yet, just try it on Developer Preview. Create a solution with a JavaScript Metro application project, then add a C++ "WinRT Component DLL" project to it. You will notice that you can do "Add Reference" on JS project, and select the C++ project. Build it, then look closely at IntelliSense in JS - you'll notice it pick up the namespace from C++, and explore how the classes inside and their members get projected. – Pavel Minaev Sep 15 '11 at 07:51