home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / PRNT16.ZIP / PRNT16.C < prev    next >
Text File  |  1992-07-17  |  15KB  |  367 lines

  1. /**************************************************************************/
  2. /*------------------------------- PRNT16.C -------------------------------*/
  3. /*                                                                        */
  4. /* PRINTING TEXT TO THE DEFAULT OS/2 1.3 PRINTER                          */
  5. /*                                                                        */
  6. /* On my printer with my dot matrix printer, the INI settings:            */
  7. /*    PM_SPOOLER                                                          */
  8. /*        PRINTER: EPSON01;                                               */
  9. /*                                                                        */
  10. /*    PM_SPOOLER_PRINTER                                                  */
  11. /*        EPSON01: LPT1;EPSON.FX-286e;EPSON01;;                           */
  12. /*                                                                        */
  13. /* You can use the Splxxx or Devxxx calls to print text to the printer.   */
  14. /* This program shows how to use both. It is conditionally compiled       */
  15. /* depending on the DEV macro in the makefile. It is conditionally linked */
  16. /* using the PROGTYPE macro which is dependent on the DEV macro.          */
  17. /*                                                                        */
  18. /* To make this program use Splxxx calls, comment out the DEV macro in    */
  19. /* the makefile like this: #DEV=-DDEV                                     */
  20. /* This will undefine DEV.                                                */
  21. /*                                                                        */
  22. /* To make this program use Devxxx calls, change the DEV macro in the     */
  23. /* make file to be DEV=-DDEV                                              */
  24. /*                                                                        */
  25. /* NOTES: This program is set up as a PM program if it is compiled to use */
  26. /*        the DevXxx calls since DevOpenDC needs an anchor block handle.  */
  27. /*        If you use the Splxxx Calls, it does not need to be a PM        */
  28. /*        program. The make file controls whether or not it is a PM       */
  29. /*        program by the PROGTYPE macro which is automatically set by the */
  30. /*        defining or not defining of the DEV macro.                      */
  31. /*                                                                        */
  32. /* Rick Fishman                                                           */
  33. /* Code Blazers, Inc.                                                     */
  34. /* 4113 Apricot                                                           */
  35. /* Irvine, CA 92720                                                       */
  36. /* CIS ID: 72251,750                                                      */
  37. /*                                                                        */
  38. /*------------------------------------------------------------------------*/
  39. /**************************************************************************/
  40. #define INCL_DEV
  41. #define INCL_SPL
  42. #define INCL_WINERRORS
  43. #define INCL_WINMESSAGEMGR
  44. #define INCL_WINSHELLDATA
  45.  
  46. #include <os2.h>
  47. #include <string.h>
  48. #include <stdio.h>
  49. #include <stdarg.h>
  50. #include <stdlib.h>
  51.  
  52. USHORT     DoDevCalls    ( VOID );
  53. USHORT     DoSplCalls    ( VOID );
  54. VOID       PausePrintJob ( VOID );
  55. VOID cdecl DisplayMessage( PSZ szFormat, ... );
  56.  
  57. #define DOCNAME         "Testdoc.Ric"
  58. #define MESSAGE_SIZE    1024
  59.  
  60. #ifdef DEV                  // Turn off WinMessageBox if nonPM program
  61.     #define Msg DisplayMessage
  62. #else
  63.     #define Msg printf
  64. #endif
  65.  
  66. // Default settings
  67.  
  68. DEVOPENSTRUC dop = { "LPT1Q", "IBM4201", 0L, "PM_Q_RAW", 0L, 0L, 0L, 0L, 0L };
  69.  
  70. DRIVDATA drivd;
  71.  
  72. PSZ  szDeviceName;
  73.  
  74. HSPL hspl;
  75. HDC  hdc;
  76.  
  77. CHAR szMessage[] = "This text looks alright to me!\n";
  78.  
  79. CHAR szBuf1[ 256 ];
  80. CHAR szBuf2[ 256 ];
  81.  
  82. /**********************************************************************/
  83. /*------------------------------ main --------------------------------*/
  84. /*                                                                    */
  85. /*  PROGRAM ENTRYPOINT                                                */
  86. /*                                                                    */
  87. /*  INPUT: nothing                                                    */
  88. /*                                                                    */
  89. /*  1.                                                                */
  90. /*                                                                    */
  91. /*  OUTPUT: nothing                                                   */
  92. /*                                                                    */
  93. /*--------------------------------------------------------------------*/
  94. /**********************************************************************/
  95. int main()
  96. {
  97.     USHORT usSize = (SHORT) PrfQueryProfileString( HINI_PROFILE,
  98.                                                    SPL_INI_SPOOLER, "PRINTER",
  99.                                                    NULL, szBuf1,
  100.                                                    (LONG) sizeof( szBuf1 ) );
  101.     if( usSize )
  102.     {
  103.         szBuf1[ usSize - 2 ] = 0;    // get rid of terminating semicolon
  104.  
  105.         if( PrfQueryProfileString( HINI_PROFILE, SPL_INI_PRINTER, szBuf1, NULL,
  106.                                    szBuf2, (LONG) sizeof( szBuf2 ) ) )
  107.         {
  108.             dop.pszDriverName  = strchr( szBuf2, ';' ) + 1;
  109.             dop.pszLogAddress  = strchr( dop.pszDriverName, ';' ) + 1;
  110.             szDeviceName       = strchr( dop.pszDriverName, '.' );
  111.             dop.pszLogAddress  = strtok( dop.pszLogAddress, ",;" );
  112.             dop.pszDriverName  = strtok( dop.pszDriverName, ",.;" );
  113.         }
  114.         else
  115.             Msg( "PrfQueryProfileString for printer data failed\n" );
  116.     }
  117.     else
  118.         Msg( "PrfQueryProfileString for default printer failed\n" );
  119.  
  120. #ifdef DEV
  121.     DoDevCalls();
  122. #else
  123.     DoSplCalls();
  124. #endif
  125.  
  126.     return 0;
  127. }
  128.  
  129. /**********************************************************************/
  130. /*--------------------------- DoDevCalls -----------------------------*/
  131. /*                                                                    */
  132. /*  DO Dev API CALLS TO PRINT STUFF.                                  */
  133. /*                                                                    */
  134. /*  INPUT: nothing                                                    */
  135. /*                                                                    */
  136. /*  1.                                                                */
  137. /*                                                                    */
  138. /*  OUTPUT: nothing                                                   */
  139. /*                                                                    */
  140. /*--------------------------------------------------------------------*/
  141. /**********************************************************************/
  142. USHORT DoDevCalls( )
  143. {
  144.     HDC       hdc;
  145.     ULONG     ulBytes;
  146.     HAB       hab = WinInitialize( 0 );
  147.     HMQ       hmq;
  148.  
  149.     if( !hab )
  150.         DosBeep( 100, 100 );
  151.     else
  152.         hmq = WinCreateMsgQueue( hab, 0 );
  153.  
  154.     if( !hmq )
  155.         DosBeep( 100, 100 );
  156.  
  157. // If this printer driver has multiple flavors (like the EPSON), it will have
  158. // a dot separating the printer driver name from the qualifier. For instance
  159. // my dot-matrix driver is EPSON.FX-286e; szDeviceName now points to the period
  160. // In this case we need to include the driver data to differentiate which
  161. // mode to run the driver in.
  162.  
  163.     if( szDeviceName )
  164.     {
  165.         drivd.cb = sizeof( DRIVDATA );
  166.  
  167.         szDeviceName++;
  168.  
  169.         szDeviceName = strtok( szDeviceName, ";" );
  170.  
  171.         strcpy( drivd.szDeviceName, szDeviceName );
  172.  
  173.         dop.pdriv = &drivd;
  174.     }
  175.  
  176. // Create a printer device context for the print queue
  177.  
  178.     if( !(hdc = DevOpenDC( hab, OD_QUEUED, "*", 4L,
  179.                            (PDEVOPENDATA) &dop, (HDC) NULL )) )
  180.         Msg( "DevOpenDC failed. RC=%x\n", (USHORT) WinGetLastError( 0 ) );
  181.  
  182. // Start the spool file and name it
  183.  
  184.     if( DEV_OK != DevEscape( hdc, DEVESC_STARTDOC, strlen( DOCNAME ),
  185.                              (PBYTE) DOCNAME, &ulBytes, NULL ) )
  186.         Msg( "DevEscape STARTDOC failed. RC=%x\n",
  187.                 (USHORT) WinGetLastError( 0 ) );
  188.  
  189. // This function will show how to put the print job on hold
  190.  
  191. //    PausePrintJob();
  192.  
  193. // Write data to the spool file
  194.  
  195.     if( DEV_OK != DevEscape( hdc, DEVESC_RAWDATA, strlen( szMessage ),
  196.                              (PBYTE) szMessage, &ulBytes, NULL ) )
  197.         Msg( "DevEscape RAWDATA failed. RC=%x\n",
  198.                 (USHORT) WinGetLastError( 0 ) );
  199.  
  200. // Close the spool file and start it printing
  201.  
  202.     if( DEV_OK != DevEscape( hdc, DEVESC_ENDDOC, 0, NULL, &ulBytes, NULL ) )
  203.         Msg( "DevEscape ENDDOC failed. RC=%x\n",
  204.                 (USHORT) WinGetLastError( 0 ) );
  205.  
  206. // Close the device context
  207.  
  208.     if( DEV_ERROR == (HMF) DevCloseDC( hdc ) )
  209.         Msg( "DevCloseDC failed. RC=%x\n", (USHORT) WinGetLastError( 0 ) );
  210.  
  211.     WinDestroyMsgQueue( hmq );
  212.  
  213.     WinTerminate( hab );
  214.  
  215.     return 0;
  216. }
  217.  
  218. /**********************************************************************/
  219. /*--------------------------- DoSplCalls -----------------------------*/
  220. /*                                                                    */
  221. /*  DO Spl API CALLS TO PRINT STUFF.                                  */
  222. /*                                                                    */
  223. /*  INPUT: nothing                                                    */
  224. /*                                                                    */
  225. /*  1.                                                                */
  226. /*                                                                    */
  227. /*  OUTPUT: nothing                                                   */
  228. /*                                                                    */
  229. /*--------------------------------------------------------------------*/
  230. /**********************************************************************/
  231. USHORT DoSplCalls( )
  232. {
  233.     HSPL        hspl;
  234.     USHORT      usJobID;
  235.  
  236. // Open the Print Manager
  237.  
  238.     if( !(hspl = SplQmOpen( "*", 4L, (PQMOPENDATA) &dop )) )
  239.         Msg( "SplQmOpen failed\n" );
  240.  
  241. // Start the spool file and name it
  242.  
  243.     if( !(SplQmStartDoc( hspl, DOCNAME )) )
  244.         Msg( "SplQmStartDoc failed\n" );
  245.  
  246. // This function will show how to put the print job on hold
  247.  
  248. //    PausePrintJob();
  249.  
  250. // Write to the spool file
  251.  
  252.     if( !SplQmWrite( hspl, strlen( szMessage ), szMessage ) )
  253.         Msg( "SplQmWrite failed\n" );
  254.  
  255. // End the spool file. This starts it printing.
  256.  
  257.     if( !(usJobID = SplQmEndDoc( hspl )) )
  258.         Msg( "SplQmEndDoc failed\n" );
  259.  
  260. //****************************************************************************
  261. // Cannot do the DosPrintJobPause here because SplQmEndDoc starts the job
  262. // printing and then it is too late to pause it (DosPrintJobPause gets a 2164
  263. // error if we issue it after the SplQmEndDoc). Too bad because that call
  264. // returns the job ID. So instead we had to do all the code in the
  265. // PausePrintJob below.
  266. //****************************************************************************
  267.  
  268. // Close the Print Manager
  269.  
  270.     if( !SplQmClose( hspl ) )
  271.         Msg( "SplQmClose failed\n" );
  272.  
  273.     return 0;
  274. }
  275.  
  276. /**********************************************************************/
  277. /*-------------------------- PausePrintJob ---------------------------*/
  278. /*                                                                    */
  279. /*  PAUSE THE PRINT JOB.                                              */
  280. /*                                                                    */
  281. /*  INPUT: nothing                                                    */
  282. /*                                                                    */
  283. /*  1.                                                                */
  284. /*                                                                    */
  285. /*  OUTPUT: nothing                                                   */
  286. /*                                                                    */
  287. /*--------------------------------------------------------------------*/
  288. /**********************************************************************/
  289. VOID PausePrintJob( )
  290. {
  291.     USHORT      usJobID = 0, usJobsReturned, usJobsAvailable, usSize, usCount;
  292.     SPLERR      splerr;
  293.     PPRJINFO2   pprjinfo2, prjPtr;
  294.  
  295.     usSize = sizeof( PRJINFO2 ) * 10;
  296.  
  297.     if( !(pprjinfo2 = (PPRJINFO2) malloc( usSize )) )
  298.         Msg( "malloc failed\n" );
  299.  
  300.     if( splerr = DosPrintJobEnum( NULL, dop.pszLogAddress, 2, (PBYTE) pprjinfo2,
  301.                                   usSize, &usJobsReturned, &usJobsAvailable ) )
  302.         Msg( "DosPrintJobEnum failed. RC = %u\n", splerr );
  303.  
  304.     for( usCount = 0, prjPtr = pprjinfo2;
  305.          usCount < usJobsReturned;
  306.          usCount++, prjPtr++ )
  307.     {
  308.         if( !stricmp( prjPtr->pszDocument, DOCNAME ) )
  309.         {
  310.             usJobID = prjPtr->uJobId;
  311.             break;
  312.         }
  313.     }
  314.  
  315.     if( !usJobID )
  316.         Msg( "Couldn't find the job using DosPrintJobEnum.\n" );
  317.  
  318.     if( splerr = DosPrintJobPause( NULL, usJobID ) )
  319.         Msg( "DosPrintJobPause failed. RC = %u\n", splerr );
  320.  
  321.     free( pprjinfo2 );
  322. }
  323.  
  324. /**********************************************************************/
  325. /*------------------------- DisplayMessage ---------------------------*/
  326. /*                                                                    */
  327. /*  INPUT: a message in printf format with its parms                  */
  328. /*                                                                    */
  329. /*  DISPLAY A MESSAGE TO THE USER.                                    */
  330. /*                                                                    */
  331. /*  1. Format the message using vsprintf.                             */
  332. /*  2. Sound a warning sound.                                         */
  333. /*  3. Display the message in a message box.                          */
  334. /*                                                                    */
  335. /*  OUTPUT: nothing                                                   */
  336. /*                                                                    */
  337. /*--------------------------------------------------------------------*/
  338. /**********************************************************************/
  339. VOID cdecl DisplayMessage( PSZ szFormat,... )
  340. {
  341.     PSZ     szMsg;
  342.     va_list argptr;
  343.  
  344.     if( (szMsg = (PSZ) malloc( MESSAGE_SIZE )) == NULL )
  345.     {
  346.         DosBeep( 1000, 1000 );
  347.  
  348.         return;
  349.     }
  350.  
  351.     va_start( argptr, szFormat );
  352.  
  353.     vsprintf( szMsg, szFormat, argptr );
  354.  
  355.     va_end( argptr );
  356.  
  357.     szMsg[ MESSAGE_SIZE - 1 ] = 0;
  358.  
  359.     WinAlarm( HWND_DESKTOP, WA_WARNING );
  360.  
  361.     WinMessageBox(  HWND_DESKTOP, HWND_DESKTOP, szMsg,
  362.                     "prnt16.exe", 1, MB_OK | MB_MOVEABLE );
  363.  
  364.     free( szMsg );
  365. }
  366.  
  367.