Category Archives: Book

Targeting Modern C++ and Windows Store apps samples for Windows 8.1 – Part 1 of n

I have previously mentioned that I will not be updating “Modern C++ and Windows Store apps” to have an edition exclusively for Windows 8.1. My reason behind the decision is simple: The concepts and technologies I have introduced in the book continue to be relevant in Windows 8.1 and all of the samples will work on Windows 8.1. Having said that, during Windows 8.1 some WinRT APIs were deprecated and you will see warnings when you compile the samples for Windows 8.1. In this series of blog posts, I will show you how, with little effort and small changes, you can recompile the samples for Windows 8.1.

Note: This is just the first post in a series.

Let us begin with the very first example from the book, the DXBouncingBall sample. When you attempt to open the solution file in Visual Studio 2013, Visual Studio prompts you to retarget the solution for Windows 8.1 as shown in the dialog below. If you choose not to retarget, then you will have to install the Windows 8 development tools in order to build the solution.

For the purpose of this exercise, go ahead and retarget the solution to Windows 8.1 by first clicking on OK in the above dialog and then right click on the project in Solution Explorer and choose Retarget to Windows 8.1 as shown below.

Once you ask Visual Studio to retarget the solution to Windows 8.1, Visual Studio makes the necessary changes and opens the project. Once the project is fully loaded, try building the solution. You should see a series of warning messages indicating the usage of deprecated APIs. The warnings are all listed below.

1>e:\projects\old\dxbouncingball\dxbouncingball\directxpage.xaml.cpp(35): warning C4973: ‘Windows::Graphics::Display::DisplayPropertiesEventHandler’ : marked as deprecated

1> Message: ‘The DisplayProperties type and related event handlers may be altered and or unavailable for releases after Windows 8.1. Instead, use DisplayInformation.’

1> This diagnostic occurred while importing type ‘Windows::Graphics::Display::DisplayPropertiesEventHandler ‘ from assembly ‘Windows, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null’.

1> This diagnostic occurred while importing type ‘Windows::Graphics::Display::DisplayProperties ‘ from assembly ‘Windows, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null’.

1>e:\projects\old\dxbouncingball\dxbouncingball\directxpage.xaml.cpp(35): warning C4973: ‘Windows::Graphics::Display::DisplayProperties::LogicalDpi::get’ : marked as deprecated

1> Message: ‘DisplayProperties may be altered or unavailable for releases after Windows 8.1. Instead, use DisplayInformation.’

1>e:\projects\old\dxbouncingball\dxbouncingball\directxpage.xaml.cpp(41): warning C4973: ‘Windows::Graphics::Display::DisplayPropertiesEventHandler’ : marked as deprecated

1> Message: ‘The DisplayProperties type and related event handlers may be altered and or unavailable for releases after Windows 8.1. Instead, use DisplayInformation.’

1>e:\projects\old\dxbouncingball\dxbouncingball\directxpage.xaml.cpp(41): warning C4973: ‘Windows::Graphics::Display::DisplayProperties::LogicalDpiChanged::add’ : marked as deprecated

1> Message: ‘DisplayProperties may be altered or unavailable for releases after Windows 8.1. Instead, use DisplayInformation.’

1>e:\projects\old\dxbouncingball\dxbouncingball\directxpage.xaml.cpp(44): warning C4973: ‘Windows::Graphics::Display::DisplayPropertiesEventHandler’ : marked as deprecated

1> Message: ‘The DisplayProperties type and related event handlers may be altered and or unavailable for releases after Windows 8.1. Instead, use DisplayInformation.’

1>e:\projects\old\dxbouncingball\dxbouncingball\directxpage.xaml.cpp(44): warning C4973: ‘Windows::Graphics::Display::DisplayProperties::OrientationChanged::add’ : marked as deprecated

1> Message: ‘DisplayProperties may be altered or unavailable for releases after Windows 8.1. Instead, use DisplayInformation.’

1>e:\projects\old\dxbouncingball\dxbouncingball\directxpage.xaml.cpp(47): warning C4973: ‘Windows::Graphics::Display::DisplayPropertiesEventHandler’ : marked as deprecated

1> Message: ‘The DisplayProperties type and related event handlers may be altered and or unavailable for releases after Windows 8.1. Instead, use DisplayInformation.’

