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.
LVL 7
SterlingMcClungAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.