Click here to Skip to main content
15,886,422 members
Articles / Desktop Programming / MFC
Tip/Trick

From scratch, made a mfc library in Visual Studio 2015

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
22 Aug 2015CPOL3 min read 18.8K   29   7   2
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

C++
#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

C++
#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

C++
#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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Spain Spain
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionhave you consider to post this as a tip? Pin
Nelek23-Aug-15 12:02
protectorNelek23-Aug-15 12:02 
I think it would fit better that cathegory. For more information about the types please read:
Code Project Article FAQ - Article[^]
Code Project Article FAQ - Tip[^]

If you need information about how to change your submission to a tip, please read:
Code Project Article FAQ - change to tip[^]
M.D.V. Wink | ;)

If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.

AnswerFormat issues.. Pin
Afzaal Ahmad Zeeshan22-Aug-15 14:48
professionalAfzaal Ahmad Zeeshan22-Aug-15 14:48 

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.