Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

From scratch, made a mfc library in Visual Studio 2015

0.00/5 (No votes)
22 Aug 2015 1  
Create a dll wich uses MFC statically linked and allow you to call normal methods, static methods and global variables, quick an easy

Introduction

Create a MFC dll linked statically from scratch and uses normal and static methods or variables, quick and easy. It is useful is you are tired of triyng code of internet to get the dll working, no .def or dll hell, no afGetThread == null, no link errors

Background

It uses an MFC application and a MFC dll so you should know how MFC or C++ works

Using the code

The first step is create a project, I will start with the dll client because it will make easier the configuration of the project (basically not copying the .lib and .dll to the .exe folder by hand or by post builds commands)

Creating the project

Open Visual Studio -> New Project -> Visual C++ -> MFC Application. I choose the name TestDll for the exe project

Creating the .exe proyect

The wizzard will appear, click on next to get this window

Settings for the .exe proyect

I opted for and SDI project and MFC Standard style to make it simplier with less classes and easier. The use of MFC is static because is what i want, the rest of the wizzard doesn´t matter, you dont need to adjust the database connection, put an extension for the app or change the names of the files but feel free to do if you want or need it

Creating the Dll

Now we are going to create the dll project, do rigth click on the solution -> Add -> new project -> Visual C++ -> MFC DLL with the name Dll

Creating the mfc statically dll proyect

Another wizzard will come up, this is very simple, choose the regular dll with mfc statically linked

Creating the regular dll with mfc statically linked

Build the solution and verify that in the folder who has the .exe are the .lib and .dll, it sholud appear if you create first the .exe project, otherwise you may copy by hand or add a post build command to copy for you

The files that we need are in the same folder

Linking

Continue linking both projects, for that you need to tell the compiler that the .exe (TestDll) needs the dll so choose the TestDll project and rigth click -> Build dependencies -> Project dependencies

Accesing the settings to the dependencies

Check the dll project

Dependencies proyect window

Also we need to make clear were are the dll headers, inside property window of TestDll project -> C/C++ -> General ->Additional include directories and choose the folder where the headers of the dll project are

Setting the headers folder

Next go to Link -> Input -> Additional dependencies and add the path and name (include the name and etension for the .lib file) from the .lib we generate before

Adding the library to the proyect

Last step is to define a variable, COMPILE_MYLIBRARY, that you will use in a minute, you can do this in code

#define COMPILE_MYLIBRARY

Or you can go to C/C++ -> Preprocessor -> Preprocessor definitions and add the variable

Adding a define macro variable

Time for coding

The code of the dll.h it is very simple

#pragma once
#include "stdafx.h"
#include "resource.h"
#include <string>

#ifdef COMPILE_MYLIBRARY
#define MYLIBRARY_EXPORT __declspec(dllexport)
#else
#define MYLIBRARY_EXPORT__declspec(dllimport)
#endif

class MYLIBRARY_EXPORT CDLLApp : public CWinApp {
public:

CDLLApp();

int plusFive(int a);
static int minusThree(int b);
std::string str;

virtual BOOL InitInstance();
DECLARE_MESSAGE_MAP()
};

extern MYLIBRARY_EXPORT CDLLApp theDllApp;

The code has a static method (minusThree),  and Object method (plusFive) and a class variable string

MYLIBRARY_EXPORT makes possible to export and import the entire class.  TheDllApp is the instance of CWinApp that i renamed to avoid confusion from the other instance of CWinApp of the TestDll project and add the same macro and the extern word to make it accesible from outside

Header code

The implementation is very simple

Build the dll project and you are ready to use it on others projects

In my case it is inside the cpp file TestDllView, i put it in the constructor, remenber to include the header dll.h and done

#include "DLL.h"

CTestDllView::CTestDllView(){

int test1 = theDllApp.plusFive(5); //Should be 10
int test2 = CDLLApp::minusThree(10); //Should be 7
std::string test3{};
theDllApp.str = "Hello world";
test3 = theDllApp.str; //Should be Hello world

}

The source code is here

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here