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

x
?
Solved

Printing to a user specified windows printer from DOS

Posted on 2004-10-25
15
Medium Priority
?
290 Views
Last Modified: 2013-12-03
I would like to be able to call a program, either one that I write or a dll, that takes a file, displays the print dialog box to allow the user to choose any windows printer installed, and then print the file.  This file is already formated(I could just do:type filename>lpt1 and it would print, but I want the user to be able to choose the printer).  I have done a little C++, but nothing in windows, just unix.

Thanx
0
Comment
Question by:SterlingMcClung
[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
  • 10
  • 5
15 Comments
 
LVL 7

Author Comment

by:SterlingMcClung
ID: 12401039
I have found a previous question that was similar to mine and the answer was a link to the following:

http://support.microsoft.com/kb/q138594/

The only problem with this is that it does not ask the user for the printer.
0
 
LVL 86

Accepted Solution

by:
jkr earned 1500 total points
ID: 12402151
You could use 'PrintDlg()' to have the user select a printer, e.g.

   char acPrinterName [ 255];
   DEVNAMES* pdn;
   
   // Create a PRINTDLG structure, and initialize the appropriate fields.
   PRINTDLG printDlg;
   ZeroMemory(&printDlg, sizeof(printDlg));
   printDlg.lStructSize = sizeof(printDlg);
   
   // Display a print dialog box.
   PrintDlg(&printDlg);

   pdn = (DEVNAMES*) printDlg.hDevNames;

   strcpy(acPrinterName,(char*)printDlg.hDevNames + pdn->wDeviceOffset));

Now, 'acPrinterName' will hold the name of the printer that you can use when calling 'OpenPrinter()'
0
 
LVL 7

Author Comment

by:SterlingMcClung
ID: 12404193
I am finally getting to test this.  I keep getting an error I don't understand:

[Linker error] undefined reference to 'PrintDlgA@4'

Am I missing an include??
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 86

Expert Comment

by:jkr
ID: 12405394
Not an include file, but you should link with commdlg32.lib
0
 
LVL 7

Author Comment

by:SterlingMcClung
ID: 12405500
I am using Dev-C++ and can't find that lib.  Any ideas?
0
 
LVL 7

Author Comment

by:SterlingMcClung
ID: 12405537
Just found it.  It is libcomdlg32.a on mine.  Thanx a lot.
0
 
LVL 7

Author Comment

by:SterlingMcClung
ID: 12405587
I am still having troubles.  Everything compiles now, but acPrinterName is just full of 'P's.  I am just trying to cout the char* after your code.
0
 
LVL 86

Expert Comment

by:jkr
ID: 12406399
Can you check the DEVMODE* and the memory it is pointing to using a debugger?
0
 
LVL 7

Author Comment

by:SterlingMcClung
ID: 12406569
I don't really know how to do this.  hDevMode value of printDlg in debug is 0x0  Is that what you need?
0
 
LVL 86

Expert Comment

by:jkr
ID: 12406585
Oops, you are on the right track, but, my fault, if should be the DEVNAMEs*
0
 
LVL 7

Author Comment

by:SterlingMcClung
ID: 12406607
0x3d25d0
0
 
LVL 7

Author Comment

by:SterlingMcClung
ID: 12406664
I also have this:
{68 'D', 69 'E', 86 'V', 78 'N', 65'A', 77 'M', 69 'E', 83 'S', 42 '*', 0 '\0'} = "DEVNAMES*"
0
 
LVL 7

Author Comment

by:SterlingMcClung
ID: 12406682
I am also getting a huge number of copies: 15196.

I really don't understand most of this... Hope I am able to give you the information you need.
0
 
LVL 7

Author Comment

by:SterlingMcClung
ID: 12415776
Is there anything else I can try to get this working?

Since I am running win server 2003 I have tried using printDlgEx() and changing to PRINTDLGEX, but I keep getting an error saying that PRINTDLGEX and printDlgEx() are undefined, even thought they are supposed to be in the same header files.

Here is my code as it stands now, I have made a couple changes to it:

#include <windows.h>
#include <iostream>


using namespace std;

int main()
{
   LPCTSTR acPrinterName;
   acPrinterName = "Testing";
   DEVNAMES* pdn;
   cout << acPrinterName << endl;
   // Create a PRINTDLG structure, and initialize the appropriate fields.
   PRINTDLG printDlg;
   ZeroMemory(&printDlg, sizeof(printDlg));
   printDlg.lStructSize = sizeof(printDlg);
   printDlg.hDevNames = NULL;
   
   // Display a print dialog box.
   PrintDlg(&printDlg);

   pdn = (DEVNAMES*) printDlg.hDevNames;

   acPrinterName=(LPCTSTR)printDlg.hDevNames + pdn->wDeviceOffset;
   
   cout << acPrinterName << endl;

   system("Pause");  

   return 0;  
}

I am not getting a single "P" in my acPrinterName no matter what printer I have chosen.  I have tried printers with short names as well as ones with long ones.  No difference.  

pdn is pointing to {112 'p', 100 'd', 110 'n', 0 '\0'}

printDlg:
   hDevMode = 0xaa001c
   hDevNames = 0xaa0024

I think that I am going to start a new question since, from what I can tell from the M$ site, this should work.  I am going to start it in C++ instead of Windows.
0
 
LVL 86

Expert Comment

by:jkr
ID: 12417407
You shouldn't have accepted an answer at this point - I was testeing it at the office today, and being busy, I forgot that I did not get any further so far (even though that is supposed t work).
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

604 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