1>e:\projects\old\dxbouncingball\dxbouncingball\directxpage.xaml.cpp(47): warning C4973: ‘Windows::Graphics::Display::DisplayProperties::DisplayContentsInvalidated::add’ : marked as deprecated

1> Message: ‘DisplayProperties may be altered or unavailable for releases after Windows 8.1. Instead, use DisplayInformation.’

1>e:\projects\old\dxbouncingball\dxbouncingball\directxpage.xaml.cpp(61): warning C4973: ‘Windows::Graphics::Display::DisplayProperties::LogicalDpi::get’ : marked as deprecated

1> Message: ‘DisplayProperties may be altered or unavailable for releases after Windows 8.1. Instead, use DisplayInformation.’

1>e:\projects\old\dxbouncingball\dxbouncingball\directxbase.cpp(177): warning C4973: ‘Windows::Graphics::Display::DisplayPropertiesEventHandler’ : marked as deprecated

1> Message: ‘The DisplayProperties type and related event handlers may be altered and or unavailable for releases after Windows 8.1. Instead, use DisplayInformation.’

1> This diagnostic occurred while importing type ‘Windows::Graphics::Display::DisplayPropertiesEventHandler ‘ from assembly ‘Windows, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null’.

1> This diagnostic occurred while importing type ‘Windows::Graphics::Display::DisplayProperties ‘ from assembly ‘Windows, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null’.

1>e:\projects\old\dxbouncingball\dxbouncingball\directxbase.cpp(177): warning C4973: ‘Windows::Graphics::Display::DisplayProperties::LogicalDpi::get’ : marked as deprecated

1> Message: ‘DisplayProperties may be altered or unavailable for releases after Windows 8.1. Instead, use DisplayInformation.’

1>e:\projects\old\dxbouncingball\dxbouncingball\directxbase.cpp(184): warning C4973: ‘Windows::Graphics::Display::DisplayProperties::CurrentOrientation::get’ : marked as deprecated

1> Message: ‘DisplayProperties may be altered or unavailable for releases after Windows 8.1. Instead, use DisplayInformation.’

1>e:\projects\old\dxbouncingball\dxbouncingball\directxbase.cpp(214): warning C4973: ‘Windows::Graphics::Display::DisplayProperties::CurrentOrientation::get’ : marked as deprecated

1> Message: ‘DisplayProperties may be altered or unavailable for releases after Windows 8.1. Instead, use DisplayInformation.’

Time for action! If you were to closely observe the warning messages, it should be pretty clear that there is one runtimeclass and some methods/properties/events associated with that particular class that are generating the warnings. That class happens to be the Windows::Graphics::Display::DisplayProperties. I will not go into the finer details of why this class has been deprecated in Windows 8.1 but for a teaser, it has to do with better multi-mon support J. Anyways back to our exercise.

The warning also informs us to use the newer class, DisplayInformation. This is one of the benefits of the great Intellisense support for WinRT apps in Visual Studio. All we need to do now, is replace the usage of DisplayProperties with DisplayInformation. Let’s begin the steps. I am showing you a table of the Windows 8 code and the new Windows 8.1 code to make the illustration easier. Each item in the table corresponds to the warnings in the order they appear.

 

File Name

Line Number in Windows 8 Code

Windows 8 Code

Windows 8.1 Code

DirectXPage.xaml.cpp

35

m_renderer->Initialize(

Window::Current->CoreWindow,

SwapChainPanel,

DisplayProperties::LogicalDpi

);

auto info = DisplayInformation::GetForCurrentView();

 

m_renderer->Initialize(


Window::Current->CoreWindow,

SwapChainPanel,

info->LogicalDpi

);

DirectXPage.xaml.cpp

41

DisplayProperties::LogicalDpiChanged +=


ref
new DisplayPropertiesEventHandler(this, &DirectXPage::OnLogicalDpiChanged);

info->DpiChanged +=


ref
new
TypedEventHandler<DisplayInformation^, Object^>( this, &DirectXPage::OnLogicalDpiChanged );

DirectXPage.xaml.cpp

44

DisplayProperties::OrientationChanged +=


