C++ support from Windows Phone 8
(Yan - - →Direct 3D) |
(Yan - - →Windows Phone runtime (c# ,VB & C++/CX)) |
||
| Line 51: | Line 51: | ||
* '''mixes managed and native code'''. Managed and Native code are incompatible by nature but this API can be consumed by both. | * '''mixes managed and native code'''. Managed and Native code are incompatible by nature but this API can be consumed by both. | ||
This API is based on COM-like technology. Like COM is not user-friendly, Microsoft have created [http://msdn.microsoft.com/library/windows/apps/hh699871.aspx Visual C++ Language Reference (C++/CX)] to Consume this API with C++ and create WinPRT components. This extension adds to C++ managed concept like garbage collector, properties, delegate, event.... To be consumed by manged code, class developed in C++/Cx generate metadata. In fact, VS translate this class to a COM-like Object before generating binary. | This API is based on COM-like technology. Like COM is not user-friendly, Microsoft have created [http://msdn.microsoft.com/library/windows/apps/hh699871.aspx Visual C++ Language Reference (C++/CX)] to Consume this API with C++ and create WinPRT components. This extension adds to C++ managed concept like garbage collector, properties, delegate, event.... To be consumed by manged code, class developed in C++/Cx generate metadata. In fact, VS translate this class to a COM-like Object before generating binary. | ||
| + | {| class="wikitable" | ||
| + | |- | ||
| + | ! with C++/CX !! whitout C++/CX | ||
| + | |- | ||
| + | | | ||
| + | <code cpp>public ref class Number sealed | ||
| + | { | ||
| + | public: | ||
| + | |||
| + | Number() : _value(0) { } | ||
| + | |||
| + | int GetValue() { return _value; } | ||
| + | void SetValue(int value) { _value = value; } | ||
| + | |||
| + | private: | ||
| + | |||
| + | int _value; | ||
| + | }; | ||
| + | </code> | ||
| + | | | | ||
| + | <code cpp n> | ||
| + | [exclusiveto(Number)] | ||
| + | [uuid(5b197688-2f57-4d01-92cd-a888f10dcd90)] | ||
| + | [version(1.0)] | ||
| + | interface INumber : IInspectable | ||
| + | { | ||
| + | HRESULT GetValue([out, retval] INT32* value); | ||
| + | HRESULT SetValue([in] INT32 value); | ||
| + | } | ||
| + | |||
| + | [activatable(1.0), version(1.0)] | ||
| + | runtimeclass Number | ||
| + | { | ||
| + | [default] interface INumber; | ||
| + | } | ||
| + | |||
| + | class Number : public RuntimeClass<INumber> | ||
| + | { | ||
| + | InspectableClass(RuntimeClass_WRLNumberComponent_Number, BaseTrust) | ||
| + | |||
| + | public: | ||
| + | |||
| + | Number() : _value(0) { } | ||
| + | |||
| + | virtual HRESULT STDMETHODCALLTYPE GetValue(INT32* value) override | ||
| + | { | ||
| + | *value = _value; | ||
| + | return S_OK; | ||
| + | } | ||
| + | |||
| + | virtual HRESULT STDMETHODCALLTYPE SetValue(INT32 value) override | ||
| + | { | ||
| + | _value = value; | ||
| + | return S_OK; | ||
| + | } | ||
| + | |||
| + | private: | ||
| + | |||
| + | INT32 _value; | ||
| + | }; | ||
| + | <code> | ||
| + | |} | ||
| + | |||
To develop in C++ you will always use this extension somewhere because it's your C++ code which is consumed by managed API and not the other way round. | To develop in C++ you will always use this extension somewhere because it's your C++ code which is consumed by managed API and not the other way round. | ||
Revision as of 11:51, 22 November 2012
Windows Phone 8 SDK adds two new sets of APIs to develop applications using native code. This article will explain how to use C++ under Windows Phone and some general directions that any C++ developer should know when targeting the platform.
Article Metadata
Tested with
Compatibility
Article
Contents |
Introduction
Windows Phone 8 SDK adds two new sets of APIs to develop applications using native code. This article will explain how to use C++ under Windows Phone and some general directions that any C++ developer should know when targeting the platform.
WIndows Phoen 8 SDK API is divided into three part. These APIs are complementary :
- .Net gives interaction with Windows phone functionality like live tiles, send sms, ...
- Windows Phone runtime is a intermediate API which gives access to low level functionality like voice Command, voip,...
- Native code gives access to low level API like socket, Direct X, ...
For more information read :Windows Phone API reference
.Net (C# & VB)
.Net API is the original Windows Phone framework. It lets you to develop GUI with XAML technologies, access principal functionality (live tiles, tasks, send Mail, sms, ...) and XNA. It's actually the most important framework to develop Windows Phone application. To develop with this framework you can use C# or VB. You can find .net reference documentation here :
Note : XNA is always supported, but only to develop Windows Phone 7 application. It's replaced by Direct3D with Windows Phone 8 .
Windows Phone runtime (c# ,VB & C++/CX)
This API have two functionalities :
- share API with windows 8. A subset of this api is shared with windows 8.
- mixes managed and native code. Managed and Native code are incompatible by nature but this API can be consumed by both.
This API is based on COM-like technology. Like COM is not user-friendly, Microsoft have created Visual C++ Language Reference (C++/CX) to Consume this API with C++ and create WinPRT components. This extension adds to C++ managed concept like garbage collector, properties, delegate, event.... To be consumed by manged code, class developed in C++/Cx generate metadata. In fact, VS translate this class to a COM-like Object before generating binary.
| with C++/CX | whitout C++/CX | |||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
public ref class Number sealed |
ref class myFactory sealed : Windows::ApplicationModel::Core::IFrameworkViewSource
IFrameworkView is the display provider use to make Direct3D rendering. Interface function are :
void myView::Initialize(CoreApplicationView^ applicationView)
void myView::SetWindow(CoreWindow^ window)
void myView::Run() Native application have three importants points:
Without .Net functionality, native applications are not so cool and you are limited to Direct3D develop ... But remember, you can develop WinPRT components with C++/CX. So it's possible to encapsulate your c++ Code with C++/CX and consume it with managed code :p
Other wiki resources about Direct3D development:
Mixed applicationYou can develop mixed application where your managed code consume Windows Phone Runtime components. To develop with C++ Code, you must create a Windows Phone Runtime Component which interface C++ part with a public sealed C++/CX class . It's important to readC++ extension documentation to understand its specificities :
ref class myclass
myclass ^ myClass = ref new myclass ();
To be consumed by managed code, C++/CX code generate a set of metadata. This generation depend on access modifier :
Metadata are generated only for specific C++/CX objects. A C++/CX class can declare C++ object only if the member/function have a private or internal access. Your public Class/Struct must be sealed because Managed code can't override it. //C++/CX class declaration which can be consumed by managed code Public access is very strict, and only specific C++/CX objects can be used : These Objects must be defined with these types :
Once your components is referenced by your managed application, you can consume it like other Managed object. It's so possible to bind public properties with XAML and connect to public events. You can find interesting explanation here :
Warning : Windows phone implement a subset of the Windows 8 C++/CX namespace. Few object are not accessible. CollectionsWindows Phone Runtime API doesn't implement collections class. To transfer collection between managed and native code, a set of collections Interface are defined. These interface have Equivalent in managed. For example IVector besome an IList in C#. C++/CX collections use C++ parts and can't be consumed directly by managed Code. Like these classes implement a Windows Phone Runtime interface, you can cast these to be consumed through the interface Windows::Foundation::Collections::IVector<int>^ Class1::GetInts()
Windows::Foundation::Collections::IVector<int>^ Class1::GetInts() DebuggerYou can't debug managed code and native code in same times. To selected which debugger you want use :
Direct 3DManaged code can't access directly to Direct 3D. You must develop a WinPRT component which consume Direct3D with C++ code. Direct3D rendering can be display by two XAML controls:
Unfortunately, integration with XAML is more complicated. You need a layer of Interopility to acces to Direct3D device ( look Direct3DContentProvider.h class in VS project) ... SDk give two project template :
These projects share important code :
Your class must implement functions which are called by Direct3DContentProvider : // IDrawingSurfaceContentProviderNative this method are called by Direct3DContentProvider to give Direct3D context for rendering. IDrawingSurfaceBackgroundContentProvider and IDrawingSurfaceContentProvider are empty interface. They are only use to associate your class with target UI Controler. But XXX::CreateContentProvider() is mportant : IDrawingSurfaceContentProvider^ Direct3DInterop::CreateContentProvider()
Reference linksRessources about C++ Direct3D developpement on Windon sPhone : The media player is loading... The media player is loading... |



