Solved

Printing to a user specified windows printer from DOS

Posted on 2004-10-25
281 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
Question by:SterlingMcClung
    15 Comments
     
    LVL 7

    Author Comment

    by:SterlingMcClung
    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:
    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
    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
     
    LVL 86

    Expert Comment

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

    Author Comment

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

    Author Comment

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

    Author Comment

    by:SterlingMcClung
    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
    Can you check the DEVMODE* and the memory it is pointing to using a debugger?
    0
     
    LVL 7

    Author Comment

    by:SterlingMcClung
    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
    Oops, you are on the right track, but, my fault, if should be the DEVNAMEs*
    0
     
    LVL 7

    Author Comment

    by:SterlingMcClung
    0x3d25d0
    0
     
    LVL 7

    Author Comment

    by:SterlingMcClung
    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
    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
    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
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
    This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
    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…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    913 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now