Home DLL Implicit Linking(English)
Post
Cancel

DLL Implicit Linking(English)

We need .lib .dll and header

Setting Up a DLL Project:

Creating a DLL project can indeed be useful, but the setup process can be tedious and error-prone.

The following example demonstrates

  1. export dll function
  2. export dll struct
  3. export dll class
  4. export dll template function

Let’s get started

Creating a DLL Project:

Desktop View

Desktop View

Open project properties and ensure that the configuration type is set to “Dynamic Library (.dll)”.

Desktop View

Set the output directory to a separate folder. This ensures that the .lib file is not bundled with the executable.

Desktop View

Output as following

Desktop View

Why?

Although the .lib file itself does not contain the actual function implementations, it does contain the names of the functions and their corresponding addresses in the DLL. Therefore, if someone gains access to the .lib file, they can view the names and addresses of the exported functions in the DLL, potentially reverse-engineering the DLL’s implementation.

However, DLLs need to be placed together with the executable to execute properly. Therefore, copying only the DLL file using post-build steps is sufficient.

Desktop View

Definition in .h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#pragma once
// export function
extern "C" __declspec(dllexport) int AddFunction(int a, int b);

struct __declspec(dllexport) MyStruct {
    int number1;
    int number2;
};

class __declspec(dllexport) MyClass {
public:

    MyClass();

    int MultiplyBy10(int a);
};

template <typename T>
__declspec(dllexport) T AddTemplate(T a, T b)
{
    return a + b;
};

Implementation in .cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "pch.h"
#include "MyDLL.h"

int AddFunction(int a, int b)
{
    return a+b;
}

MyClass::MyClass()
{
}

int MyClass::MultiplyBy10(int a)
{
    return a*10;
}

Alright, the DLL is set up successfully. Now, how do we use it?

Creating a Project Using the DLL:

Let’s create a console project to demonstrate.

Desktop View

Specify the directory where this project should find the lib file (it’s recommended to use a relative path).

Desktop View

Specify the name of the lib file that the linker should use.

Desktop View

Folder Structure

Desktop View

How it Looks in Visual Studio

Desktop View

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include "../Dll1/MyDLL.h"
int main()
{
    std::cout << "DLL demo : \n";

    std::cout << "-----------------" << std::endl << std::endl;

    std::cout << "Call AddFunction in DLL : " << AddFunction(5, 5) << std::endl<< std::endl;

    MyStruct myStruct{1,5};
    std::cout << "Use MyStruct in DLL : " << myStruct.number1 << std::endl;
    std::cout << "Use MyStruct in DLL : " << myStruct.number2 << std::endl << std::endl;

    auto myClass = new MyClass();

    std::cout << "Use MyClass in DLL : " << myClass->MultiplyBy10(10) << std::endl << std::endl;

    std::cout << "Call AddTemplate in DLL : " << AddTemplate(1.1, 2.3) << std::endl << std::endl;

    std::cout << "Call AddTemplate in DLL : " << AddTemplate(0.1f, 0.3f) << std::endl << std::endl;

    system("pause");
}

Output Folder

Desktop View

Result

Desktop View

The process is quite tedious, indeed. Phew! Thanks for sticking with it till the end!

☝ツ☝

This post is licensed under CC BY 4.0 by the author.

👈 ツ 👍