Solved

printDlg() to get printer name

Posted on 2004-10-26
881 Views
Last Modified: 2012-05-05
I am trying to use the printerDlg() function to get a windows printer name.  Here is my code:

#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 trying to right a console app that will let a user print a pre-formatted, postscript, file to any windows printer he likes.  Once I have the printer name I can send the raw data to the printer as discussed in:

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

I have tested this second portion out and it works great for us, but I would like the print properties page to come up.

OK The real problem is that my code above just give me the letter "P" in my acPrinterName.  I would like to get an anwser to this as quickly as I can.
0
Question by:SterlingMcClung
    3 Comments
     
    LVL 7

    Author Comment

    by:SterlingMcClung
    Some more info.  If I run this program on a Win98 machine it does return at least a portion of the printer name.  It seems the offset is not correct as I miss the first 11 characters.  On Win2kP I got nothing, even if I make the name really long.  I am trying to use this in Windows Server 2003.

    Thanx in advance
    0
     
    LVL 12

    Accepted Solution

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


    using namespace std;
    //by Onega(www.fruitfruit.com)
    //windows 2000 SP4
    //Dev-C++ 4.9.90
    //don't forget to check error!
    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);
       LPDEVMODE lpDevMode = (LPDEVMODE)::GlobalLock(printDlg.hDevMode ) ;
       LPDEVNAMES lpDevNames = (LPDEVNAMES)::GlobalLock( printDlg.hDevNames ) ;
       pdn = (DEVNAMES*) printDlg.hDevNames;

       acPrinterName=(LPCTSTR)printDlg.hDevNames+pdn->wDeviceOffset;
       TCHAR printnamebuf[128];
       memset(printnamebuf,0,sizeof(printnamebuf));
       memcpy(printnamebuf,lpDevMode->dmDeviceName, CCHDEVICENAME);
        if ( lpDevMode )
        {
        ::GlobalUnlock( printDlg.hDevMode ) ;
        }
        if ( lpDevNames )
        {
        ::GlobalUnlock( printDlg.hDevNames ) ;
        }
       cout << printnamebuf << endl;

       system("Pause");  

       return 0;  
    }
    0
     
    LVL 7

    Author Comment

    by:SterlingMcClung
    Great, that works!  Can you explain it a little.  It seems very similar to what I had, but I don't understand some of what you did.

    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Anonabox PRO Tor & VPN Router

    PRO is the most advanced way to fortify your privacy and online anonymity by layering the Tor network with VPN services. Use both together or separately, and without needing to download software onto your devices.

    Some Windows API functions expect you to provide a pointer to a CALLBACK function that the system will need to call as part of the operation.  Such API functions as SetTimer, timeSetEvent, CreateThread, EnumWindows, LineDDA, even window message hand…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    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 learn how to clear a vector as well as how to detect empty vectors in C++.

    857 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

    18 Experts available now in Live!

    Get 1:1 Help Now