[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 291
  • Last Modified:

Printing to a user specified windows printer from DOS

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
SterlingMcClung
Asked:
SterlingMcClung
  • 10
  • 5
1 Solution
 
SterlingMcClungAuthor Commented:
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
 
jkrCommented:
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
 
SterlingMcClungAuthor Commented:
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
[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

 
jkrCommented:
Not an include file, but you should link with commdlg32.lib
0
 
SterlingMcClungAuthor Commented:
I am using Dev-C++ and can't find that lib.  Any ideas?
0
 
SterlingMcClungAuthor Commented:
Just found it.  It is libcomdlg32.a on mine.  Thanx a lot.
0
 
SterlingMcClungAuthor Commented:
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
 
jkrCommented:
Can you check the DEVMODE* and the memory it is pointing to using a debugger?
0
 
SterlingMcClungAuthor Commented:
I don't really know how to do this.  hDevMode value of printDlg in debug is 0x0  Is that what you need?
0
 
jkrCommented:
Oops, you are on the right track, but, my fault, if should be the DEVNAMEs*
0
 
SterlingMcClungAuthor Commented:
0x3d25d0
0
 
SterlingMcClungAuthor Commented:
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
 
SterlingMcClungAuthor Commented:
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
 
SterlingMcClungAuthor Commented:
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
 
jkrCommented:
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 10
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now