• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1018
  • Last Modified:

printDlg() to get printer name

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
SterlingMcClung
Asked:
SterlingMcClung
  • 2
1 Solution
 
SterlingMcClungAuthor Commented:
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
 
OnegaZhangCommented:
#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
 
SterlingMcClungAuthor Commented:
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

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

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