Windows desktop development with C++ in Visual Studio
The Windows desktop platform has long been the most popular platform among C++ application developers. With C++ and Visual Studio, you use Windows SDKs to target many versions of Windows ranging from Windows XP to Windows 10, which is well over a billion devices. Popular desktop applications like Microsoft Office, Adobe Creative Suite, and Google Chrome all are built using the same Win32 APIs that serve as the foundation for Windows desktop development. Developing for Windows desktop allows you to reach the highest number of users on any non-mobile development platform.
In this post we will dive into the “Desktop development with C++” workload inside Visual Studio and go over the workflow used to develop a desktop app. For information on developing Universal Windows Platform apps, check out this post.
- Acquiring the tools
- Opening code and building
- Creating new projects
- Editing code
- Debugging and diagnostics
- Working with others
- Windows Store packaging
Acquiring the tools
After installing Visual Studio, open the Visual Studio Installer from the Start menu and navigate to the Workloads Page. We need to install the “Desktop development with C++” workload, which provides the tools needed for developing Windows desktop applications that can run on Windows. The Win32 API model used in these types of applications is the development model used in Windows versions prior to the introduction of the modern Windows API that began with Windows 8. This modern API later evolved into the UWP platform for Windows 10, but traditional desktop development using Windows APIs is still fully supported on all versions of Windows.
When you install the C++ Windows desktop workload, you have many options to customize the installation by selecting your desired tools, Windows SDKs, and other additional features like recent ISO C++ standards candidates such modules support for the STL. The core C++ compiler and libraries for building desktop applications that target x86 and x64 systems are included in the VC++ 2017 v141 toolset (x86, x64). Notable optional tools include support for MFC and C++/CLI development. In the following examples, we will show how to create an MFC app, so this optional component was installed.
Opening code and building
After installing the C++ desktop workload, you can begin coding in existing projects or you can create new ones. Out of the box, Visual Studio can open any folder of code and be configured to build using CMake, a cross-platform build system. The Visual Studio CMake integration even allows you to use another compiler by opening the directory containing your CMakeLists.txt files and let VS do the rest.
Of course ,there is also full support for Microsoft’s own build system called MSBuild, which uses the .vcxproj file format. MSBuild is a robust and fully featured build system that allows building projects in Visual Studio that target Windows. Building an MSBuild-based project just requires a .vcxproj file and can be built in the IDE or by using the command prompt.
In Visual Studio 2017, you can also simply open a folder of code files and immediately begin working in it. In the background, Visual Studio will index your files and providing Intellisense support along with refactoring and all the other navigation aids that you expect. You can create custom .json scripts to specify build configurations.
Creating new projects
If you are creating a new project from scratch, then you can start with one of a variety of project templates.: Each template provides customizable build configurations and boilerplate code that compiles and runs out of the box:
|Project Type (folder)||Description|
|Win32||The Win32 API (also known as the Windows API) is a C-based framework for creating GUI-based Windows desktop applications that have a message loop and react to Windows messages and commands. A Win32 console application has no GUI by default and runs in a console window from the command line.|
|ATL||The Active Template Library is a set of template-based C++ classes that let you create small, fast Component Object Model (COM) objects. COM|
|MFC||Microsoft Foundation Classes is an object oriented wrapper over the Win32 API that provides designers and extensive code-generation support for creating a native UI.|
|CLR||C++/CLI (Common Language Interface) enables efficient communication between native C++ code and .NET code written in languages such as C# or Visual Basic.|
Project templates are included for each of these types of desktop applications depending on the features you select for the workload.
Once you have selected a template, you have the option to customize the project you have selected to create. Each of these project types has a wizard to help you create and customize your new project. The illustrations below show the wizard for an MFC application.
The wizard creates and opens a new project for you and your project files will show up in Solution Explorer.
At this point, even before you write a single line of code, you can build and run the application by pressing F5.
Editing code and navigating
Visual Studio provides many features that help you to code correctly and more efficiently. Whether it be the powerful predictive capabilities provided by IntelliSense or the fluid navigation found in Navigate To there is a feature to make almost any action faster inside Visual Studio. Let Visual Studio do the work for you with autocompletion simply by pressing Tab on the item you want to add from the member list.
You can also hover over any variable, function, or other code symbol and get information about that symbol using the quick info feature.
There are also many great code navigation features to make dealing with large code bases much more productive, including Go To Definition, Go To Line/Symbols/Members/Types, Find All References, View Call Hierarchy, Object Browser, and more. Peek Definition allows you to view the code that defines the selected variable without even having to open another file which minimizes context switching.
We also have support for some of the more common refactoring techniques like rename and extract function that allow you to keep your code looking nice and consistent.
Debugging and Diagnostics
Debugging applications is what Visual Studio is famous for! With a world-class debugging experience that provides a plethora of tools for any type of app, no tool is better suited to debugging applications that target the Windows desktop platform.
Powerful features like advanced breakpoints, custom data visualization, and debug-time profiling enable you to have full control of your app’s execution and pinpoint even the hardest to find bugs. View data values from your code with debugger data tips. Take memory snapshots and diff them to reveal potential memory leaks, and even invoke PageHeap on your app from inside Visual Studio to help solve the notoriously hard problem of memory corruption. Track live CPU and memory usage while your application runs and monitor performance in real-time.
Testing your code
Unit testing is a very popular way of improving code quality, and test-driven-development is fully supported inside Visual Studio. Create new tests and manage them inside the Test Explorer for easy tracking and execution of tests. Writing unit tests is easy and can help find problems as they arise instead of later on when things are harder to isolate. Visual Studio allows both native and managed test project templates for testing native code which can be found in the Visual C++/Test section of the new project templates. Note that the native test template comes with the C++ desktop workload and the managed test comes with the .NET desktop workload.
Working with others
Besides all of the individual developer activities that Visual Studio makes more productive, collaboration is also something that is directly integrated into the IDE. Visual Studio Team Services is a suite of features that optimize the team collaboration process for software development organizations. Create work items, track progress, and manage your bug and open issue database all from inside Visual Studio. Git is fully supported and works seamlessly with the Team Explorer, allowing for easy management of branches, commits, and pull requests.
Simply sign up for a Visual Studio Team Services account for free, then you can track the source code of your desktop applications into Visual Studio Team Services. Visual Studio Team Services also simplifies continuous integrations for your desktop applications. Create and manage build processes that automatically compile and test your apps in the cloud.
Windows Store packaging for desktop apps
When you are ready to deploy your desktop application, you would typically build an executable (.exe) and possibly some libraries so that your application can run on a Windows device. This allows you to easily distribute your application however you like, for example via a download from your website or even through a third-party sales platform such as Steam.
A new option for Windows desktop apps is to be available in the Windows Store with all the advantages that entails. The Desktop Bridge project allows you to package and distribute your Win32 application through the Windows Store alongside Universal Windows Platform apps. When targeting Windows 10, this can provide advantages including streamlined deployment, greater reach, simpler monetization, simplified setup authoring, and differential updates.
Try out Visual Studio 2017 for desktop development with C++!
Download Visual Studio 2017, try it out and share your feedback. For problems, let us know via the Report a Problem option in the upper right corner of the VS title bar. Track your feedback on the developer community portal. For suggestions, let us know through UserVoice.