【Win】加载动态库 (loadlibrary)

概述:通过 using 给导出函数起别名

[toc]

加载

using的用法参考 使用using起别名

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
26
27
28
29
30
31
#include <iostream>
#include <windows.h>
#include "PipeIPC/PipeIPC.h"

int main()
{
std::cout << "--- DLL Test ---\n";

HINSTANCE hDLL; // Handle to DLL
using Face = int * (*)(int,int);

hDLL = LoadLibrary(L"mydll.dll");
if (hDLL != NULL)
{
Face faceSum = (Face)GetProcAddress(hDLL,
"mySum");

if (!faceSum)
{
// handle the error
FreeLibrary(hDLL);
return 0;
}
else
{
// call the function
faceSum(1,1);
}
}
return 0;
}

生成动态库

dllmain.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifdef __cplusplus
extern "C"
{
#endif

#define MYIMAPI extern "C" __declspec(dllimport)

#define MYEXAPI extern "C" __declspec(dllexport)

#ifdef __cplusplus
}
#endif

MYEXAPI int Add(int numa, int numb)
{
return numa + numb;
}

MYEXAPI int Sub(int numa, int numb)
{
return (numa - numb);
}


【Win】加载动态库 (loadlibrary)
https://hodlyounger.github.io/A_OS/Windows/API/【winapi】API_加载动态库/
作者
mingming
发布于
2023年10月27日
许可协议