How to write a DLL file

How to write a DLL file

To read this article, please first learn C++ and the use of C language. This is the basis.

what is dll file

DLL file: Chinese translation is dynamic link library. Windows system programs provide a lot of executable files that can be called. Just like when we develop software, we never write a graphical interface for a program ourselves. This is because The Windows system has provided it for us, and we only need to call it to use it. For example, we often see kernel32.dll (including functions related to managing memory, processes and threads) and User32.dll (mostly user interface functions).

The main reason is that if dll files are provided step by step, many applications and developers will have to write the underlying code themselves, which will bring great difficulties to software developers.

How to create dll file

I use VS for construction. I prefer to use VS for windows code development. After all, it is a compiler developed by Microsoft itself.

Create a new project and select New dll project

Insert image description here

Even if a dll file is created in this way, we don't need to worry about what is inside. We only need to know that our code is written in dllmain.cpp. The method of dll generation is also different from ordinary C++ files. , we just right-click on the DLL project and click Generate.

Insert image description here

We can find that a dll file is generated

A brief introduction to dll file code

Our VS automatically generated some codes for us. Let’s briefly introduce what the following codes are used for.

// dllmain.cpp : 定义 DLL 应用程序的入口点。
#include "pch.h"

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

DllMain function

Static linking or dynamic linking (LoadLibrary call, etc.) will call the DllMain function. We can judge when to use this dllmain function through the following four situations.

  1. DLL_PROCESS_ATTACH: This is called when the dllmain file is mapped to the address space. That is, this section will only be run when it is called for the first time.
  2. DLL_PROCESS_DETACH: When a DLL is unmapped from the process's address space
  3. DLL_THREAD_ATTACH: This is when the process creates a thread
  4. DLL_THREAD_DETACH: This is called when the thread ends

A simple example

We plan to use a Dll file to download an html web page

This is our code in dllmain

// dllmain.cpp : 定义 DLL 应用程序的入口点。
#include "pch.h"
#include<windows.h>
#include "stdio.h"


BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    printf("dll load\n");
}

We can generate a dll file, and then we have to find a way to call this file

#include "windows.h"
#include "stdio.h"

int main(int argc, char* argv[])
{
    HMODULE  g_hDll = LoadLibrary("D:\\Code\\source\\repos\\DLLStudy\\Debug\\DLLStudy.dll");
    return 0;
}

Because I deleted the switch in this dll file, this printf function will be executed no matter which call is made, so we will find that this is called twice in total.

Insert image description here

ok this is a simple operation called by dll, I hope it can help you

Hope this blog can help you!

Guess you like

Origin blog.csdn.net/qq_52380836/article/details/128011260