home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR2 / DESCRIPW.ZIP / DESCRIP.CPP < prev    next >
C/C++ Source or Header  |  1993-09-27  |  4KB  |  135 lines

  1. #include <windows.h>
  2. #include <commdlg.h>
  3. #include <fstream.h>
  4. #include <string.h>
  5.  
  6. int GetModuleDescription(const char *filename, char *descrip, BYTE len);
  7. char achCmdLine[128];
  8. OPENFILENAME ofn;
  9.  
  10. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  11.   {
  12.   char achDescrip[0xFF];
  13.   char szFile[128];
  14.   UINT  i;
  15.   char  szFilter[]=
  16.     "Windows Programs|*.exe|"
  17.     "Windows Libraries|*.dll|"
  18.     "Control Panel Applets|*.cpl|"
  19.     "Screen Savers|*.scr|"
  20.     "All Files (*.*)|*.*|";
  21.   char  szDefExt[]="exe";
  22.  
  23.   for (i = 0; szFilter[i] != '\0'; i++)
  24.     {
  25.     if (szFilter[i] == '|')
  26.        szFilter[i] = '\0';
  27.     }
  28.  
  29.   /* Set all structure members to zero. */
  30.   memset(&ofn, 0, sizeof(OPENFILENAME));
  31.  
  32.   _fstrcpy(achCmdLine,lpCmdLine);
  33.  
  34.  
  35.   if (achCmdLine[0] == '\0')
  36.     {
  37.     i=1;
  38.  
  39.     do
  40.       {
  41.       szFile[0] = '\0';
  42.       ofn.lStructSize = sizeof(OPENFILENAME);
  43.       ofn.hwndOwner = NULL;
  44.       ofn.lpstrFilter = szFilter;
  45.       ofn.nFilterIndex = 1;
  46.       ofn.lpstrFile= szFile;
  47.       ofn.nMaxFile = sizeof(szFile);
  48.       ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
  49.       ofn.lpstrDefExt = szDefExt;
  50.  
  51.       i = GetOpenFileName(&ofn);
  52.  
  53.       if ( i != 0)
  54.         {
  55.         switch(GetModuleDescription(szFile,achDescrip,sizeof(achDescrip)))
  56.           {
  57.       case 0:  MessageBox(NULL,achDescrip,"Module Description",
  58.                  MB_OK | MB_ICONINFORMATION);
  59.                    break;
  60.       case 1:  MessageBox(NULL,"File processing error","Module Description",
  61.              MB_OK | MB_ICONINFORMATION);
  62.                    break;
  63.       case 2:  MessageBox(NULL,"Invalid executable header information.\n\n"
  64.                    "    May not be a Windows 3.1\n"
  65.                    "    Executable or Dynamic Link\n"
  66.                    "    Library","Module Description",
  67.              MB_OK | MB_ICONINFORMATION);
  68.       }
  69.         }
  70.       }
  71.     while(i != 0);
  72.     }
  73.  
  74.   else
  75.     {
  76.     switch(GetModuleDescription(achCmdLine,achDescrip,sizeof(achDescrip)))
  77.       {
  78.       case 0:  MessageBox(NULL,achDescrip,"Module Description",
  79.                  MB_OK | MB_ICONINFORMATION);
  80.                break;
  81.       case 1:  MessageBox(NULL,"File processing error","Module Description",
  82.                  MB_OK | MB_ICONINFORMATION);
  83.                break;
  84.       case 2:  MessageBox(NULL,"Invalid executable header information.\n\n"
  85.                                "    May not be a Windows 3.1\n"
  86.                    "    Executable or Dynamic Link\n"
  87.                    "    Library","Module Description",
  88.                        MB_OK | MB_ICONINFORMATION);
  89.       }
  90.     }
  91.  
  92.   return(0);
  93.   }
  94.  
  95. int GetModuleDescription(const char *filename, char *descrip, BYTE len)
  96.   {
  97.   /* finds the module description found in the non-resident name table of the
  98.            Windows Executable Header
  99.      filename    path to the file
  100.      descrip     address of return buffer
  101.      len         size of the return buffer
  102.      returns 0 and alters descrip if found
  103.          1 if file error
  104.          2 if invalid header information */
  105.  
  106.   ifstream ifsExeFile;
  107.   UINT offset;
  108.   BYTE bDescripSize;
  109.  
  110.   ifsExeFile.open( filename, ios::in|ios::binary);
  111.   ifsExeFile.seekg(0x3C);
  112.   ifsExeFile.read((char*)&offset,2);
  113.   ifsExeFile.seekg(offset);
  114.   ifsExeFile.read((char*)&offset,2);
  115.   if (ifsExeFile.bad())
  116.     return(1);
  117.  
  118.   if (!(LOBYTE(offset) == 'N' && HIBYTE(offset) == 'E'))
  119.     return(2);
  120.   else
  121.     {
  122.     ifsExeFile.seekg(0x2A,ios::cur);
  123.     ifsExeFile.read((char*)&offset,2);
  124.     ifsExeFile.seekg(offset);
  125.     ifsExeFile.read(&bDescripSize,1);
  126.     if (bDescripSize > len-1)
  127.       bDescripSize = len-1;
  128.     ifsExeFile.read(descrip,(int)bDescripSize);
  129.     if (ifsExeFile.bad())
  130.       return(1);
  131.  
  132.     descrip[bDescripSize] = '\0';
  133.     }
  134.   return(0);
  135.   }