Native code for Windows Phone 8

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Windows Phone 8 introduces the ability to use native code to implement Windows Phone. In this section, we explain how you can use C++ in your app, and provide some recommended scenarios.

When to use native code in your app

All Windows Phone 8 apps can use native code but not all apps need to. For many app scenarios, a managed app provides the same features and performance as an app that uses native code. The following are reasons why you may want to use native code in your app.

  • Portability – If you are targeting multiple platforms, it may make sense to implement your core functionality in a native library that can be used on all of the platforms you are targeting.

  • Direct3D graphics – If your app needs to use Direct3D to render graphics to the screen, this must be done using native code.

  • Performance – For some scenarios, you can achieve a performance benefit in large computation tasks by using native code. Note that there is overhead associated with the interop between native and managed code. This overhead may cancel out any performance gains from using native code, so don’t assume that using C++ will automatically increase the performance of your app.

Native code support

Windows Phone 8 supports Visual C++ 2012. This is the full version of C++ that is available on the desktop, and the two versions use the same compiler and IDE. This means that your phone app can reuse existing C and C++ libraries that you have developed for other platforms, in addition to third-party libraries and middleware.

Windows Phone 8 supports a limited set of COM and Win32 APIs that are available on the desktop. For info about which APIs are supported, see Win32 and COM API for Windows Phone 8.

All Windows Phone 8 apps also can use the Windows Phone Runtime, which is a phone-specific implementation of the Windows Runtime, which is used for desktop apps. The Windows Phone Runtime provides infrastructure and an API library. The infrastructure is the core plumbing of the language, including type activation, memory management, a common type system, and a standard programming model that encompasses objects, methods, events, exceptions, and so on. The Windows Phone Runtime library contains phone-specific APIs for features such as speech, managing contacts, location, sensors, networking, and more. In addition, the library supports a subset of the Windows Runtime library that’s available on the desktop. All of these APIs are projected into Visual Basic, C#, and C++ on the phone. For more info about the APIs supported on the phone, see Windows Phone Runtime API.

The Windows Phone Runtime also enables interop between managed and native code. You can add a Windows Phone Runtime component implemented in C++ to your project and then access the objects and methods in the component directly from managed code. Note that there is some overhead cost when using Windows Phone Runtime because of things like reference counting and array bounds checking. For this reason, we recommend that you use it only on the boundary between managed and native. Especially if you are implementing a computation scenario in which performance is key, you should write your computation library in straight C or C++ and then create a Windows Phone Runtime wrapper that can be called from managed code.

Direct3D graphics on Windows Phone 

Windows Phone 8 introduces the ability to use Direct3D in your app, and Direct3D code must be written in C++. You can use the XAML and Direct3D app and the Direct3D with XAML app project templates to create hybrid apps in which the UI is defined in XAML with managed (C# or Visual Basic) code-behind pages. With these apps, a Windows Phone Runtime component contains the native Direct3D code, which renders to the screen within or behind the XAML UI. The Direct3D app project template creates a pure native app that uses no XAML or managed code. Because of this, there are several feature and app model differences between this type of app and a XAML-based app. For guidance on determining which app type is right for your app, see Choosing the right project template for your game for Windows Phone 8.

For info about Direct3D API support on Windows Phone, see Direct3D for Windows Phone 8.

Unsupported native code scenarios

The following uses of native code are not supported for Windows Phone apps.

  • Native code can’t be called from an XNA app.

  • Windows Phone 8 doesn’t support the XAML UI with C++ code-behind app model that is supported for Windows Store apps.

  • Native code can’t be called from Windows Phone OS 7.0 or Windows Phone OS 7.1 apps. Only Windows Phone 8 apps can use this feature.