Printing!

This is a discussion on Printing! within the C++ Programming forums, part of the General Programming Boards category; I tried this, and it didn't work. I also tried the C version, and I also tried "LPT1" instead of ...

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    891

    Printing!

    I tried this, and it didn't work. I also tried the C version, and I also tried "LPT1" instead of "PRN". But I cannot get anything to print! Maybe my drivers don't like console apps. Anyway, I'm in Windows98 with a BJC-3000 printer connected via LPT port. Here's the code:

    Code:
    int main(int argc, char* argv[])
    {
    	ofstream prnt("PRN");
    
    	prnt << "\r HELLO FRIEND!" << '\f';
    
    	prnt.close();
    	
    	return 0;
    }

  2. #2
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    cool same printer as me!
    Please direct all complaints regarding this post to the nearest brick wall Have a nice day.

  3. #3
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    I really hope somebody answers this question because it have been asked many times, and nobody answered it.
    none...

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    You have to use windows drivers!

    There are two problems...

    These Canon printers will only work with their windows drivers. They don't work with DOS. (This means that you can't send them straight-ASCII.)

    Even when I had a printer that would print from DOS, if I compiled the program as a console application, it wouldn't print.

    So, you'll have to include windows.h and dig-up the prihter command. (I don't have Petzold's book with me.)

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    console applications are really restricted windows programs simulating a DOS environment and not true DOS programs. (if you don't have a black background with white letters, you probably aren't programming in DOS (of course there are exceptions to that generalization). Thererfore, most (all?) compilers require console programs to use windows functions to print. If your compiler has the option of compiling as either DOS, console, Windows, or command line; choose DOS or command line. If you are using a console app and want to print directly, I wouldtry including windows.h and go from there . Alternatively, print to a text file and print from NotePad/WordPad/whatever, which is what I generally do.

  6. #6
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    891
    Thank you, and you're right, but that just isn't enough. Is there any way to just communicate to a printer with DC's?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    sorry, I haven't gotten that far in my study of Windows API. There must be a way to do it. Maybe you can ask on the Windows board.

  8. #8
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    See...
    This is one of the "no answer" queston, I saw this question many times and no body ever answered it!
    I think somebody should give the answer, and maybe it can be added to the FAQ's, because it's really very common.
    none...

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    The problem as I see it is that C++ does provide for direct printing, but only in DOS/lowest level code available in C/C++. When done in Windows, even if through a console "Window", then you need to use Windows API, and if through an Apple console window , then you need to use Apple API, and if through a Linux console window, then you need Linus API, etc. May not be true, but it fits the data/experience I have.

  10. #10
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786

    Red face :o Yawn

    Originally posted by ammar
    See...
    This is one of the "no answer" queston, I saw this question many times and no body ever answered it!
    I think somebody should give the answer, and maybe it can be added to the FAQ's, because it's really very common.
    You still cant find the answer!!?!! This has been answered so many times...do a search and you will find all sorts of info......but as its the season of goodwill....

    CodeMonkey's example works fine for me on WinXP with a battered old cannon printer on LPT1........if you have a USB printern then I dont think it will work.....

    There are various ways to do this, but if you are on windows then its best to do it properly and use the API (remember that these days there are often more than 1 printer and not all printers are on COM1 or LPT1 - there are network printers, Faxs...etc)......this code has a function that recieves a string and then opens the print dialog (that you all know) to chose which printer to send to (it can be USB...or even a fax...etc)....

    Code:
    #include <windows.h>
    #include <commdlg.h>
    #include <string>
    #include <iostream>
    //You must include comdlg32.lib for this to work
    
    bool PrintIt(const std::string& str){
    
    	PRINTDLG  pd = {0};
    	pd.lStructSize = sizeof(PRINTDLG);
    	pd.hwndOwner = HWND_DESKTOP;
    	pd.Flags = PD_RETURNDC;
    	pd.nCopies = 1;
    	DOCINFO di = {sizeof(DOCINFO),"Printed by C++!",0};
    
    	if(!PrintDlg(&pd))
    		return false;
    
    	if(StartDoc(pd.hDC,&di)>0 && StartPage(pd.hDC)>0){
    		TextOut(pd.hDC,10,10,str.c_str(),str.length());
    		EndPage(pd.hDC);
    		EndDoc(pd.hDC); 
    	}
    	DeleteDC(pd.hDC);
    
    	return true;
    }
    
    
    int main(int argc, char* argv[])
    {
    	if(PrintIt("Hello World"))
    		std::cout << "Printed!";
    		else std::cout << "Not Printed";
    	
        return 0;
    }

  11. #11
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    me thinks faq this should be placed in
    Please direct all complaints regarding this post to the nearest brick wall Have a nice day.

  12. #12
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    891
    Spank you, Fordy.

  13. #13
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    891
    Might we be explaining what this means? I'm curious:
    Code:
    const std::string& str
    std string? Is that a special type, lol?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  14. #14
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Originally posted by CodeMonkey
    Might we be explaining what this means? I'm curious:
    Code:
    const std::string& str
    std string? Is that a special type, lol?
    the string class from the Standard C++ Library is part of namespace std (as are most types in that lib)

    Oh...and you try spank me and I might be forced to kill you

  15. #15
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Hopefully:

    codeMonkey's code will work in DOS mode using C++, but it won't work in console mode.

    vVv's code will work in DOS mode using C or C++, but not in console mode.

    Fordy's code will work in DOS or console mode.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 01:13 AM
  2. generic printing preferences dialog box
    By stanlvw in forum Windows Programming
    Replies: 8
    Last Post: 06-27-2008, 02:20 AM
  3. printing data to a file
    By coralreef in forum C Programming
    Replies: 3
    Last Post: 11-02-2006, 07:10 PM
  4. need help relating printing?
    By omarlodhi in forum Linux Programming
    Replies: 0
    Last Post: 03-03-2006, 03:46 AM
  5. Printing
    By Dual-Catfish in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 05-25-2002, 08:10 PM

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