ref
new DisplayPropertiesEventHandler(this, &DirectXPage::OnOrientationChanged);

info->OrientationChanged +=


ref
new
TypedEventHandler<DisplayInformation^, Object^>( this, &DirectXPage::OnOrientationChanged );

DirectXPage.xaml.cpp

47

DisplayProperties::DisplayContentsInvalidated +=


ref
new DisplayPropertiesEventHandler(this, &DirectXPage::OnDisplayContentsInvalidated);

info->DisplayContentsInvalidated +=


ref
new
TypedEventHandler<DisplayInformation^, Object^>( this, &DirectXPage::OnDisplayContentsInvalidated );

DirectXPage.xaml.cpp

61

m_renderer->SetDpi(DisplayProperties::LogicalDpi);

m_renderer->SetDpi( DisplayInformation::GetForCurrentView()->LogicalDpi );

DirectXBase.cpp

177

if (m_dpi != DisplayProperties::LogicalDpi)

{


return;

}

if ( m_dpi != DisplayInformation::GetForCurrentView()->LogicalDpi )

{


return;

}

DirectXBase.cpp

184

m_orientation != DisplayProperties::CurrentOrientation

m_orientation != DisplayInformation::GetForCurrentView()->CurrentOrientation

DirectXBase.cpp

214

m_orientation = DisplayProperties::CurrentOrientation

m_orientation = DisplayInformation::GetForCurrentView()->CurrentOrientation

DirectXPage.xaml.h

41-43

void OnLogicalDpiChanged(Platform::Object^ sender);

void OnOrientationChanged(Platform::Object^ sender);

void OnDisplayContentsInvalidated(Platform::Object^ sender);

void OnLogicalDpiChanged( Windows::Graphics::Display::DisplayInformation^ sender, Platform::Object^ args );

 

void OnOrientationChanged( Windows::Graphics::Display::DisplayInformation^ sender, Platform::Object^ args );

 

void OnDisplayContentsInvalidated( Windows::Graphics::Display::DisplayInformation^ sender, Platform::Object^ args );

 

Change the method signatures appropriately in the DirectXPage.xaml.cpp too and rebuild the solution. It should build clean with no warnings and your app should work on Windows 8.1 without any issues.

Try to resize the application window to different sizes and see how resizing and redrawing are all handled smoothly without having to explicitly write code. Is it not cool to write future proof code? J

 

Have fun!

-Sridhar

Will there be an update for “Modern C++ and Windows Store apps” for Windows 8.1?

With Windows 8.1 now released as a Consumer Preview enabling developers to try out and begin building apps for the new Operating System, I wanted to let folks know if there is an updated version of my book, “Modern C++ and Windows Store apps” under development.

When I began contemplating writing a book about Windows 8, I did not want to just show how to build Windows Store apps using C++ /CX and XAML. That would probably have been a great disservice to C++ developers since it would have restricted the scope of the book to just XAML. Don’t get me wrong. XAML is a great UX framework but I wanted to show how the power and flexibility of C++ and C++ /CX can be used to build a vide variety of apps that could be run both on the Desktop and Modern.

The more I thought about what to talk about in the book, the more my ideas began to crystallize around narrating the “C++ Renaissance” at Microsoft and in the industry. Discussing the “C++ Renaissance” is a vast topic in itself and I had to make a few hard decisions on the list of topics. After multiple deliberations, I ended up discussing the following topics:

  • Show the simplicity of developing Windows Store apps using C++ /CX
  • Important concepts of C++11
  • Introduction to C++ /CX
  • A High level tour of XAML
  • Using C++ /CX to combine XAML and DirectX
  • Advanced GPGPU Programming by using C++AMP in Desktop and Windows Store apps.
      • Under the covers of the Windows Runtime and the Windows Runtime Library (WRL)
      • Introducing the new native unit test support in VS2012.
      • Debugging tips for Windows Store apps
      • Performance tips for using XAML
      • Introduction to Windows Azure Mobile Services
      • A sneak preview of Windows Phone 8 “shared code”.

One of the ideas that I steadfastly took while developing samples and the narration was to focus explaining how C++ developers should be using XAML/DirectX interop techniques to make their apps stand apart from pure XAML or WWA counterparts. XAML/DirectX interop is really the “super pill” that can help C++ developers take full advantage of both the retained mode benefits of XAML and the immediate mode rendering provided by DirectX. When coupled with C++AMP, this has the added advantage of eking out maximum performance from the available hardware for all of your workloads.

