Listing 2: DPdemo.cpp
A demonstration of DLLPack
/* DPDEMO.CPP - Illustrates the use of DLLpack */ #include <windows.h> #include <stdio.h> #include <stdlib.h> #include <conio.h> #include "dllpack.h" #include "cpuinf.h" DWORD dpExceptionFilter(struct _EXCEPTION_POINTERS *ep) { DWORD dwResult = EXCEPTION_CONTINUE_SEARCH; PDelayLoadInfo pdli = (PDelayLoadInfo) ep->ExceptionRecord->ExceptionInformation[0]; /* If there is any chance that you can fix the situation, store * the address of the imported function in pdli->pfnCur and * return EXCEPTION_CONTINUE_EXECUTION. */ switch (ep->ExceptionRecord->ExceptionCode) { case VcppException(ERROR_SEVERITY_ERROR, ERROR_MOD_NOT_FOUND): printf("Unable to load %s\n", pdli->szDll); dwResult = EXCEPTION_EXECUTE_HANDLER; break; case VcppException(ERROR_SEVERITY_ERROR, ERROR_PROC_NOT_FOUND): char OrdStr[20]; const char *FuncName; if (pdli->dlp.fImportByName) { FuncName = pdli->dlp.szProcName; } else { wsprintf(OrdStr, "ord:%u", pdli->dlp.dwOrdinal); FuncName = OrdStr; } /* if */ printf("Unable to import %s\n", FuncName); dwResult = EXCEPTION_EXECUTE_HANDLER; break; } /* switch */ return dwResult; } #define ELEMENTS(array) (sizeof(array) / sizeof(array[0])) int main(void) { char *CPUnames[] = { "8086/88", "80286", "80386", "80486", "Pentium", "PentiumPro", "PentiumII", "PentiumIII", "Pentium4", "unknown" }; unsigned int result; __pfnDliNotifyHook = dpDelayLoadHook; /* Use SEH, do not set the failure hook * * __pfnDliFailureHook = dpDelayLoadHook; * */ __try { result = wincpuid(); printf("CPU = %d\n", result); result = min(result, ELEMENTS(CPUnames)-1); printf("CPU = %s\n", CPUnames[result]); /* At any time, you can unload a specific delay-loaded DLL * * dpUnloadDLL("CPUINF32.DLL"); * * Or you can unload all delay-loaded DLLs in one call * * dpUnloadDLL(NULL); * */ } __except(dpExceptionFilter(GetExceptionInformation())) { printf("Caught the exception\n"); } /* try */ printf("Done\n"); return 0; }