0

I am trying to crate an application for Microsoft Windows in C/C++. The goal of the application is to take a text file as input, parse the text file and generate the output in form of an HTML file.

Then it prints the generated HTML file using a printer attached to the computer.

Everything is simple but printing. The problem is that I don't know how to control printer to do the printing job, whether it's an HTML document or any other.

I searched the forum and internet for a solution but here are my problems:

  • I don't know much about Windows programming since I come from Linux background.
  • I have never used Visual C++ compiler so I had a little trouble understanding how things were going with it. (I have only used GCC/G++).

What I would like to know is whether there is any API available that I can use directly with my MinGW compiler. Or if there is any other method I could learn how to do this using Windows API supplied with Visual C++ compiler (which I suppose comes with Visual Studios in Windows).

Edit: I was also wondering if there was any way to print the file directly to the printer avoiding the popups every time a printing job is done. The application I am working on does a lot of printing jobs, so if it was possible to avoid those popping dialogs which ask user to select a printing device by automatically checking for available printing devices and choosing one as default.

Thank you.

Mat
  • 202,337
  • 40
  • 393
  • 406
Chetan Bhasin
  • 3,503
  • 1
  • 23
  • 36
  • 1
    AS you, I don't know Windows (but only Linux). Can't you find a printing command ([`PRINT`](http://en.wikipedia.org/wiki/PRINT_%28command%29) perhaps) and start it (with `system` or `popen`)? – Basile Starynkevitch Jan 11 '14 at 08:37
  • @BasileStarynkevitch Well! That's a good idea. I'll try that for sure. But I think it's still a good idea to know the logic in order to avoid coming popups and make things easy for the user. – Chetan Bhasin Jan 11 '14 at 08:40
  • There are a few open source libraries for html-> pdf -> itextSharp. Also you can use NPOI(OPEN SOURCE) to do the same with excel. – Ross Bush Jan 11 '14 at 08:43
  • Google gave me the following options: http://www.bing.com/search?setmkt=en-GB&q=windows+api+print, http://msdn.microsoft.com/en-us/library/windows/desktop/dd162861(v=vs.85).aspx – rhughes Jan 11 '14 at 08:49
  • @lrb Never heard of such things, but I'll surely give them a try. – Chetan Bhasin Jan 14 '14 at 10:27
  • @rhughes Isn't Print Spooler API a part of .NET? – Chetan Bhasin Jan 14 '14 at 10:28

1 Answers1

1

You could print using the Win32 shell ShellExecute. There's another question here, where the shell associations are described, in the approved answer, as a "terrible" approach (I don't quite agree, it's the user that chooses the associations), but the answer also has another command to be run, maybe also with ShellExecute. I could provide some code, but there's nothing to be afraid of or very difficult with this solution.

Edit to edit A solution using GDI (skipping the shell): How To: Print Using the GDI Print API, also find some question with some code, but I think I'm as good as anybody else to searching :).

Community
  • 1
  • 1
Liviu
  • 1,859
  • 2
  • 22
  • 48
  • The "other command" was pretty helpful. But I was wondering if there must be some way for us to be able to avoid the popup messages and print the file directly by detecting a physical printer. – Chetan Bhasin Jan 11 '14 at 12:28
  • Find some links, but I never done it this way (without ShellExecute). – Liviu Jan 11 '14 at 12:42
  • I'll see if I can find something. Otherwise, PrintHTML and ShellExecute seem like the only alternatives. Thank you, by the way. – Chetan Bhasin Jan 14 '14 at 10:31