home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 11 Util
/
11-Util.zip
/
WHATFN.ZIP
/
ENUMPROC.C
next >
Wrap
C/C++ Source or Header
|
1989-01-22
|
4KB
|
156 lines
/* enumproc.c */
/* enumerates all exported procs from DLL file */
/* Andrew Schulman, 32 Andrews St., Cambridge MA 02139, (617) 876-2102 */
/* 28-May-1988 */
/* revised 12-Oct-1988: looking at non-resident table too, now */
/* revised 15-Oct-88: checking to see if it's really a DLL */
/* revised 22-Jan-1989:
reset nres_state=LOOK_FOR_NONRES (Charles Petzold found bug)
rewrote using low-level IO instead of stream IO */
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#define LOOK_FOR_NONRES 0
#define SAW_NONRES 1
#define SAW_DESCRIPTION 2
/* lifted from MSoft newexe.h */
#define ENEWHDR 0x003CL /* offset of new EXE header */
#define EMAGIC 0x5A4D /* old EXE magic id: 'MZ' */
#define NEMAGIC 0x454E /* new EXE magic id: 'NE' */
/*
the following are considered to be dynamic link libraries:
OS/2 DLL (e.g., PMWIN.DLL)
Windows dynamic link library (e.g., USER.EXE)
font file (e.g., COURA.FON)
new device driver (e.g., POINTDD.SYS)
Windows device driver (e.g., EPSON.DRV)
*/
#define DLL_FLAG 0x8000
typedef struct
{
unsigned ne_magic;
char ne_ver;
char ne_rev;
unsigned ne_enttab;
unsigned ne_cbenttab;
long ne_crc;
unsigned ne_flags;
unsigned ne_autodata;
unsigned ne_heap;
unsigned ne_stack;
long ne_csip;
long ne_sssp;
unsigned ne_cseg;
unsigned ne_cmod;
unsigned ne_cbnrestab; /* size of nonres name table */
unsigned ne_segtab;
unsigned ne_rsrctab; /* offset of resource table */
unsigned ne_restab; /* offset of resident name table */
unsigned ne_modref;
unsigned ne_impname;
long ne_nonrestab; /* offset from begin of nres table */
} NEWEXE;
char *dll_description(const char *dll, char *buf)
{
NEWEXE hdr;
int f;
unsigned w;
unsigned char len;
*buf = '\0';
if ((f = open(dll, O_RDONLY | O_BINARY)) == -1)
goto closeit;
read(f, &w, 2);
if (w != EMAGIC)
goto closeit;
lseek(f, ENEWHDR, SEEK_SET);
read(f, &w, 2);
lseek(f, /*start_newexe*/ (long) w, SEEK_SET);
read(f, &hdr, sizeof(NEWEXE));
if (hdr.ne_magic != NEMAGIC)
goto closeit;
lseek(f, hdr.ne_nonrestab, SEEK_SET);
read(f, &len, 1);
if (len)
{
read(f, buf, len);
buf[len] = '\0';
}
else
buf[0] = '\0';
closeit:
if (f != -1) close(f);
return buf;
}
char *enumproc(const char *dll, char *buf, unsigned *ord)
{
static NEWEXE hdr;
static int f = -1;
static int nres_state = LOOK_FOR_NONRES;
unsigned w;
unsigned char len;
if (dll == (char *)-1)
goto closeit;
else if (dll)
{
unsigned long start_newexe;
if (f) close(f);
if ((f = open(dll, O_RDONLY | O_BINARY)) == -1)
goto closeit;
read(f, &w, 2);
if (w != EMAGIC)
goto closeit;
lseek(f, ENEWHDR, SEEK_SET);
read(f, &w, 2);
start_newexe = (long) w;
lseek(f, start_newexe, SEEK_SET);
read(f, &hdr, sizeof(NEWEXE));
if (hdr.ne_magic != NEMAGIC || !(hdr.ne_flags & DLL_FLAG))
goto closeit;
lseek(f, start_newexe, SEEK_SET);
lseek(f, (long) hdr.ne_restab, SEEK_CUR);
}
getone:
read(f, &len, 1);
if (len)
{
read(f, buf, len);
buf[len] = '\0';
read(f, ord, 2);
if (nres_state == SAW_NONRES)
{
/* skip past the module description string */
nres_state = SAW_DESCRIPTION;
goto getone;
}
return buf;
}
else if (nres_state == LOOK_FOR_NONRES)
{
lseek(f, hdr.ne_nonrestab, SEEK_SET); /* from beginning */
nres_state = SAW_NONRES;
goto getone;
}
else
{
closeit:
close(f);
f = -1;
nres_state = LOOK_FOR_NONRES; /* thanks, Charles P.! */
return NULL;
}
/*NOTREACHED*/
}