Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++

A Tip to Get MFC Support for Console Application in Visual Studio 2019

5.00/5 (11 votes)
26 Mar 2021CPOL2 min read 14.2K  
Some quick configurations to get MFC support for console application
In this tip, you will learn about quick project configuration to add MFC support to console application in Visual Studio 2017+.

Introduction

Let's say we have a console program to test some C++ short logic and do some prototype. how about we want to test some MFC functions? What is the quick way to get MFC support in current console application?

This tip will help you to do so.

I am inspired by [1], but my perspective and goal are different than [1].

The Project Configuration

Here are some quick steps:

  1. Open your console project in Visual Studio 2019.
  2. In solution explorer, right click and select the project.
  3. Configuration Properties -> General -> Use of MFC, select Use MFC in a Shared DLL option.
  4. (optional) Configuration properties ->Advanced -> Character set: select "use unicode character set".
  5. Configuration Properties -> linker - > System - > Subsystem: set Windows(SYSTEM:WINDOWS)
  6. Configuration Properties -> linker - > Command Line- > Additional Options: type these parameters: /SUBSYSTEM:windows /ENTRY:mainCRTStartup

stdafx.h

This is optional. You use it only if it is needed, but I collected it based on my experience:

C++
#pragma once

#define _WIN32_WINNT 0x0A00
#define WINVER 0x0A00 
#define VC_EXTRALEAN		               // Exclude rarely-used stuff from Windows headers
#define _CRT_SECURE_NO_WARNINGS
#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS // some CString constructors will be explicit
#define _CRT_NON_CONFORMING_SWPRINTFS      //use traditional swprintf()


#include <afxwin.h>         // MFC core and standard components
#include <afxext.h>         // MFC extensions
#include <afxdisp.h>        // MFC Automation classes
#include <afxdtctl.h>	    // MFC support for Internet Explorer 4 Common Controls
#include <afxcmn.h>         // MFC support for Windows Common Controls

The macros value 0x0A00 is to target every version of the Windows 10 operating system.

All values are listed in the Windows header file <sdkddkver.h > that defines macros for each major Windows version. To save time,we can include <sdkddkver.h > directly as Rick York pointed in the comments.

in order to let our application to support a previous Windows platform, firstly we include WinSDKVer.h. secondly, we need to set the WINVER and _WIN32_WINNT macros to the oldest supported platform . thirdly, we include <sdkddkver.h >.

 

A Shortest MFC Program

Here is a short demo program I created from the "Hello World" console application. Following the above steps, you can see this message box pop up.

In Project configuration-> Advanced -> Character set: select "use unicode character set".

C++
// ConsoleMFC03.cpp: This file contains the 'main' function. 
// Program execution begins and ends there.
//
#include <afxwin.h>

int main()
{
    ::MessageBox(NULL, _T("Hello World"), _T("MFC Programming"), MB_OK);

    return 0;
}

Here is the screenshot for this console:

You can use this console application to test MFC functions.

Points of Interest

You can also use this console application to test OpenGL functions.

Any feedback and comments are welcome. Hope this tip can help beginners to save some time.

Here I want to thank Rick York's comments!

Reference

History

  • 9th March, 2021: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)