Windows 核心编程之Dll 延时加载

这篇博客探讨了Windows核心编程中的DLL延迟加载技术。通过创建工程并生成DLL与LIB,作者展示了如何实现动态卸载DLL,并解释了添加LIB文件在编译链接阶段确保找到导出函数的重要性。同时,提到了Delay Loaded DLLs的概念,指出哪些DLL可以支持这种加载方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

DLL和Lib自己建立个工程,自己生成吧


代码是控制台的

#include <iostream>
#include <map>
#include <Windows.h>
#include <tchar.h>
#include <process.h>
#include <delayimp.h>
#include "../../dynamic_DLL/dynamic_DLL/dynamic_DLL.h"

//系统的lib
#pragma comment(lib,"delayimp.lib")

//自定义的lib
#pragma comment(lib,"dynamic_DLL.lib")

using namespace std;


FARPROC WINAPI delayHook(unsigned dliNotify, PDelayLoadInfo pdli)
{
	switch (dliNotify) {
		case dliStartProcessing :

			// If you want to return control to the helper, return 0.
			// Otherwise, return a pointer to a FARPROC helper function
			// that will be used instead, thereby bypassing the rest 
			// of the helper.

			break;

		case dliNotePreLoadLibrary :

			// If you want to return control to the helper, return 0.
			// Otherwise, return your own HMODULE to be used by the 
			// helper instead of having it call LoadLibrary itself.

			break;

		case dliNotePreGetProcAddress :

			// If you want to return control to the helper, return 0.
			// If you choose you may supply your own FARPROC function 
			// address and bypass the helper's call to GetProcAddress.

			break;

		case dliFailLoadLib : 

			// LoadLibrary failed.
			// If you don't want to handle this failure yourself, return 0.
			// In this case the helper will raise an exception 
			// (ERROR_MOD_NOT_FOUND) and exit.
			// If you want to handle the failure by loading an alternate 
			// DLL (for example), then return the HMODULE for 
			// the alternate DLL. The helper will continue execution with 
			// this alternate DLL and attempt to find the
			// requested entrypoint via GetProcAddress.

			break;

		case dliFailGetProc :

			// GetProcAddress failed.
			// If you don't want to handle this failure yourself, return 0.
			// In this case the helper will raise an exception 
			// (ERROR_PROC_NOT_FOUND) and exit.
			// If you choose you may handle the failure by returning 
			// an alternate FARPROC function address.


			break;

		case dliNoteEndProcessing : 

			// This notification is called after all processing is done. 
			// There is no opportunity for modifying the helper's behavior
			// at this point except by longjmp()/throw()/RaiseException. 
			// No return value is processed.

			break;

		default :

			return NULL;
	}

	return NULL;
}


PfnDliHook   __pfnDliNotifyHook2 = delayHook ;

PfnDliHook   __pfnDliFailureHook2 = delayHook ;

void IsDllExist(LPCSTR lpName)
{
	HMODULE hInt = GetModuleHandleA(lpName);
	if (NULL == hInt)
	{
		printf("no\n");
	}
	else
	{
		printf("yes\n");
	}
}
char chDllName[]="dynamic_DLL.dll";

int main()
{
	IsDllExist(chDllName);

	MyAdd(1,2);

	IsDllExist(chDllName);

	__FUnloadDelayLoadedDLL2(chDllName);

	IsDllExist(chDllName);

	MyAdd(2,4);

	IsDllExist(chDllName);

	getchar();

	return 0;
}


下图是工程属性


设置它,支持动态卸载DLL



添加lib是为了编译程序link时能找到导出的函数,   Delay Loaded DLLs 表示哪些Dll支持延迟加载



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值