导入库的一些代码。
//延迟加载
#include<windowsx.h>
#include<tchar.h>
#include<StrSafe.h>
#include<Delayimp.h>
#pragma comment(lib,"Delayimp.lib")
TCHAR g_szDelayLoadModuleName[] = TEXT("20-DelayLoadLib");
LONG WINAPI DelayLoadDllExceptionDilter(PEXCEPTION_POINTERS pep);
void IsModuleLoaded(PCTSTR pszModuleName)
{
HMODULE hmod = GetModuleHandle(pszModuleName);
char sz[100];
#ifdef UNICODE
StringCchPrintfA(sz, _countof(sz), "Module \"%S\" is %Sloaded.",
pszModuleName,(hmod == NULL)? L"not" : L"");
#else
StringCchPrintfA(sz, _countof(sz), "Module \"%s\" is %sloaded.",
pszModuleName, (hmod == NULL) ? L"not" : L"");
#endif // UNICODE
//chMB(sz);
}
int WINAPI _tWinMain(HINSTANCE hInstExe, HINSTANCE, PTSTR pszCmdLine, int)
{
//wrap all calls to delay-load DLL functions inside SEH
__try {
int x = 0;
//if you're in the debugger ,try the new debug .Modules menu item to
//see that the DLL is not loaded prior to executing the line below
IsModuleLoaded(g_szDelayLoadModuleName);
x = fnLib();//Attempt to call delay-load function//随便的导入函数1
//Use debug. Modules to see that the DLL is now loaded
IsModuleLoaded(g_szDelayLoadModuleName);
x = fnLib2();//Attempt to call delay-load function //随便的导入函数2
//Unload the delay-loaded DLL
//NOTE:name must exactly match /DelayLoad:(DllName)
__FUnloadDelayLoadedDLL2("20-DelayLoadLib.dll");
//Use debug. Modules to see that the DLL is now unloaded
IsModuleLoaded(g_szDelayLoadModuleName);
x = fnLib();//Attempt to call delay-load function//随便的导入函数1
//Use debug. Modules to see that the DLL is now loaded again
IsModuleLoaded(g_szDelayLoadModuleName);
}
__except (DelayLoadDllExceptionDilter(GetExceptionInformation()))
{
//nothing to do in here, thread continues to run normally
}
//More code can go here...
return 0;
}
LONG WINAPI DelayLoadDllExceptionDilter(PEXCEPTION_POINTERS pep)
{
//assume wo recognize this exception
LONG lDisposition = EXCEPTION_EXECUTE_HANDLER;
//If this is a Delay-load problem. ExceptionInformation[0]points
//to a DelayLoadInfo structure that has detailed error info
PDelayLoadInfo pdli = PDelayLoadInfo(pep->ExceptionRecord->ExceptionInformation[0]);
//Create a buffer where wo construct error messages
char sz[500] = { 0 };
switch (pep->ExceptionRecord->ExceptionCode)
{
case VcppException(ERROR_SEVERITY_ERROR,ERROR_MOD_NOT_FOUND):
//the Dll module was not found at runtime
StringCchPrintfA(sz, _countof(sz), "Dll not found: %s", pdli->szDll);
break;
case VcppException(ERROR_SEVERITY_ERROR, ERROR_PROC_NOT_FOUND):
if (pdli->dlp.fImportByName)
{
StringCchPrintfA(sz, _countof(sz), "Function %s was not found in %s",
pdli->dlp.szProcName, pdli->szDll);
}
else
{
StringCchPrintfA(sz, _countof(sz), "Function ordinal %d was not found in %s",
pdli->dlp.dwOrdinal,pdli->szDll);
}
break;
default:
lDisposition = EXCEPTION_CONTINUE_SEARCH;
break;
}
if (lDisposition == EXCEPTION_CONTINUE_SEARCH)
{
//chMB(sz);
}
return (lDisposition);
}
FARPROC WINAPI DliHook(unsigned dliNotify, PDelayLoadInfo pdli)
{
FARPROC fp = NULL;//default return value
//NOTE: the members of the DelayLoadInfo structure pointed
//to by pdli show the results of progress made so far
switch (dliNotify)
{
case dliStartProcessing:
//call when __delayloadhelper2 attempts to find a DLL/function
//return 0 to have normal behavior or nonzero ti override
//everything (you will still get dliNoteEndProcessing)
break;
case dliNotePreLoadLibrary:
//called if loadlibrary
//return NULL to have __delayLoadHelper2 call LoadLibrary
//or you can call LoadLibrary yourself and return the HMODULE
fp = (FARPROC)(HMODULE)NULL;
break;
case dliFailLoadLib:
//called if loadLibrary fails
//again ,you can call loadlibrary yourself here and return an HMODULE
//if you return NULL, __delayLoadHelper2 raises the
//ERROR_MOD_NOT_FOUND exception
fp = (FARPROC)(HMODULE)NULL;
break;
case dliNotePreGetProcAddress:
//called just befor GetProcAddress
//Return NULL to have __delayLoadHelper2 call GetProcAddress,
//or you can call GetProcAddress yourself and return the address
fp = (FARPROC)NULL;
break;
case dliFailGetProc:
//called if GetProcAddress fails
//you can call GetProcAddress yourself here and return an address
//If you return NULL ,__delayLoadHelper2 raises the
//ERROR_PROC_NOT_FOUND exception
fp = (FARPROC)NULL;
break;
case dliNoteEndProcessing:
//A simple notification that __delayLoadHelper2 is done
//you can examine the members of the DelayLoadInfo structure
//pointed to by pdli and raise an exception if you desire
break;
default:
break;
}
return (fp);
}
//Tell __delayLoadHelper2 to call my hook function
PfnDliHook __pfnDliNotifyHook2 = DliHook;
PfnDliHook __pfnDliFailureHook2 = DliHook;