-->
This walkthrough shows how to create a traditional Windows desktop application in Visual Studio. The example application you'll create uses the Windows API to display 'Hello, Windows desktop!' in a window. You can use the code that you develop in this walkthrough as a pattern to create other Windows desktop applications.
The Windows API (also known as the Win32 API, Windows Desktop API, and Windows Classic API) is a C-language-based framework for creating Windows applications. It has been in existence since the 1980s and has been used to create Windows applications for decades. More advanced and easier-to-program frameworks have been built on top of the Windows API. For example, MFC, ATL, the .NET frameworks. Even the most modern Windows Runtime code for UWP and Store apps written in C++/WinRT uses the Windows API underneath. For more information about the Windows API, see Windows API Index. There are many ways to create Windows applications, but the process above was the first.
Important
Apr 21, 2017 Are you new to Visual Studio and working with C? Then you’ve come to the right place. Whether you’re a student writing one of your first programs or a seasoned C developer with years of experience, you’ll find Visual Studio to be a powerful environment for C development. Using GCC with MinGW. In this tutorial, you configure Visual Studio Code to use the GCC C compiler (g) and GDB debugger from Mingw-w64 to create programs that run on Windows. After configuring VS Code, you will compile and debug a simple Hello World program in VS Code.
For the sake of brevity, some code statements are omitted in the text. The Build the code section at the end of this document shows the complete code.
Prerequisites
A computer that runs Microsoft Windows 7 or later versions. We recommend Windows 10 for the best development experience.
A copy of Visual Studio. For information on how to download and install Visual Studio, see Install Visual Studio. When you run the installer, make sure that the Desktop development with C++ workload is checked. Don't worry if you didn't install this workload when you installed Visual Studio. You can run the installer again and install it now.
An understanding of the basics of using the Visual Studio IDE. If you've used Windows desktop apps before, you can probably keep up. For an introduction, see Visual Studio IDE feature tour.
An understanding of enough of the fundamentals of the C++ language to follow along. Don't worry, we don't do anything too complicated.
Create a Windows desktop project
Follow these steps to create your first Windows desktop project. As you go, you'll enter the code for a working Windows desktop application. To see the documentation for your preferred version of Visual Studio, use the Version selector control. It's found at the top of the table of contents on this page.
To create a Windows desktop project in Visual Studio 2019
From the main menu, choose File > New > Project to open the Create a New Project dialog box.
At the top of the dialog, set Language to C++, set Platform to Windows, and set Project type to Desktop.
From the filtered list of project types, choose Windows Desktop Wizard then choose Next. In the next page, enter a name for the project, for example, DesktopApp.
Choose the Create button to create the project.
The Windows Desktop Project dialog now appears. Under Application type, select Desktop application (.exe). Under Additional options, select Empty project. Choose OK to create the project.
In Solution Explorer, right-click the DesktopApp project, choose Add, and then choose New Item.
In the Add New Item dialog box, select C++ File (.cpp). In the Name box, type a name for the file, for example, HelloWindowsDesktop.cpp. Choose Add.
Your project is now created and your source file is opened in the editor. To continue, skip ahead to Create the code.
Convert Dev C++ To Visual Studios
To create a Windows desktop project in Visual Studio 2017
On the File menu, choose New and then choose Project.
In the New Project dialog box, in the left pane, expand Installed > Visual C++, then select Windows Desktop. In the middle pane, select Windows Desktop Wizard.
In the Name box, type a name for the project, for example, DesktopApp. Choose OK.
In the Windows Desktop Project dialog, under Application type, select Windows application (.exe). Under Additional options, select Empty project. Make sure Precompiled Header isn't selected. Choose OK to create the project.
In Solution Explorer, right-click the DesktopApp project, choose Add, and then choose New Item.
In the Add New Item dialog box, select C++ File (.cpp). In the Name box, type a name for the file, for example, HelloWindowsDesktop.cpp. Choose Add.
Your project is now created and your source file is opened in the editor. To continue, skip ahead to Create the code.
To create a Windows desktop project in Visual Studio 2015
On the File menu, choose New and then choose Project.
In the New Project dialog box, in the left pane, expand Installed > Templates > Visual C++, and then select Win32. In the middle pane, select Win32 Project.
In the Name box, type a name for the project, for example, DesktopApp. Choose OK.
On the Overview page of the Win32 Application Wizard, choose Next.
On the Application Settings page, under Application type, select Windows application. Under Additional options, uncheck Precompiled header, then select Empty project. Choose Finish to create the project.
In Solution Explorer, right-click the DesktopApp project, choose Add, and then choose New Item.
In the Add New Item dialog box, select C++ File (.cpp). In the Name box, type a name for the file, for example, HelloWindowsDesktop.cpp. Choose Add.
Your project is now created and your source file is opened in the editor.
Create the code
Next, you'll learn how to create the code for a Windows desktop application in Visual Studio.
To start a Windows desktop application
Just as every C application and C++ application must have a
main
function as its starting point, every Windows desktop application must have aWinMain
function.WinMain
has the following syntax.For information about the parameters and return value of this function, see WinMain entry point.
Note
What are all those extra words, such as
CALLBACK
, orHINSTANCE
, or_In_
? The traditional Windows API uses typedefs and preprocessor macros extensively to abstract away some of the details of types and platform-specific code, such as calling conventions, __declspec declarations, and compiler pragmas. In Visual Studio, you can use the IntelliSense Quick Info feature to see what these typedefs and macros define. Hover your mouse over the word of interest, or select it and press Ctrl+K, Ctrl+I for a small pop-up window that contains the definition. For more information, see Using IntelliSense. Parameters and return types often use SAL Annotations to help you catch programming errors. For more information, see Using SAL Annotations to Reduce C/C++ Code Defects.Windows desktop programs require <windows.h>. <tchar.h> defines the
TCHAR
macro, which resolves ultimately to wchar_t if the UNICODE symbol is defined in your project, otherwise it resolves to char. If you always build with UNICODE enabled, you don't need TCHAR and can just use wchar_t directly.Along with the
WinMain
function, every Windows desktop application must also have a window-procedure function. This function is typically namedWndProc
, but you can name it whatever you like.WndProc
has the following syntax.In this function, you write code to handle messages that the application receives from Windows when events occur. For example, if a user chooses an OK button in your application, Windows will send a message to you and you can write code inside your
WndProc
function that does whatever work is appropriate. It's called handling an event. You only handle the events that are relevant for your application.For more information, see Window Procedures.
To add functionality to the WinMain function
In the
WinMain
function, you populate a structure of type WNDCLASSEX. The structure contains information about the window: the application icon, the background color of the window, the name to display in the title bar, among other things. Importantly, it contains a function pointer to your window procedure. The following example shows a typicalWNDCLASSEX
structure.For information about the fields of the structure above, see WNDCLASSEX.
Register the
WNDCLASSEX
with Windows so that it knows about your window and how to send messages to it. Use the RegisterClassEx function and pass the window class structure as an argument. The_T
macro is used because we use theTCHAR
type.Now you can create a window. Use the CreateWindow function.
This function returns an
HWND
, which is a handle to a window. A handle is somewhat like a pointer that Windows uses to keep track of open windows. For more information, see Windows Data Types.At this point, the window has been created, but we still need to tell Windows to make it visible. That's what this code does:
The displayed window doesn't have much content because you haven't yet implemented the
WndProc
function. In other words, the application isn't yet handling the messages that Windows is now sending to it.To handle the messages, we first add a message loop to listen for the messages that Windows sends. When the application receives a message, this loop dispatches it to your
WndProc
function to be handled. The message loop resembles the following code.For more information about the structures and functions in the message loop, see MSG, GetMessage, TranslateMessage, and DispatchMessage.
At this point, the
WinMain
function should resemble the following code.
To add functionality to the WndProc function
To enable the
WndProc
function to handle the messages that the application receives, implement a switch statement.One important message to handle is the WM_PAINT message. The application receives the
WM_PAINT
message when part of its displayed window must be updated. The event can occur when a user moves a window in front of your window, then moves it away again. Your application doesn't know when these events occur. Only Windows knows, so it notifies your app with aWM_PAINT
message. When the window is first displayed, all of it must be updated.To handle a
WM_PAINT
message, first call BeginPaint, then handle all the logic to lay out the text, buttons, and other controls in the window, and then call EndPaint. For the application, the logic between the beginning call and the ending call is to display the string 'Hello, Windows desktop!' in the window. In the following code, notice that the TextOut function is used to display the string.HDC
in the code is a handle to a device context, which is a data structure that Windows uses to enable your application to communicate with the graphics subsystem. TheBeginPaint
andEndPaint
functions make your application behave like a good citizen and doesn't use the device context for longer than it needs to. The functions help make the graphics subsystem is available for use by other applications.An application typically handles many other messages. For example, WM_CREATE when a window is first created, and WM_DESTROY when the window is closed. The following code shows a basic but complete
WndProc
function.
Build the code
As promised, here's the complete code for the working application.
To build this example
Delete any code you've entered in HelloWindowsDesktop.cpp in the editor. Copy this example code and then paste it into HelloWindowsDesktop.cpp:
On the Build menu, choose Build Solution. The results of the compilation should appear in the Output window in Visual Studio.
To run the application, press F5. A window that contains the text 'Hello, Windows desktop!' should appear in the upper-left corner of the display.
Congratulations! You've completed this walkthrough and built a traditional Windows desktop application.
Visual Studio C++ Download
See also
A step-by-step guide to moving Microsoft VS projects to Eclipse C/C++ Development Toolkit
The Eclipse Platform is an open source tool to assist you with moving a project from the design to the test phase within a single development environment and without the need for separate tools for each stage. Eclipse was developed to assist the tools development community, concentrating on the core functionality of the tool instead of concentrating on the IDE itself. This is achievable because of programming model of Eclipse, which supports software building blocks called plug-ins.
Originally, Eclipse started with Java programming IDE, as it was the need of the hour. Because of its extensibility, it was embraced for developing applications for other programming languages, such as COBOL and C/C++.
The focus of this article is on C and C++ applications. Most C and C++ projects for deployment on Windows are developed using Microsoft Visual Studio. This article provides a step-by-step procedure for migrating Microsoft Visual Studio C/C++ (MSVC) projects to Eclipse. Along the way, we compare and contrast the benefits of using MSVC and Eclipse CDT.
Prerequisites
- Eclipse Platform
- Download Eclipse from the Eclipse Foundation.
- Eclipse C++ Development Toolkit (CDT)
- Get this Eclipse plug-in for C and C++ development (see About CDT).
- Visual Studio/Platform SDK
- This should be your current development IDE for C/C++ applications in Windows. The recent version of Platform SDK (MSVC Express) is undergoing some changes that restricts you to building executables in the conventional way using an nmake makefile. This article assumes you are building the executable through a makefile. Be sure to identify the version you are using.
See Related topics for links to download each.
Visual Studio and Eclipse compared
Microsoft Visual Studio has a long history with wide use among Windows application developers. It is the leader in C and C++ Microsoft Windows application development, as well. By contrast, Eclipse CDT is relatively new and quite popular these days in the developers community. Eclipse CDT is designed to satisfy the developers of many OS platforms, such as Linux®, and is dependent on the gcc compiler and other open source tools.
There are many ways of comparing Eclipse with Visual Studio. One can compare the user interface (UI), architecture, cost, manageability, and many other criteria. Since this article looks at C and C++ development, we compare and contrast the strengths of both the tools with respect to C and C++ application development for Windows.
C/C++ development compared on Windows
Microsoft Visual Studio C/C++ | Eclipse CDT | |
---|---|---|
Nature of license | Proprietary | Open source |
Programming languages supported | Specific to C/C++ and Microsoft-supported programming languages | Parallel different programming languages (COBOL, the Java programming language, and others), along with C/C++ |
SDLC phases supported | Only coding | Designing, coding, configuration management, testing, etc. |
Debugging | Complete debugging support | No debugging support; debugging support available only for gcc-compiled C code on platforms other than Windows. Microsoft Debugging Tools for Windows required |
Resource editor | Comprehensive resource editor | No resource editor |
Testing | No testing tools as part of Visual Studio; third-party tools must be used | CppUnit can be used (see Related topics for more information) |
MFC programming | Development support for MFC application (wizard, code templates, etc.) | No support |
Plug-in tools development | Visual Studio 6 does not support any plugable software components | Based on a plug-in architecture |
Tools specific to Microsoft Windows | Spy++, error look-up, ActiveX Container, and others | No tools specific for Windows platforms |
About CDT
Eclipse C++ Development Toolkit (CDT) is an extension to the Eclipse platform in the form of a plug-in. This plug-in is available for download for all platforms. The open source nature of the plug-in with its user-friendliness makes it more popular not just among the Linux developers but also among C++ developers on other platforms. CDT and the Web Tools plug-ins are the two most popular Eclipse plug-ins. Nearly two out of three developers using CDT are Windows users.
CDT has subcomponents or plug-ins that are independent projects in the CDT community. The most important is the CDT primary plug-in, which provides the core CDT capabilities. CDT Debug UI provides the UI capabilities for the debug editors and views. CDT UI plug-in provides the UI-related features, views, editors, wizards, etc. CDT Debug provides core debugging capabilities. CDT Feature provides CDT Feature component. CDT core presents Core Model, CDOM, and other core components. CDT Launch provides launch mechanism for launching external executables and tools. CDT Debug MI is the application connector for MI-compatible debuggers.
CDT editors have several features that make them popular. For example, syntax highlighting and code assist make software development quick and easy. Syntax highlighting is configurable and can be personalized to your individual taste. Code assist is the code completion feature that is similar to the one in Visual Studio. Custom-defined code templates can be added to the plug-in, which can be used by code assist.
In the following sections, we will learn how to use CDT effectively to migrate the Visual Studio projects to the Eclipse CDT Workbench.
VS to Eclipse
In this section, we migrate a simple HelloWorld Win32 application developed using Microsoft Visual C++ 6.
- If you do not have an existing Win32 application, create a HelloWorld Win32 application using Visual Studio. Create a makefile from the Projects > Export Makefile menu in Visual Studio.
Figure 1. Create a HelloWorld Win32 application using Visual Studio
- Start Eclipse and open the C/C++ perspective.
- Select Window > Open Perspective > Other
Figure 2a. Open the C/C++ perspective
- Select C/C++ perspective
Figure 2b. Select C/C++ perspective
Note: C/C++ perspective will be listed in the Select Perspective dialog only if the CDT plug-in is installed. - Select Window > Open Perspective > Other
- Create a standard C/C++ Make project via File > New > Standard Make C++ Project. Name the project HelloEclipse in the Project Name edit box in the New Project dialog that appears and click Finish.
Figure 3a. It's HelloWorld time
Figure 3b. Name your project HelloEclipse
- Now we have to import the files created by Visual Studio into Eclipse. Go to File > Import. Select Filesystem in the Import dialog and click Next. If the newly created project is not visible, go to Window > ShowView > C/C++ project.
Figure 4. Import the HelloWorld Visual Studio project files
- Browse to the directory where the Visual Studio project was created, select the *.c*, *.h*,*.rc,*.ico and the .mak file to be imported and click Finish.
Figure 5. Import the *.c*, *.h*,*.rc,*.ico and .mak files
- Addition of environment variables
Figure 6. Add environment variables
- Right-click on the project and select Properties. In the Properties for HelloEclipse dialog, select C/C++ Make Project on the left side, and add INCLUDE and LIB environment variables to point to Visual Studio's include and lib paths. If you are using the platform SDK, point to its include and lib directories.
Figure 7. Point the Visual Studio include and lib paths in the right direction
- Open the .mak file imported from the Visual Studio project and make the following changes:
- Change the configuration (CFG) from Debug to Release so we get a release version of the application. It would look like
CFG=HelloWin - Win32 Release
.Figure 8a. Change the configuration from Debug to Release
- Add the command for executing the application in the ALL: tag:
@cmd /c $(OUTDIR)$(EXENAME).exe
- Define
EXENAME=HelloWin
Figure 8b. Define EXENAME
- Change the configuration (CFG) from Debug to Release so we get a release version of the application. It would look like
- The next step is to create a new make target.
- Right-click on our current project and click on Create Make Target.
Figure 9a. Create a new make target
- Create a make target for our project. 'Target name' specifies any name you wish. 'Make target' specifies the .mak file we just modified in the previous step. 'Build command' is specified by unchecking the Use default checkbox in the build command so the build command is
nmake
. To make the Make targets view visible, go to Window > Show View > Make Targets.
Figure 9b. The make target should be HelloWin.mak
- Right-click on our current project and click on Create Make Target.
- Double-click on the HelloEclipse target we created to build and run the Win32 application.
Figure 10. Run the application
With VC++ 2005 Express, making many changes in the traditional Visual Studio C/C++, there is a project initiated at Eclipse to solve this issue. Visit Eclipse MSVC for details (see Related topics).
Challenges to Eclipse CDT as an IDE for Windows development
Let's look at the various challenges posed by Eclipse CDT in various phases of the software development cycle.
Design
UML has become the de-facto standard for representing and designing software applications using object-oriented languages. IBM offers two Eclipse-based Java development tools: Rational® Application Developer and Rational Rose® XDE Developer for Java technology. Both offer model-driven development with UML support, Java code round-trip engineering, automatic or on-demand model-code synchronization, and other helpful features. Though we have a UML plug-in for Eclipse, there is no tight integration between UML to C/C++ in Eclipse platform today.
Development
Visual Studio's most interesting feature is its resource editor. Whether it's the SDK or MFC resource editor, both are used extensively to develop the static UI controls for the application. Today, Eclipse CDT offers no support to develop a UI. Consider using the Eclipse visual editor project when generating .rc files for Windows development.
Microsoft changes its compilers between versions, which makes it a challenge for Eclipse CDT to support the various versions of SDK tools Microsoft releases.
It is not viable today for you to migrate MSVC code totally to open source. In Linux, such an undertaking is complex. The SDK tools of Microsoft cannot run on open source, such as Linux. The SDK tools have to be run within the Windows emulator to make the SDK tools run on Linux.
However currently in Windows platform, developers with the help of platform SDK, Eclipse CDT and Debugging Tools for Windows can see Eclipse platform as an alternative for MS Visual Studio.
Visual Studio C++ Tutorial
Debugging
Eclipse CDT relies on GNU Debugger, GDB. GDB is a source-level debugger for C, C++, Ada, and other languages. Neither Eclipse nor GDB understand the debugging information generated by Microsoft compilers. As a result, it is a challenge to select CDT as a full-time development environment for Windows development. However, you can use Debugging Tools for Windows for debugging side by side with Eclipse as a development environment.
Testing
CppUnit is the testing framework for C/C++ similar to JUnit for Java. The CppUnit plug-in for Eclipse is also available and can be used side by side with CDT for testing the CPP code. See Related topics for a CppUnit tutorial.
Tools and other criteria
Apart from normal SDK development on Windows, Visual Studio supports many other developments, including DDK, .NET, WMI, Web development, IE component development, MDAC, and more. More contributions are expected in this space from Eclipse plug-in development community.
All open source tools, such as listdlls, process explorer, and task handler, can be considered to provide support inside Eclipse CDT, which can bridge this gap to a certain extent.
Conclusion
The growing popularity, versatility, and open source nature of Eclipse motivates many to embrace Eclipse as the development platform of the future. Nevertheless, porting Windows applications to use open source development tools like GCC, GDB, or GCC/GDB for Windows providing functionalities similar to Windows SDK is a nontrivial task today.
However, Eclipse is an effective open source alternative to visual studio when we consider the entire SDLC phases. With more and more plug-ins supported in Eclipse for C/C++ application development in Windows, Eclipse CDT may become a default IDE for C/C++ development for Windows.
Downloadable resources
Related topics
- Read Brian Lee's Eclipse Project CDT (C/C++) Plugin Tutorial if you are new to CDT.
- See the CppUnit wiki to learn about the C++ unit testing framework for Eclipse.
- See the CDT wiki at Eclipse.org for a good starting page on all things CDT.
- Eclipse C/C++ Development Tooling contains links to CDT documentation and downloads.
- See CDT/designs/msvc at Eclipse.org to learn more about CDT and Microsoft Visual Studio.
- Download the Eclipse Platform and get started with Eclipse now.
- For an excellent introduction to the Eclipse platform, see 'Getting started with the Eclipse Platform.
- See the latest Eclipse technology downloads at IBM alphaWorks.
- Visit IBM developerWorks' Eclipse project resources to learn more about Eclipse.
- Innovate your next open source development project with IBM trial software, available for download or on DVD.