FREE Subscription to Dr. Dobb’s Digest: Same Great Content, New Digital Edition
Site Archive (Complete)
Dobbs M-Dev
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
June 01, 2002

Packing DLLs in your EXE

(Page 3 of 4)
June 2002/ Packing DLLs in your EXE

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;
}
Previous Page | 1 | 2 | 3 | 4 Next Page
RELATED ARTICLES
No Related Articles
TOP 5 ARTICLES
No Top Articles.



MICROSITES
FEATURED TOPIC

ADDITIONAL TOPICS

INFO-LINK