Solved

How to print to printer?

Posted on 2006-06-22
17
989 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

733 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