With Windows 8.1, while XAML got a host of new controls, using those controls by itself is not an impossible task if you have already worked your way through my book. There simply is no point creating a new version of the book, when the fundamentals have already been discussed. All of the new updates are important but they are not breaking changes or stuff that is hard to incorporate. What is really interesting is the changes to the XAML/DirectX interop types. Does it warrant the creation of a new version of the book to explain 4 new interop types? I don’t think so. It, however, does merit explanation. So, going forward, I will begin posting content about the updated XAML/DirectX interop types. I will also update the samples from my book to show how to move code from Windows 8 to Windows 8.1. I hope that is of more value to readers instead of coming up with a new version of the book. What do you think? Please do let me know.

If you have noticed the topics I discussed in my book, attentive readers must have noticed that I did not discuss any communications or network API in the book. Is it a harbinger for things to come in future? Stay tuned for updates Smile

-Sridhar

The Windows Store opportunity for C++ developers

One of my colleagues sent this statement my way and I quote him “The advent of Windows Store combines a great app-development infrastructure with a reach of 250 million people and counting. This is a great opportunity for seasoned and new app developers alike. While Microsoft has published great examples, tutorials, and help, a more detailed and focused resource like the book, “Modern C++ and Windows Store apps” is a fantastic contribution to help developers profit from this opportunity.”

Many thanks Mike Wehinger

-Sridhar

Modern C++ and Windows Store apps giveaway – An update

I wanted to provide a quick update on the winning responses so far to book contest I have announced a few weeks back. I am humbled by the responses I have received so far. The winning responses, from the mails I have received until now are:

I am excited to apply modern C++ to code snappy and responsive Windows 8 metro apps using asynchronous functions, now wonderfully described in Sridhar’s book on Windows 8 application programming. I’ve been trying to find the best sources of information for Windows 8 application programming to write apps that work on phone/tablet and PC.  I plan to write applications that are education games or tools related to biology.”

“Thanks for writing a book on using modern C++ with Windows 8. I am a long time C++ developer, having worked for many years with Symbian and Qt. This book has come at the right time for me to update my skills 🙂 The fascinating thing about Windows has always been its reach. No question about it. But with Windows 8, I feel that Microsoft is now at the forefront of design as well. I love the way the “modern” apps look and perform on my tablet, and I am amazed by their performance even on this old device. Modern C++ provides me with an easy pathway to the Windows 8 ecosystem. Ever since Microsoft released the source for Project Austin, I have been looking at the possibility of developing an application for Windows 8 with C++. With the release of your book, I hope to learn a few tricks which will make my task much easier.

“My name is XXXX and I work for a XYZ company and write software that processes large amounts of data and visualizes it using various plot types such as scatter, histogram, density, and contour plots. The software also controls the hardware during run-time data acquisition. Currently the software runs on the desktop using WPF and C#. As the data rates and file sizes increase I am starting to hit the threshold of the C# – .Net technology. I am now seriously considering writing the software in C++ 11 and Windows Store App. Previously I did some prototyping with C++/CLI and WPF, and the performance while better was not enough to justify the move. The boundary between C++ and C# forced some design compromises and to be honest had some performance problems. With WinRT C++/CX and native XAML the story becomes very intriguing. While there are still boundary problems to aware of (avoid ‘chatty’ api’s) I believe the design will be much cleaner and have much better performance. Additionally I have been working with C++ Amp to do a lot of parallel processing. The results have been very encouraging. It seems as though the stars have aligned in terms of performance (C++, and AMP), Fast and Fluid UI (Windows Store App), and my existing knowledge of Xaml and WPF. I believe this combination will give me the opportunity to write some great software that performs well, and looks great and runs on tablet and eventually mobile (reduced function set.)  I think that the combination will truly give me full power at my fingertips to extract the most from the hardware available. I am looking for the knowledge of how to best use Modern C++ with the new WinRT framework to quickly come come up to speed. ”

 

I still have 6 copies to giveaway. If you want a copy, please do let me know.

-Sridhar