home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 19 Printer / 19-Printer.zip / PRNT32.ZIP / PRNT32.C < prev    next >
Text File  |  1992-07-17  |  11KB  |  287 lines

  1. /**************************************************************************/
  2. /*------------------------------- PRNT32.C -------------------------------*/
  3. /*                                                                        */
  4. /* PRINTING TEXT TO THE DEFAULT OS/2 2.0 PRINTER                          */
  5. /*                                                                        */
  6. /* You can use the Splxxx or Devxxx calls to print text to the printer.   */
  7. /* This program shows how to use both. It is conditionally compiled       */
  8. /* depending on the DEV macro in the makefile. It is conditionally linked */
  9. /* using the PROGTYPE macro which is dependent on the DEV macro.          */
  10. /*                                                                        */
  11. /* To make this program use Splxxx calls, comment out the DEV macro in    */
  12. /* the makefile like this: #DEV=-DDEV                                     */
  13. /* This will undefine DEV.                                                */
  14. /*                                                                        */
  15. /* To make this program use Devxxx calls, change the DEV macro in the     */
  16. /* make file to be DEV=-DDEV                                              */
  17. /*                                                                        */
  18. /* NOTES: This program is set up as a PM program if it is compiled to use */
  19. /*        the DevXxx calls since DevOpenDC needs an anchor block handle.  */
  20. /*        If you use the Splxxx Calls, it does not need to be a PM        */
  21. /*        program. The make file controls whether or not it is a PM       */
  22. /*        program by the PROGTYPE macro which is automatically set by the */
  23. /*        defining or not defining of the DEV macro.                      */
  24. /*                                                                        */
  25. /* Rick Fishman                                                           */
  26. /* Code Blazers, Inc.                                                     */
  27. /* 4113 Apricot                                                           */
  28. /* Irvine, CA 92720                                                       */
  29. /* CIS ID: 72251,750                                                      */
  30. /*                                                                        */
  31. /*------------------------------------------------------------------------*/
  32. /**************************************************************************/
  33.  
  34. #define INCL_DEV
  35. #define INCL_SPL
  36. #define INCL_SPLDOSPRINT
  37. #define INCL_WINERRORS
  38. #define INCL_WINMESSAGEMGR
  39. #define INCL_WINSHELLDATA
  40.  
  41. #include <os2.h>
  42. #include <string.h>
  43. #include <stdio.h>
  44. #include <stdarg.h>
  45. #include <stdlib.h>
  46.  
  47. INT    main          ( VOID );
  48. USHORT DoDevCalls    ( VOID );
  49. USHORT DoSplCalls    ( VOID );
  50. VOID   DisplayMessage( PSZ szFormat, ... );
  51.  
  52. #define DOCNAME         "Testdoc.Ric"
  53. #define MESSAGE_SIZE    1024
  54.  
  55. #ifdef DEV                  // Turn off WinMessageBox if nonPM program
  56.     #define Msg DisplayMessage
  57. #else
  58.     #define Msg printf
  59. #endif
  60.  
  61. // Default settings
  62.  
  63. DEVOPENSTRUC dop = { "LPT1Q", "IBM4201", 0, "PM_Q_RAW", 0, 0, 0, 0, 0 };
  64.  
  65. CHAR szMessage[] = "This text looks alright to me!\n";
  66.  
  67. CHAR szBuf[ 256 ];
  68.  
  69. /**********************************************************************/
  70. /*------------------------------ main --------------------------------*/
  71. /*                                                                    */
  72. /*  PROGRAM ENTRYPOINT                                                */
  73. /*                                                                    */
  74. /*  INPUT: nothing                                                    */
  75. /*                                                                    */
  76. /*  1.                                                                */
  77. /*                                                                    */
  78. /*  OUTPUT: nothing                                                   */
  79. /*                                                                    */
  80. /*--------------------------------------------------------------------*/
  81. /**********************************************************************/
  82. INT main()
  83. {
  84.     PPRQINFO3 pprq3 = NULL;
  85.     USHORT    usSize = (SHORT) PrfQueryProfileString( HINI_PROFILE,
  86.                                         SPL_INI_SPOOLER, "QUEUE", NULL, szBuf,
  87.                                         (LONG) sizeof( szBuf ) );
  88.     if( usSize )
  89.     {
  90.         ULONG   cbNeeded, ulRC;
  91.         PCH     pchDot;
  92.  
  93.         szBuf[ usSize - 2 ] = 0;    // get rid of terminating semicolon
  94.  
  95.         dop.pszLogAddress = szBuf;  // default queue
  96.  
  97.         SplQueryQueue( NULL, szBuf, 3, NULL, 0, &cbNeeded );
  98.  
  99.         pprq3 = (PPRQINFO3) malloc( cbNeeded );
  100.  
  101.         if( !pprq3 )
  102.             Msg( "Out of memory" );
  103.  
  104.         ulRC = SplQueryQueue( NULL, szBuf, 3, pprq3, cbNeeded, &cbNeeded );
  105.  
  106.         if( ulRC )
  107.             Msg( "SplQueryQueue RC: %u", ulRC );
  108.  
  109.         pchDot = strchr( pprq3->pszDriverName, '.' );
  110.  
  111.         if( pchDot )
  112.             *pchDot = 0;
  113.  
  114.         dop.pszDriverName = pprq3->pszDriverName;
  115.  
  116.         dop.pdriv = pprq3->pDriverData;
  117.     }
  118.     else
  119.         Msg( "PrfQueryProfileString for default queue failed\n" );
  120.  
  121. #ifdef DEV
  122.     DoDevCalls();
  123. #else
  124.     DoSplCalls();
  125. #endif
  126.  
  127.     if( pprq3 )
  128.         free( pprq3 );
  129.  
  130.     return 0;
  131. }
  132.  
  133. /**********************************************************************/
  134. /*--------------------------- DoDevCalls -----------------------------*/
  135. /*                                                                    */
  136. /*  DO Dev API CALLS TO PRINT STUFF.                                  */
  137. /*                                                                    */
  138. /*  INPUT: nothing                                                    */
  139. /*                                                                    */
  140. /*  1.                                                                */
  141. /*                                                                    */
  142. /*  OUTPUT: nothing                                                   */
  143. /*                                                                    */
  144. /*--------------------------------------------------------------------*/
  145. /**********************************************************************/
  146. USHORT DoDevCalls( )
  147. {
  148.     HDC     hdc;
  149.     LONG    lBytes;
  150.     HAB     hab = WinInitialize( 0 );
  151.     HMQ     hmq;
  152.  
  153.     if( !hab )
  154.         DosBeep( 100, 100 );
  155.     else
  156.         hmq = WinCreateMsgQueue( hab, 0 );
  157.  
  158.     if( !hmq )
  159.         DosBeep( 100, 100 );
  160.  
  161. // Create a printer device context for the print queue
  162.  
  163.     if( !(hdc = DevOpenDC( hab, OD_QUEUED, "*", 4L,
  164.                            (PDEVOPENDATA) &dop, (HDC) NULL )) )
  165.         Msg( "DevOpenDC failed. RC=%x\n", (USHORT) WinGetLastError( 0 ) );
  166.  
  167. // Start the spool file and name it
  168.  
  169.     if( DEV_OK != DevEscape( hdc, DEVESC_STARTDOC, strlen( DOCNAME ),
  170.                              DOCNAME, &lBytes, NULL ) )
  171.         Msg( "DevEscape STARTDOC failed. RC=%x\n",
  172.                 (USHORT) WinGetLastError( 0 ) );
  173.  
  174. // Write data to the spool file
  175.  
  176.     if( DEV_OK != DevEscape( hdc, DEVESC_RAWDATA, strlen( szMessage ),
  177.                              (PBYTE) szMessage, &lBytes, NULL ) )
  178.         Msg( "DevEscape RAWDATA failed. RC=%x\n",
  179.                 (USHORT) WinGetLastError( 0 ) );
  180.  
  181. // Close the spool file and start it printing
  182.  
  183.     if( DEV_OK != DevEscape( hdc, DEVESC_ENDDOC, 0, NULL, &lBytes, NULL ) )
  184.         Msg( "DevEscape ENDDOC failed. RC=%x\n",
  185.                 (USHORT) WinGetLastError( 0 ) );
  186.  
  187. // Close the device context
  188.  
  189.     if( DEV_ERROR == (HMF) DevCloseDC( hdc ) )
  190.         Msg( "DevCloseDC failed. RC=%x\n", (USHORT) WinGetLastError( 0 ) );
  191.  
  192.     WinDestroyMsgQueue( hmq );
  193.  
  194.     WinTerminate( hab );
  195.  
  196.     return 0;
  197. }
  198.  
  199. /**********************************************************************/
  200. /*--------------------------- DoSplCalls -----------------------------*/
  201. /*                                                                    */
  202. /*  DO Spl API CALLS TO PRINT STUFF.                                  */
  203. /*                                                                    */
  204. /*  INPUT: nothing                                                    */
  205. /*                                                                    */
  206. /*  1.                                                                */
  207. /*                                                                    */
  208. /*  OUTPUT: nothing                                                   */
  209. /*                                                                    */
  210. /*--------------------------------------------------------------------*/
  211. /**********************************************************************/
  212. USHORT DoSplCalls( )
  213. {
  214.     HSPL hspl;
  215.  
  216. // Open the Print Manager
  217.  
  218.     if( !(hspl = SplQmOpen( "*", 4L, (PQMOPENDATA) &dop )) )
  219.         Msg( "SplQmOpen failed\n" );
  220.  
  221. // Start the spool file and name it
  222.  
  223.     if( !(SplQmStartDoc( hspl, DOCNAME )) )
  224.         Msg( "SplQmStartDoc failed\n" );
  225.  
  226. // Write to the spool file
  227.  
  228.     if( !SplQmWrite( hspl, strlen( szMessage ), szMessage ) )
  229.         Msg( "SplQmWrite failed\n" );
  230.  
  231. // End the spool file. This starts it printing.
  232.  
  233.     if( !SplQmEndDoc( hspl ) )
  234.         Msg( "SplQmEndDoc failed\n" );
  235.  
  236. // Close the Print Manager
  237.  
  238.     if( !SplQmClose( hspl ) )
  239.         Msg( "SplQmClose failed\n" );
  240.  
  241.     return 0;
  242. }
  243.  
  244. /**********************************************************************/
  245. /*------------------------- DisplayMessage ---------------------------*/
  246. /*                                                                    */
  247. /*  INPUT: a message in printf format with its parms                  */
  248. /*                                                                    */
  249. /*  DISPLAY A MESSAGE TO THE USER.                                    */
  250. /*                                                                    */
  251. /*  1. Format the message using vsprintf.                             */
  252. /*  2. Sound a warning sound.                                         */
  253. /*  3. Display the message in a message box.                          */
  254. /*                                                                    */
  255. /*  OUTPUT: nothing                                                   */
  256. /*                                                                    */
  257. /*--------------------------------------------------------------------*/
  258. /**********************************************************************/
  259. VOID DisplayMessage( PSZ szFormat,... )
  260. {
  261.     PSZ     szMsg;
  262.     va_list argptr;
  263.  
  264.     if( (szMsg = (PSZ) malloc( MESSAGE_SIZE )) == NULL )
  265.     {
  266.         DosBeep( 1000, 1000 );
  267.  
  268.         return;
  269.     }
  270.  
  271.     va_start( argptr, szFormat );
  272.  
  273.     vsprintf( szMsg, szFormat, argptr );
  274.  
  275.     va_end( argptr );
  276.  
  277.     szMsg[ MESSAGE_SIZE - 1 ] = 0;
  278.  
  279.     WinAlarm( HWND_DESKTOP, WA_WARNING );
  280.  
  281.     WinMessageBox(  HWND_DESKTOP, HWND_DESKTOP, szMsg,
  282.                     "prnt32.exe", 1, MB_OK | MB_MOVEABLE );
  283.  
  284.     free( szMsg );
  285. }
  286.  
  287.