刘收获

导航

< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

统计

延迟载入Dll(动态载入Dll)

  windows核心编程(第五版)20.3节的延迟载入Dll

  

  延迟载入Dll技术出现的原因:

  因为DLL的加载是比较浪费时间的,特别是大型软件加载,因此,这项技术是在应对软件初始化过程中避免浪费太多的时间。

  [1]因为部分DLL是在软件运行过程中才加载的。因此,DLL的加载延迟在了进程的运行过程中,节省程序的初始化时间

  [2]解决软件兼容性问题,比如一个新软件在老系统中运行,需要调用老版本的函数,我们只需要先使用GetVerisonEx()获得系统版本,然后再调用相应的函数,这样就解决了软件兼容性的问题,否则,新版在旧系统中就会报错。

 

  源代码:

  Resource.def

1
2
3
LIBRARY
EXPORTS
    sub_1 @1

  

  Dll.cpp

  

1
2
3
4
5
6
7
8
9
10
11
12
// Dll.cpp : 定义 DLL 应用程序的导出函数。
//
 
#include "stdafx.h"
#include <stdio.h>
 
#define dll extern "C" __declspec(dllexport)
#include "dll.h"
dll void __stdcall sub_1()
{
    printf("Hello SH!\n");
}

  Dll.h

1
2
3
4
5
6
7
8
#pragma once
#ifndef dll
 
#define dll extern "C" __declspec(dllimport)
 
#endif
 
dll void __stdcall sub_1();

  延迟载入Dll.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// 延迟载入Dll.cpp : 定义控制台应用程序的入口点。
//
 
#include "stdafx.h"
#include <Windows.h>
#include <delayimp.h>
#pragma comment(lib, "Delayimp.lib")
#include "../Dll/Dll.h"
#pragma comment(lib, "Dll")//dll.lib 要放到cpp文件下
int main()
{
    if (!GetModuleHandle(L"Dll.dll"))
        printf("dll.dll Not Load\r\n");
 
    sub_1();
    __HrLoadAllImportsForDll("Dll.dll");
    if (GetModuleHandle(L"Dll.dll"))
        printf("Dll.dll Load\r\n");
 
    __FUnloadDelayLoadedDLL2("Dll.dll");
    if (!GetModuleHandle(L"Dll.dll"))
        printf("Dll.dll UnLoad\r\n");
    return 0;
}

  相关设置:

  控制台应用程序相关设置:

  

  

 

posted on   沉疴  阅读(814)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗
点击右上角即可分享
微信分享提示