Using standard printing from windows There are many applications where I do not really want to use the usual windows page print. Instead I want output to go directly to the printer using standard print i / o. It's actually a topic that is hard to find in any of the books on Windows, at least I've never found anything on it. But to my surprise I recently learned that standard (DOS / UNIX) printing is alive and well underneath windows. All we need to do is open a printer port and print to it. If the printer is directly attached to the computer it's trivial. The method for obtaining a printer port when the printer is on the network is not hard either. The example below shows how I use the Windows NET uSE command to re-direct LPT1 to a shared printer on an NT Server. The same technique applies for Novell networks with a slightly different syntax. Try this out by creating a new MFC form-based project. Put a button on the form and attach this code To it. You can actually Print with Only 3 Lines Of code: file * fp = fopen ("lpt1", "w"); fPrintf (fp, "what's up, doc? / n"); fclose (fp); Instant Print Gratification !! While The Program Is Open IT HOGS The Printer Port. in My Shop That Isn't a problem but be aware of the effect on your windows spooled output. ********************************* ***************************** THE CODE ******************* ************************************************ // The Headers for the Convenctional I / O routines # include #include #include using namespace std;
// makes string and ofstream // work without std :: qualifiervoid CLineprtView :: OnButton1 () {// I could have used a CString instead of the buff [] // but I wanted to show how this is used with lightweight // ATL code and STD library char buff [MAX_BUFF_SIZE]; // My printer is located on another server so I must re-direct the // printer port If the printer is directly attached this extra step // is not needed // on.. My Network The Printer Is Published AS // Green / HP5annex // All Those Back-Slashes Escape The Backslash In The Path Name if (Printer_Is_Remote) {System ("Net Use LPT1 / D"); // Free Up The Port System "NET USE LPT1 Green // HP5annex");} // old fashioned file handle with // old fashioned open of the print port file * ptr = fopen ("lpt1", "w"); // Laser Printer Setup Spring Sprintf (BUFF, "/ 033E / 033 (S0P4102T1B16.66H / 033 & L1O"); FPRINTF (PTR, BUFF); // Old Fashioned Pr Int fprintf (PTR, "WHO of Late Doth make a think./N"); FPRINTF (PTR, "IS A LOWER BUNK A Status Symbol ?? / n"); // Old Fashied Close Fclose (PTR); // Now The Same Thing With Stream Io OFStream Optr ("LPT1", IOS :: Out); string str_text = "Hey Doc, ain't this a print test from windows / n"; str_text = "with more lines to follow? / N "; OPTR << Str_Text << Endl; Optr <<" Quiet, Wabbit. I'm conversing with my muse !! / n "; optr <<" That's all folks. "<<" / f "<< Flush;