Click here to Skip to main content
15,886,788 members
Articles / Programming Languages / C++
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (11 votes)
26 Mar 2021CPOL2 min read 12.5K   14   7
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)


Written By
Software Developer
United States United States
turns good thoughts into actions...


Comments and Discussions

 
GeneralMy vote of 5 Pin
kanalbrummer23-Sep-22 0:52
kanalbrummer23-Sep-22 0:52 
QuestionAn observation Pin
abdekker12319-Jul-22 5:01
abdekker12319-Jul-22 5:01 
AnswerRe: An observation Pin
Southmountain27-Jul-22 14:52
Southmountain27-Jul-22 14:52 
Questionnot an console application Pin
Member 1423574520-Apr-21 19:34
Member 1423574520-Apr-21 19:34 
AnswerRe: not an console application Pin
Southmountain8-May-21 8:58
Southmountain8-May-21 8:58 
QuestionMessage Closed Pin
27-Mar-21 12:46
Member 1512284527-Mar-21 12:46 
SuggestionAnother Step Pin
Rick York9-Mar-21 10:22
mveRick York9-Mar-21 10:22 
GeneralRe: Another Step Pin
Southmountain9-Mar-21 11:03
Southmountain9-Mar-21 11:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.