home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / inole2 / chap10 / targdev / targdev.cpp
Encoding:
C/C++ Source or Header  |  1995-05-03  |  6.5 KB  |  251 lines

  1. /*
  2.  * TARGDEV.CPP
  3.  *
  4.  * Sample target device structure handling functions.  This file
  5.  * is intended to be a small repository of functions that might
  6.  * be useful in development:
  7.  *  TargetDeviceToDC          Creates a DC or IC from a
  8.  *                            DVTARGETDEVICE structure.
  9.  *  TargetDeviceFromPrintDlg  Creates a DVTARGETDEVICE structure
  10.  *                            from PrintDlg information.
  11.  *  TargetDeviceCopy          Makes a binary copy of a DVTARGETDEVICE
  12.  *                            structure.
  13.  *  TargerDeviceCompare       Compares two DVTARGETDEVICE structures
  14.  *                            for equality.
  15.  *
  16.  * Copyright (c)1992-1995 Microsoft Corporation, All Rights Reserved
  17.  *
  18.  * Kraig Brockschmidt, Microsoft
  19.  * Internet  :  kraigb@microsoft.com
  20.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  21.  */
  22.  
  23.  
  24. #define INC_OLE2
  25. #include <windows.h>
  26. #include <ole2.h>
  27. #include <ole2ver.h>
  28. #include <memory.h>
  29. #ifndef WIN32
  30. #include <print.h>
  31. #endif
  32.  
  33.  
  34. /*
  35.  * TargetDeviceToDC
  36.  *
  37.  * Purpose:
  38.  *  Given an OLE DVTARGETDEVICE structure, create an hDC for that
  39.  *  device, where that hDC can optionally be an IC only.
  40.  *
  41.  * Parameters:
  42.  *  ptd             DVTARGETDEVICE * describing the device.
  43.  *  fICOnly         BOOL indicating that the caller wants an IC
  44.  *                  not a DC.
  45.  *
  46.  * Return Value:
  47.  *  HDC             Valid DC for the device or NULL.
  48.  */
  49.  
  50. STDAPI_(HDC) TargetDeviceToDC(DVTARGETDEVICE *ptd, BOOL fICOnly)
  51.     {
  52.     LPDEVNAMES pDevNames;
  53.     LPDEVMODE  pDevMode;
  54.     LPTSTR     pszDriver;
  55.     LPTSTR     pszDevice;
  56.     LPTSTR     pszPort;
  57.  
  58.     //Screen DC for a NULL device.
  59.     if (NULL==ptd)
  60.         {
  61.         if (fICOnly)
  62.             return CreateIC(TEXT("DISPLAY"), NULL, NULL, NULL);
  63.         else
  64.             return CreateDC(TEXT("DISPLAY"), NULL, NULL, NULL);
  65.         }
  66.  
  67.     pDevNames=(LPDEVNAMES)ptd; // offset for size field
  68.  
  69.     pDevMode=(ptd->tdExtDevmodeOffset==0) ? NULL
  70.         : (LPDEVMODE)((LPSTR)ptd+ptd->tdExtDevmodeOffset);
  71.  
  72.     pszDriver=(LPTSTR)pDevNames+ptd->tdDriverNameOffset;
  73.     pszDevice=(LPTSTR)pDevNames+ptd->tdDeviceNameOffset;
  74.     pszPort  =(LPTSTR)pDevNames+ptd->tdPortNameOffset;
  75.  
  76.     if (fICOnly)
  77.         return CreateIC(pszDriver, pszDevice, pszPort, pDevMode);
  78.     else
  79.         return CreateDC(pszDriver, pszDevice, pszPort, pDevMode);
  80.     }
  81.  
  82.  
  83.  
  84.  
  85. /*
  86.  * TargetDeviceFromPrintDlg
  87.  *
  88.  * Purpose:
  89.  *  Creates an OLE DVTARGETDEVICE structure from a PRINTDLG
  90.  *  structure as returned from the common dialog API PrintDlg,
  91.  *  which is commonly used for Printer Setup.
  92.  *
  93.  * Parameters:
  94.  *  pPD             LPPRINTDLG containing device information.
  95.  *
  96.  * Return Value:
  97.  *  DVTARGETDEVICE * pointing to the new structure.  This is
  98.  *  allocated with the task memory allocator and should be
  99.  *  freed with CoTaskMemFree.
  100.  */
  101.  
  102. STDAPI_(DVTARGETDEVICE *)TargetDeviceFromPrintDlg(LPPRINTDLG pPD)
  103.     {
  104.     DVTARGETDEVICE  *ptd=NULL;
  105.     LPDEVNAMES       pDevNames, pDN;
  106.     LPDEVMODE        pDevMode, pDM;
  107.     UINT             nMaxOffset;
  108.     LPTSTR           pszName;
  109.     DWORD            dwDevNamesSize, dwDevModeSize, dwPtdSize;
  110.  
  111.     pDN=(LPDEVNAMES)GlobalLock(pPD->hDevNames);
  112.  
  113.     if (NULL==pDN)
  114.         return NULL;
  115.  
  116.     pDM=(LPDEVMODE)GlobalLock(pPD->hDevMode);
  117.  
  118.     if (NULL==pDM)
  119.         {
  120.         GlobalUnlock(pPD->hDevNames);
  121.         return NULL;
  122.         }
  123.  
  124.     nMaxOffset=(pDN->wDriverOffset > pDN->wDeviceOffset)
  125.         ? pDN->wDriverOffset : pDN->wDeviceOffset ;
  126.  
  127.     nMaxOffset=(pDN->wOutputOffset > nMaxOffset)
  128.         ? pDN->wOutputOffset : nMaxOffset ;
  129.  
  130.     pszName=(LPTSTR)pDN+nMaxOffset;
  131.  
  132.     dwDevNamesSize=(DWORD)(nMaxOffset+lstrlen(pszName)+1);
  133.     dwDevModeSize=(DWORD) (pDM->dmSize+pDM->dmDriverExtra);
  134.  
  135.     dwPtdSize=sizeof(DWORD)+dwDevNamesSize+dwDevModeSize;
  136.  
  137.     ptd=(DVTARGETDEVICE *)CoTaskMemAlloc(dwPtdSize);
  138.  
  139.     if (NULL!=ptd)
  140.         {
  141.            ptd->tdSize=(UINT)dwPtdSize;
  142.  
  143.            pDevNames=(LPDEVNAMES) &ptd->tdDriverNameOffset;
  144.        #ifdef WIN32
  145.         memcpy(pDevNames, pDN, (size_t)dwDevNamesSize);
  146.        #else
  147.            _fmemcpy(pDevNames, pDN, (size_t)dwDevNamesSize);
  148.        #endif
  149.  
  150.            pDevMode=(LPDEVMODE)((LPTSTR)&ptd->tdDriverNameOffset
  151.             + dwDevNamesSize);
  152.        #ifdef WIN32
  153.            memcpy(pDevMode, pDM, (size_t)dwDevModeSize);
  154.        #else
  155.         _fmemcpy(pDevMode, pDM, (size_t)dwDevModeSize);
  156.        #endif
  157.  
  158.         ptd->tdDriverNameOffset +=4 ;
  159.         ptd->tdDeviceNameOffset +=4 ;
  160.         ptd->tdPortNameOffset   +=4 ;
  161.         ptd->tdExtDevmodeOffset=(UINT)dwDevNamesSize+4 ;
  162.         }
  163.  
  164.     GlobalUnlock(pPD->hDevNames);
  165.     GlobalUnlock(pPD->hDevMode);
  166.     return ptd;
  167.     }
  168.  
  169.  
  170.  
  171.  
  172.  
  173. /*
  174.  * TargetDeviceCopy
  175.  *
  176.  * Purpose:
  177.  *  Duplicate a DVTARGETDEVICE structure, allocating new memory
  178.  *  for the copy which the caller must free with CoTaskMemFree.
  179.  *
  180.  * Parameters:
  181.  *  ptdSrc          DVTARGETDEVICE * to copy
  182.  *
  183.  * Return Value:
  184.  *  DVTARGETDEVICE * to the copy, NULL if copy fails or ptdSrc
  185.  *                  is NULL.
  186.  */
  187.  
  188. STDAPI_(DVTARGETDEVICE *) TargetDeviceCopy(DVTARGETDEVICE * ptdSrc)
  189.     {
  190.     DVTARGETDEVICE * ptdDest=NULL;
  191.  
  192.     if (NULL==ptdSrc)
  193.         return NULL;
  194.  
  195.     ptdDest=(DVTARGETDEVICE *)CoTaskMemAlloc(ptdSrc->tdSize);
  196.  
  197.     if (NULL!=ptdDest)
  198.        #ifdef WIN32
  199.         memcpy(ptdDest, ptdSrc, (size_t)ptdSrc->tdSize);
  200.        #else
  201.         _fmemcpy(ptdDest, ptdSrc, (size_t)ptdSrc->tdSize);
  202.        #endif
  203.  
  204.     return ptdDest;
  205.     }
  206.  
  207.  
  208.  
  209.  
  210.  
  211. /*
  212.  * TargetDeviceCompare
  213.  *
  214.  * Purpose:
  215.  *  Checks if two DVTARGETDEVICE structures are equivalent.
  216.  *
  217.  * Parameters:
  218.  *  ptdLeft         DVTARGETDEVICE to one structure.
  219.  *  ptdRight        DVTARGETDEVICE to another structure.
  220.  *
  221.  * Return Value:
  222.  *  BOOL            TRUE if the structures are equivalent,
  223.  *                  FALSE otherwise.
  224.  */
  225.  
  226. STDAPI_(BOOL) TargetDeviceCompare(DVTARGETDEVICE *ptdLeft
  227.     , DVTARGETDEVICE * ptdRight)
  228.     {
  229.     //Same address, same devices
  230.     if (ptdLeft==ptdRight)
  231.         return TRUE;
  232.  
  233.     //Either is NULL but not the other, not equal
  234.     if (NULL==ptdLeft || NULL==ptdRight)
  235.         return FALSE;
  236.  
  237.     //Different sizes, not equal
  238.     if (ptdLeft->tdSize!=ptdRight->tdSize)
  239.         return FALSE;
  240.  
  241.     //Fo a binary compare
  242.    #ifdef WIN32
  243.     if (0!=memcmp(ptdLeft, ptdRight, ptdLeft->tdSize))
  244.    #else
  245.     if (0!=_fmemcmp(ptdLeft, ptdRight, (int)ptdLeft->tdSize))
  246.    #endif
  247.     return FALSE;
  248.  
  249.     return TRUE;
  250.     }
  251.