Solved

How to print to printer?

Posted on 2006-06-22
17
987 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

770 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