printer

This is a discussion on printer within the C++ Programming forums, part of the General Programming Boards category; I know that this has been addressed many times on the boards but I have a new question that I ...

  1. #1
    Master of Puppets rwmarsh's Avatar
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    96

    printer

    I know that this has been addressed many times on the boards but I have a new question that I have not been able to find an answer for anywhere.

    I am creating a program that needs to send simple text, mostly C++ style string variables and some string literals, to the printer to create a hard copy. Using this simple example works:
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
        ofstream printer("PRN");
    
        if ( printer.fail() ) 
            cout<<"\aPrinter NOT Open!!!";
        else
        {
            cout<<"Printer Opened"<<endl;
            printer<<"This is a test string.\f";
            printer.flush();
        }
        printer.close();
    
        cout<<"Test string sent to printer, hit <enter>"<<endl;
        cin.get();
    
        return 0;
    }
    The problem I am having is that it works only on my machine running windows 98 using a plain Dot Matrix Printer. It does NOT work on my machine running XP with an HP Deskjet. I though at first that it might have something to do with the deskjet printer verses the DMP but after googleing around it seems that windows XP does not allow applications to directly access a printer port, while 98 will. It seems that on XP you have to go through the printer driver.

    My question is this, Is there a windows API or maybe a thrid party library function that I can download that will send text through the printer driver? Or maybe someone can explain how to program one myself or give a URL that explains it.

    I really need this to work on both windows 98 and XP so the above program will not work

    Thanks a Lot for any help, it is greatly appreciated.
    Using DEV-C++ Under Windows XP
    +------------------------------+

    "No! Do, or Do Not. There is no Try..."

  2. #2
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    ...something to do with the deskjet printer verses the DMP....that windows XP does not allow applications to directly access a printer port...
    Both may be true. If you send the (old?) dot matrix printer an ASCII 'A', it will print an A. Some new printers don't understand plain ASCII and you have to use a Windows driver. I found this out when I tried to use a Canon inkjet printer with a DOS program.

    I thought there was a way to access the printer port by opening it as a file... but I dunno... I know of a program called Port Talk that might work for you.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    You can open "LPT1" (or "LPT2", or whatever) with an fstream, however, this won't solve the problem of trying to print to an HP deskjet which doesn't accept plain ASCII. I believe that, for their own reasons, modern printers deliberately don't allow this, and will either print junk, or do nothing. (Which, as you say, is a bit of a pain if you want to print from some old 16-bit DOS software)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Set Printer Prior To Loading Print Dialog Box
    By Beaner in forum Windows Programming
    Replies: 3
    Last Post: 10-10-2008, 02:02 PM
  2. changing property (layout) of a printer
    By leojose in forum Windows Programming
    Replies: 5
    Last Post: 12-05-2005, 07:16 AM
  3. printer gone crazy
    By DavidP in forum Tech Board
    Replies: 2
    Last Post: 03-29-2004, 01:16 AM
  4. Success - Output to Printer
    By noraa in forum C Programming
    Replies: 5
    Last Post: 08-04-2002, 10:12 AM
  5. Replies: 0
    Last Post: 04-30-2002, 08:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21