PEXEINFO LoadExeInfo()

in tools/ExeView/exehdr.cpp [51:155]


PEXEINFO LoadExeInfo (LPSTR lpFile)
{
    OFSTRUCT of;
    int      fFile=0, nLen, nErr=0;
    WORD     wSize;
    PEXEINFO pExeInfo;

#define ERROREXIT(X)    {nErr=X; goto error_out;}

// Allocate place main EXEINFO structure    
    pExeInfo = (PEXEINFO)LocalAlloc(LPTR, sizeof(EXEINFO));
    if (!pExeInfo)
        return (PEXEINFO)LERR_MEMALLOC;

// Open file and check for errors
    fFile = OpenFile( lpFile, &of, OF_READ );

    if (!fFile)
        ERROREXIT(LERR_OPENINGFILE);

// Allocate space for the filename
    pExeInfo->pFilename = (PSTR)LocalAlloc(LPTR, lstrlen(lpFile)+1 );
    if (!pExeInfo->pFilename)
        return (PEXEINFO)LERR_MEMALLOC;
        
    lstrcpy( pExeInfo->pFilename, lpFile );

// Read the OLD exe header
    nLen = (int)_lread(fFile, (LPSTR)&(pExeInfo->OldHdr), sizeof(OLDEXE));    

    if (nLen<sizeof(OLDEXE))
        ERROREXIT(LERR_READINGFILE);

    if (pExeInfo->OldHdr.wFileSignature != OLDSIG)
        ERROREXIT(LERR_NOTEXEFILE);

    if (pExeInfo->OldHdr.wFirstRelocationItem < 0x40)  // Old EXE
    {
        pExeInfo->NewHdr.wNewSignature = 0;
        _lclose( fFile );
        return pExeInfo;
    }

    _llseek( fFile, pExeInfo->OldHdr.lNewExeOffset, 0 );

// Read the NEW exe header
    nLen = (int)_lread(fFile, (LPSTR)&(pExeInfo->NewHdr), sizeof(NEWEXE));    

    if (nLen<sizeof(NEWEXE))
        ERROREXIT(LERR_READINGFILE);

    if (pExeInfo->NewHdr.wNewSignature != NEWSIG)
        ERROREXIT(LERR_NOTEXEFILE);

// Read entry table
    wSize = pExeInfo->NewHdr.wEntrySize;

    pExeInfo->pEntryTable=(PSTR)LocalAlloc(LPTR, wSize);

    if (!pExeInfo->pEntryTable)
        ERROREXIT(LERR_MEMALLOC);

    _llseek(fFile, pExeInfo->OldHdr.lNewExeOffset +
                   pExeInfo->NewHdr.wEntryOffset, 0 );

    nLen = _lread(fFile, (LPSTR)pExeInfo->pEntryTable, wSize );
    
    if (nLen != (int)wSize)
        ERROREXIT(LERR_READINGFILE);

// Read all the other tables
    if ( (nErr=ReadSegmentTable( fFile, pExeInfo )) < 0 )
        ERROREXIT(nErr);

// Do not read resources for OS/2 apps!!!!!!!
    if (pExeInfo->NewHdr.bExeType == 0x02)
    {
        if ( (nErr=ReadResourceTable( fFile, pExeInfo )) < 0 )
            ERROREXIT(nErr);
    }

    if ( (nErr=ReadResidentNameTable( fFile, pExeInfo )) < 0 )
        ERROREXIT(nErr);
    
    if ( (nErr=ReadImportedNameTable( fFile, pExeInfo )) < 0 )
        ERROREXIT(nErr);

    if ( (nErr=ReadNonResidentNameTable( fFile, pExeInfo )) < 0 )
        ERROREXIT(nErr);

    nErr = 1;

error_out:
// Close file and get outta here
    if (fFile)
        _lclose( fFile );

    if (nErr<=0)
    {
        FreeExeInfoMemory( pExeInfo );
        return (PEXEINFO)nErr;
    }
    return pExeInfo;

} //*** LoadExeInfo