动态链接库的学习经验

关于动态链接库的学习经验

//3个动态链接库

//user32.dll
//gdi32.dll

——————作用

//共享资源
//本地化资源

//感谢小金鱼老师

加载方式:

//申明可导方式

C++标准

#ifdef API_EXPORT
//导出 采用宏定义容易
#define DLL_EXPORT  __declspec(dllexport)
#else
//导入
#define DLL_EXPORT  __declspec(dllimport)
#endif

C标准
#ifdef API_EXPORT
//导出 采用宏定义容易
#define DLL_EXPORT  extern "C" __declspec(dllexport)
#else
//导入
#define DLL_EXPORT  extern "C" __declspec(dllimport)
#endif
//包含链接库方式
#pragma comment(lib,"FilePath.lib")

/——————————-隐式————————————/

#include "related head files"
#pragma comment(lib,"FilePath.lib")

/——————————-显式————————————/

//获得句柄
HINSTANCE hInstance = LoadLibrary(_T("DllText.dll"));

//获得链接库中指定函数
GetProcAddress(hInstance, "Add");

//消除句柄
FreeLibrary(hInstance);

//————————————————————————-
//——–实例———–显示———-

//链接库
//.h

#pragma once
#ifdef API_EXPORT
#define DLL_EXPORT extern "C" __declspec(dllexport)
#else
#define DLL_EXPORT extern "C" __declspec(dllimport)
#endif


DLL_EXPORT int Add(int a, int b);

//.cpp

#define API_EXPORT
#include "math.h"

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

//链接库函数


#include <iostream>
BOOL APIENTRY DllMain
(
    HANDLE hModule,
    DWORD ul_reason_for_call,
    LPVOID lpReserve
)
{ 
    return TRUE;
}

//应用程序

//.cpp

#include <iostream>
#include <Windows.h>
#include <tchar.h>
#include <assert.h>

using namespace std;

//函数指针
typedef int(*Fun)(int, int);

int main()
{
    HINSTANCE hInstance = LoadLibrary(_T("DllText.dll"));
    assert(hInstance);
    Fun Add = (Fun)::GetProcAddress(hInstance, "Add");
    if (Add)
    {
        cout << Add(1, 2);
    }
    getchar();
    FreeLibrary(hInstance);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值