Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to print to printer?

Posted on 2006-06-22
17
Medium Priority
?
993 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

618 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