Generate DLL using Visual Studio

Preface

Previously, we needed to use CMD to call external tools to send HID commands from the PC to the microcontroller. I want to try integrating this into a DLL and use the call DLL method to achieve it. This would simplify the operation and achieve the goal more quickly. Today, I gathered information from the internet and completed the process of generating DLLs using Visual Studio.

Create a DLL project:

Select to build a DLL project and add a .h file.

The official documentation provides two methods to export functions from the DLL:

  • Keyword __declspec(dllexport): Simple to use but less versatile. This method is the default when creating a DLL project in Visual Studio.
  • Module definition file (.def): More versatile (can be called by other languages like Java, C#), but more complex to operate.

First, create a new header file ‘XXX.h’, which is used to declare the functions that need to be exported.

In the new header file, enter the following code.

#ifdef MYDLL_EXPORTS
#define MYDLL_API __declspec(dllexport)
#else
#define MYDLL_API __declspec(dllimport)
#endif 
 
//匯出類
class MYDLL_API Rectangle
{
public:
    double getarea(double w, double h);
    void   print();
 
};
 
//匯出函式
extern"C" MYDLL_API int __stdcall mysum(int a, int b);

Next, add a ‘C++ file (.cpp)’.

Then we need to implement the functions declared in ‘XXXX.h’ in ‘XXXX.cpp’. The code is as follows:

#include "pch.h"
#include "CreateDll.h"
#include<iostream>
double Rectangle::getarea(double w, double h)
{
    return w * h;
}
void Rectangle::print()
{
    std::cout << "已被列印";
}
int __stdcall mysum(int a, int b)
{
    return a + b;
}
  • __declspec(dllexport) – This modifier tells the compiler and linker that the function or variable it modifies needs to be exported from the DLL for use by other applications.

Conversely, there is another line of code, __declspec(dllimport), which tells the compiler and linker that the function or variable it modifies needs to be imported from the DLL.

  • The purpose of extern "C" is to tell the compiler to compile the code it modifies in the C language manner.

This is because C does not have function overloading and does not change function names, whereas C++ does have overloading and modifies function names during compilation based on return values and arguments.

  • __stdcall defines the calling convention for the exported function entry point as _stdcall.

How to call a DLL

  • Implicit linking requires three files: .h file, .lib file, and .dll file.
  • For the .h file: Go to the properties page -> C/C++ -> Additional Include Directories, add the path and reference it. (Or directly reference the absolute path)
  • For the .lib file (there are two ways to add it):
    • Properties page -> Linker -> General -> Additional Library Directories (add the .lib file path); Properties page -> Linker -> Input -> Additional Dependencies (add the .lib file name).
    • Directly use #pragma comment(lib, "MyDll.lib") (the .lib file needs to be placed in the same directory as the .exe file).”

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart