Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

printDlg() to get printer name

Posted on 2004-10-26
3
Medium Priority
?
987 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
Comment
Question by:SterlingMcClung
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 7

Author Comment

by:SterlingMcClung
ID: 12416667
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:
OnegaZhang earned 1500 total points
ID: 12417127
#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
ID: 12417421
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

610 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