home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / ENUMP2.ZIP / ENUMPROC.C < prev    next >
C/C++ Source or Header  |  1988-06-11  |  2KB  |  78 lines

  1. /* enumproc.c */
  2. /* enumerates all exported procs from DLL file */
  3. /* Andrew Schulman 28-May-1988 */
  4. /* revised 2-Jun-1988 (AS) */
  5.  
  6. #include <stdio.h>
  7.  
  8. /* lifted from MSoft newexe.h */
  9. #define ENEWHDR     0x003C          /* offset of new EXE header */
  10. #define EMAGIC      0x5A4D          /* old EXE magic id:  'MZ'  */
  11. #define NEMAGIC     0x454E          /* new EXE magic id:  'NE'  */
  12.  
  13. typedef struct
  14. {
  15.     unsigned    ne_magic;
  16.     char        ne_ver;
  17.     char        ne_rev;
  18.     unsigned    ne_enttab;
  19.     unsigned    ne_cbenttab;
  20.     long        ne_crc;
  21.     unsigned    ne_flags;
  22.     unsigned    ne_autodata;
  23.     unsigned    ne_heap;
  24.     unsigned    ne_stack;
  25.     long        ne_csip;
  26.     long        ne_sssp;
  27.     unsigned    ne_cseg;
  28.     unsigned    ne_cmod;
  29.     unsigned    ne_cbnrestab;
  30.     unsigned    ne_segtab;
  31.     unsigned    ne_rsrctab;             
  32.     unsigned    ne_restab;              /* offset of resident name table */
  33.     /* etc. -- we could care less */
  34. } NEWEXE;
  35.  
  36. char *enumproc(const char *dll, char *buf, unsigned *ord)
  37. {
  38.     static FILE *f = (FILE *)0;
  39.     unsigned char len;
  40.     
  41.     if (dll == (char *)-1)
  42.         goto closeit;
  43.     else if (dll)
  44.     {
  45.         NEWEXE hdr;
  46.         unsigned start_newexe;
  47.         
  48.         if (f) fclose(f);
  49.         f = fopen(dll, "rb");
  50.         if (! f || getw(f) != EMAGIC)
  51.             goto closeit;
  52.         fseek(f, ENEWHDR, SEEK_SET);
  53.         start_newexe = getw(f);
  54.         fseek(f, start_newexe, SEEK_SET);
  55.         fread(&hdr, sizeof(NEWEXE), 1, f);
  56.         if (hdr.ne_magic != NEMAGIC)
  57.             goto closeit;
  58.         fseek(f, start_newexe, SEEK_SET);
  59.         fseek(f, hdr.ne_restab, SEEK_CUR);
  60.     }
  61.  
  62.     if (len = fgetc(f))
  63.     {
  64.         fread(buf, len, 1, f);
  65.         buf[len] = '\0';
  66.         *ord = getw(f);
  67.         return buf;
  68.     }
  69.     else
  70.     {
  71. closeit:
  72.         if (f) fclose(f);
  73.         f = NULL;
  74.         return NULL;
  75.     }
  76.     /*NOTREACHED*/
  77. }
  78.