Solved

How to print to printer?

Posted on 2006-06-22
17
983 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

707 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

11 Experts available now in Live!

Get 1:1 Help Now