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
The wizzard will appear, click on next to get this window
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
Another wizzard will come up, this is very simple, choose 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
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
Check the dll project
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
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
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
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
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); int test2 = CDLLApp::minusThree(10); std::string test3{};
theDllApp.str = "Hello world";
test3 = theDllApp.str;
}
The source code is here