home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / STDPRN.ZIP / OPENPRN.C < prev    next >
Text File  |  1989-07-30  |  5KB  |  98 lines

  1. /****************************************************************************
  2.  *  HDC APIENTRY OpenDefaultPrinterDC( HAB hab,                             *
  3.  *                                     PSZ pszAppName,                      *
  4.  *                                     PSZ pszRedirect )                    *
  5.  *                                                                          *
  6.  *  Purpose             This function opens a device context for the        *
  7.  *                      current default printer.                            *
  8.  *                                                                          *
  9.  *  Parameters          hab contains the anchor block handle.               *
  10.  *                                                                          *
  11.  *                      pszAppName points to a null-terminated string       *
  12.  *                      that contains the name of the application.          *
  13.  *                                                                          *
  14.  *                      pszRedirect points to a null-terminated string      *
  15.  *                      that contains the name of the file to which         *
  16.  *                      printer output is to be redirected. This parameter  *
  17.  *                      is NULL if no output redirection is desired.        *
  18.  *                                                                          *
  19.  *  Return Value        This function returns a handle to the opened        *
  20.  *                      printer device context. This return value is        *
  21.  *                      DEV_ERROR if the function is unsuccessful.          *
  22.  ****************************************************************************/
  23.  
  24.     #define INCL_DEV
  25.     #define INCL_WINSHELLDATA
  26.     #include <os2.h>
  27.     #include <string.h>
  28.  
  29. /****************************************************************************/
  30.     HDC EXPENTRY OpenDefaultPrinterDC( HAB hab,PSZ pszAppName,PSZ pszRedirect )
  31.     {
  32.         #define MAX_STRING_LEN  256
  33.  
  34.         static CHAR     szPrinter[MAX_STRING_LEN];
  35.         static CHAR     szDetails[MAX_STRING_LEN];
  36.         static DRIVDATA driv;
  37.  
  38.         DEVOPENSTRUC    dop;
  39.         PCHAR           pchDelimiter;
  40.  
  41.     /* Get the default printer name, for example, "PRINTER1" */
  42.         WinQueryProfileString( hab,
  43.                 "PM_SPOOLER",       // section name
  44.                 "PRINTER",          // keyname
  45.                 ";",                // default
  46.                 szPrinter,          // profile string
  47.                 MAX_STRING_LEN );   // maximum characters
  48.  
  49.         if ( NULL != (pchDelimiter = strchr(szPrinter,';')) )
  50.             *pchDelimiter = '\0';
  51.  
  52.     /* Get the printer details.
  53.      *  Fill in a supplied string with substrings:
  54.      *  <physical port>;<driver name>;<queue port>;<network params>;
  55.      */
  56.         WinQueryProfileString( hab,
  57.                 "PM_SPOOLER_PRINTER",
  58.                 szPrinter,
  59.                 ";;;;",
  60.                 szDetails,
  61.                 MAX_STRING_LEN );
  62.  
  63.     /* Fill in the DEVOPENSTRUC and DRIVDATA structures */
  64.         if ( NULL == (pchDelimiter = strchr(szDetails,';')) )
  65.             return DEV_ERROR;
  66.         dop.pszDriverName = pchDelimiter + 1;
  67.         if ( NULL == (pchDelimiter = strchr(dop.pszDriverName,';')) )
  68.             return DEV_ERROR;
  69.         dop.pszLogAddress = pchDelimiter + 1;
  70.         *(dop.pszLogAddress + strcspn(dop.pszLogAddress,",;")) = '\0';
  71.         *(dop.pszDriverName + strcspn(dop.pszDriverName,",;")) = '\0';
  72.         dop.pszComment = pszAppName;
  73.         if ( NULL != (pchDelimiter = strchr(dop.pszDriverName,'.')) )
  74.             {
  75.             driv.cb = sizeof(DRIVDATA) - 1;
  76.             driv.lVersion = 0L;
  77.             *pchDelimiter = '\0';
  78.             strncpy( driv.szDeviceName,pchDelimiter+1,
  79.                      sizeof driv.szDeviceName );
  80.             dop.pdriv = &driv;
  81.             }
  82.         else
  83.             dop.pdriv = NULL;
  84.         dop.pszDataType = "PM_Q_RAW";
  85.  
  86.     /* Reset logical address if output is to be redirected */
  87.         if ( pszRedirect != NULL ) dop.pszLogAddress = pszRedirect;
  88.  
  89.     /* Now open the device context an return to caller */
  90.         return DevOpenDC( hab,
  91.                           (pszRedirect == NULL) ? OD_QUEUED : OD_DIRECT,
  92.                           "*",
  93.                           5L,         // use first five fields.
  94.                           (PDEVOPENDATA) &dop,
  95.                           (HDC) NULL );
  96.     }
  97. /****************************************************************************/
  98.