home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / wpj_mag / wpjv1n8.zip / GASH.ZIP / METAFILE / MET2.C < prev    next >
C/C++ Source or Header  |  1993-08-14  |  2KB  |  77 lines

  1. /*
  2.  
  3. MET2.C - Reads in metafiles, regardless of whether they have
  4.          a placeable header.
  5.          (C) 1993, Dennis Chuah
  6.  
  7. */
  8.  
  9. #define STRICT
  10. #include <windows.h>
  11. #include <windowsx.h>
  12. #include <metafile.h>
  13.  
  14.  
  15. HMETAFILE WINAPI GetMetaFileBetter (LPCSTR path, METAFILEHEADER far *lpMh)
  16.   {HFILE hFile;
  17.    long filelen, read;
  18.    HMETAFILE hMetafile;
  19.    METAFILEHEADER mh;
  20.    HGLOBAL handle;
  21.    void huge *hpVoid;
  22.  
  23.    /* Open disk metafile */
  24.    hFile = _lopen (path, READ);
  25.    if (hFile == HFILE_ERROR) return NULL;
  26.  
  27.    /* Find the size of the metafile */
  28.    filelen = _llseek (hFile, 0L, 2);
  29.    if (filelen == HFILE_ERROR)
  30.      {_lclose (hFile);
  31.       return NULL;
  32.      }
  33.    if (_llseek (hFile, 0L, 0) == HFILE_ERROR)
  34.      {_lclose (hFile);
  35.       return NULL;
  36.      }
  37.  
  38.    /* See if it is a placeable metafile */
  39.    read = _lread (hFile, &mh, sizeof (METAFILEHEADER));
  40.    if (read < sizeof (METAFILEHEADER) || read == HFILE_ERROR)
  41.      {_lclose (hFile);
  42.       return NULL;
  43.      }
  44.    if (mh.key != 0x9AC6CDD7L)
  45.      {_lclose (hFile);    // If it is, use GetMetaFile
  46.       return GetMetaFile (path);
  47.      } // endif
  48.  
  49.    /* Read the rest of the file in and convert to a HMETAFILE */
  50.    filelen -= sizeof (METAFILEHEADER);
  51.    hpVoid = GlobalAllocPtr (GHND, filelen);
  52.    if (hpVoid == NULL)
  53.      {_lclose (hFile);
  54.       return NULL;
  55.      } // endif
  56.    read = _hread (hFile, hpVoid, filelen);
  57.    if (read == -1 || read < filelen)
  58.      {(void) GlobalFreePtr (hpVoid);
  59.       _lclose (hFile);
  60.       return NULL;
  61.      } // endif
  62.    handle = GlobalPtrHandle (hpVoid);
  63.    hMetafile = (HMETAFILE) SetMetaFileBitsBetter ((HMETAFILE) handle);
  64.    /* Note: don't need to free memory as that will be done by
  65.       DeleteMetaFile -- see SetMetaFileBits docs */
  66.  
  67.    /* If supplied placeable metafile header is valid, return header
  68.       information */
  69.    if (!IsBadHugeReadPtr (lpMh, sizeof (METAFILEHEADER)))
  70.      {hmemcpy (lpMh, &mh, sizeof (METAFILEHEADER));
  71.      } // endif
  72.  
  73.    _lclose (hFile);
  74.    return hMetafile;
  75.   } // end GetMetafileBetter
  76.  
  77.