Solved

How to print to printer?

Posted on 2006-06-22
17
984 Views
Last Modified: 2012-06-21
Hello,

I want to know how to print via a printer using C++ (not VC).
I have some data on my screen and want to print them to a printer.

Please any ideas, links to understand this task.

Thanks,
0
Comment
Question by:amrelgarhy81
  • 11
  • 6
17 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 16958725
Hi amrelgarhy81,
> I want to know how to print via a printer using C++ (not VC).
Send your contents to the LPT1: device.

Cheers!
0
 
LVL 1

Author Comment

by:amrelgarhy81
ID: 16958742
How?
Wana see samples or articles to see because i didn't that before.

Thanks,
0
 
LVL 30

Expert Comment

by:Axter
ID: 16958839
>>How?

The same way you open any file.

Either use fstream or use fopen to open the device.  Then write to the device as you would write to a file.
0
 
LVL 30

Expert Comment

by:Axter
ID: 16958846
Is you're printer connected to your computer through the parallel port?
0
 
LVL 1

Author Comment

by:amrelgarhy81
ID: 16958868
mmm
Connected through a usp.
Want to see samples plz.
0
 
LVL 30

Expert Comment

by:Axter
ID: 16958922
>>Want to see samples plz.

I believe you mean USB, and if you're connected via USB, I don't think printing to the LPT1 port will work.

You can first try it on the command line using the following method:

copy somefile.txt LPT1:

If you can get it working using above command line, then I'll post an example.

There's no use posting an example, if it's not going to work for your requiremnts.

When you try it out, make sure you're copy an existing text file.
0
 
LVL 1

Author Comment

by:amrelgarhy81
ID: 16958950
I want to let you know that i will make that application for a buyer so i donn't know how he will use and with which printer.
So i just want to know:
We can print to a printer using C++? and how?
0
 
LVL 30

Expert Comment

by:Axter
ID: 16958984
>>We can print to a printer using C++? and how?

You can print to a printer using C++ if the printer is connected to a printer port.

You can NOT print to a printer using standard C++ if the printer is connect to a USB, UNLESS the printer or other third party software has a driver to redirect LPT1 to the USB device.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 30

Expert Comment

by:Axter
ID: 16959024
>>I want to let you know that i will make that application for a buyer so i donn't know how he will use and with which printer.

If this is for a client, then you don't have any way of doing this with just ANSI C++, that would be reliable, without knowning what hardware your customer has.
0
 
LVL 30

Accepted Solution

by:
Axter earned 50 total points
ID: 16959081
You can print to a USB printer by using Windows API functions.
Example code:

BOOL RawDataToPrinter(LPSTR szPrinterName, LPBYTE lpData, DWORD dwCount)
   {
     HANDLE     hPrinter;
     DOC_INFO_1 DocInfo;
     DWORD      dwJob;
     DWORD      dwBytesWritten;

     // Need a handle to the printer.
     if( ! OpenPrinter( szPrinterName, &hPrinter, NULL ) )
       return FALSE;

     // Fill in the structure with info about this "document."
     DocInfo.pDocName = "My Document";
     DocInfo.pOutputFile = NULL;
     DocInfo.pDatatype = "RAW";
     // Inform the spooler the document is beginning.
     if( (dwJob = StartDocPrinter( hPrinter, 1, (LPSTR)&DocInfo )) == 0 )
     {
       ClosePrinter( hPrinter );
       return FALSE;
     }
     // Start a page.
     if( ! StartPagePrinter( hPrinter ) )
     {
       EndDocPrinter( hPrinter );
       ClosePrinter( hPrinter );
       return FALSE;
     }
     // Send the data to the printer.
     if( ! WritePrinter( hPrinter, lpData, dwCount, &dwBytesWritten ) )
     {
       EndPagePrinter( hPrinter );
       EndDocPrinter( hPrinter );
       ClosePrinter( hPrinter );
       return FALSE;
     }
     // End the page.
     if( ! EndPagePrinter( hPrinter ) )
     {
       EndDocPrinter( hPrinter );
       ClosePrinter( hPrinter );
       return FALSE;
     }
     // Inform the spooler that the document is ending.
     if( ! EndDocPrinter( hPrinter ) )
     {
       ClosePrinter( hPrinter );
       return FALSE;
     }
     // Tidy up the printer handle.
     ClosePrinter( hPrinter );
     // Check to see if correct number of bytes were written.
     if( dwBytesWritten != dwCount )
       return FALSE;
     return TRUE;
   }
0
 
LVL 30

Expert Comment

by:Axter
ID: 16959095
If you don't know the name of your printer, you can pass a NULL value to the above RawDataToPrinter function as the first argument.
That should cause OpenPrinter to open the first local printer available.
0
 
LVL 30

Expert Comment

by:Axter
ID: 16959099
The above method should work for any type of standard printer connected to the machine, regardless if it's connected using USB or parallel printer port.

It doesn't require VC.
Just need to include <windows.h> and <Winspool.h>

You'll also need to add Winspool.lib to your project.
0
 
LVL 1

Author Comment

by:amrelgarhy81
ID: 16959128
Thank you very much for your great help.
You gave me all what i want.
Just 2 small questions:
1- If you have a site that talk about printing in C++, can i know it?
2- If iam using VC it will be easier to print? i mean like vb6 printing style?

Thanks,
0
 
LVL 30

Expert Comment

by:Axter
ID: 16959301
>>If iam using VC it will be easier to print? i mean like vb6 printing style?

If you're using MFC with VC, and you're familiar with MFC, then it would be easier.

Otherewise, it would not be easier to code.


>>1- If you have a site that talk about printing in C++, can i know it?
If you're referring to printing using ANSI C++, no, I don't have a site for that.
0
 
LVL 1

Author Comment

by:amrelgarhy81
ID: 16959333
Thanks sir for help.
0
 
LVL 30

Expert Comment

by:Axter
ID: 16959357
0
 
LVL 1

Author Comment

by:amrelgarhy81
ID: 16959382
Thanks.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

1 Experts available now in Live!

Get 1:1 Help Now