write dll module

Here are all display loading (put the generated dll under the project to be called together with the exe)
here are all empty projects created.
There are two types. The first type does not write the dllmain function, that is, only write some export function
dll .

//allen.h
extern "C" _declspec(dllexport) int Sum(int a, int b);//加法函数。
extern "C" _declspec(dllexport) int Max(int a, int b);//取较大值函数
extern "C" _declspec(dllexport) int Min(int a, int b);//取较小值函数
//allen.cpp
extern "C" _declspec(dllexport)int Sum(int a, int b)
{
    return a + b;
}
extern "C" _declspec(dllexport)int Max(int a, int b)
{
    if (a >= b)return a;
    else
        return b;
}
extern "C" _declspec(dllexport)int Min(int a, int b)
{
    if (a >= b)return b;
    else
        return a;
}

exe

//allen.cpp
#include <iostream>
#include<windows.h>
using namespace std;
void main(void)
{
    typedef int(*pMax)(int a, int b);//函数指针
    typedef int(*pMin)(int a, int b);
    pMax Max = NULL;
    pMin Min = NULL;
    HINSTANCE hDLL;
    hDLL = LoadLibrary("Win32Project6.dll");//加载动态链接库Win32Project6.dll文件;
    Max = (pMax)GetProcAddress(hDLL, "Max");
    Min = (pMin)GetProcAddress(hDLL, "Min");
    if (Max)//如果取出函数成功,则执行下面的语句
    {
        int A = Max(5, 8);
        cout << "比较的结果为" << A;
    }
    if (Min)
    {
        int B = Min(5, 8);
        cout << "比较的结果为" << B;
    }
    FreeLibrary(hDLL);//卸载Win32Project6.dll文件;
}


The second is to write dllmain and use more dlls when calling injection when the dll is loaded

//allen.cpp
#include "allen.h"
#include<Windows.h>
BOOL APIENTRY DllMain(HMODULE hModule,
    DWORD  ul_reason_for_call,
    LPVOID lpReserved
)
{
    MessageBox(NULL, L"haha", 0, 0);
}

exe

#include <iostream>
#include<windows.h>
using namespace std;
void main(void)
{
    HINSTANCE hDLL;
    hDLL = LoadLibrary("Win32Project6.dll");

    FreeLibrary(hDLL);
}

write dll module

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324878997&siteId=291194637