windows 核心编程DLL 高级技术-延迟载入dll,01
windows 核心编程DLL 高级技术-延迟载入dll,01
头文件
/************************************************************************
Module: ExportDelay.h
************************************************************************/
#pragma once
#ifdef DELAYLIB_EXPORT
//MYLIB_EXPORT必须在Dll源文件包含该头件前被定义
#define DELAYAPI extern "C" __declspec(dllexport)
//本例中所有的函数和变量都会被导出
#else
#define DELAYAPI extern "C" __declspec(dllimport)
#endif
//定义要导出的函数的原型
DELAYAPI int Func_A(int iVal);
DELAYAPI int Func_B(int iVal1, int iVal2);
DELAYAPI int Func_C(int iVal1, int iVal2, int iVal3);
DLL程序
/*------------------------------------------------------------------------
20_ExportDelay.cpp
演示延迟载入DLL---DLL程序
-----------------------------------------------------------------------*/
#include <windows.h>
#include <tchar.h>
#include <locale.h>
//在这个DLL源文件定义要导出的函数和变量
#define DELAYLIB_EXPORT //这个源文件中须定义这个宏,以告诉编译器函数要
//__declspect(dllexport),这个宏须在包含20_ImportDelay.cpp
//之前被定义
#include "ExportDelay.h"
BOOL APIENTRY DllMain(HMODULE hDllHandle, DWORD dwReason, LPVOID lpreserved)
{
static TCHAR pModuleName[MAX_PATH] = {};
switch (dwReason)
{
case DLL_PROCESS_ATTACH:
_tsetlocale(LC_ALL, _T("chs"));
GetModuleFileName(hDllHandle, pModuleName, MAX_PATH);
_tprintf(_T("进程[%u]调用线程[0x%X]加载DLL[0x%08X]:%s\n"),
GetCurrentProcessId(), GetCurrentThreadId(), (UINT)hDllHandle, pModuleName);
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
//GetModuleFileName(hDllHandle, pModuleName, MAX_PATH);
_tprintf(_T("进程[%u]调用线程[0x%X]卸载DLL[0x%08X]:%s\n"),
GetCurrentProcessId(), GetCurrentThreadId(), (UINT)hDllHandle, pModuleName);
break;
}
return TRUE;
}
int Func_A(int iVal)
{
return iVal;
}
int Func_B(int iVal1, int iVal2)
{
return iVal1 + iVal2;
}
int Func_C(int iVal1, int iVal2, int iVal3)
{
return iVal1 + iVal2 + iVal3;
}