Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting the Default Printer in WinXP 1

Status
Not open for further replies.

Teric

Programmer
Aug 12, 2003
4
US
I have found a number of articles explaining how to get the name of the default printer in Win95, Win98, WinNT, and Win2000. However, I have found nothing about how to get the name of the default printer in WinXP.

The method for Win95/98 is to use EnumPrinters. I can use EnumPrinters in WinXP, but I get an array of possible printers as a result--how can I tell which one is the default printer?

Win2000 has a convenient function GetDefaultPrinter, which WinXP does not have. :(

Any suggestions? Thanks in advance!
 
>> Win2000 has a convenient function GetDefaultPrinter,
>> which WinXP does not have. :(

That does not appear to be accurate, From the Platform SDK : GDI
Code:
BOOL GetDefaultPrinter(
  LPTSTR pszBuffer,   // printer name buffer
  LPDWORD pcchBuffer  // size of name buffer
);

...<snip>...

Requirements
Windows NT/2000/XP: Included in Windows 2000 and later.
Windows 95/98/Me: Unsupported.
Header: Declared in Winspool.h; include Windows.h.
Library: Use Winspool.lib.
Unicode: Implemented as Unicode and ANSI versions.


-pete
 
Unfortunately, I've tried that. I use the following code:

DWORD BytesCopied; //Number of bytes returned
char PrinterName[80]; //Name of the printer

if(!GetDefaultPrinter(PrinterName, &BytesCopied))
return false;

I've included Winspool.lib in my project settings, I've even explicitly #included <Windows.h> and <Winspool.h> in my source file. It doesn't seem to matter; I always get this error when I compile:

error C2065: 'GetDefaultPrinter' : undeclared identifier

I appreciate your help, though! Any other ideas?
 
If your version of Winspool.h does not contain the declaration of GetDefaultPrinter then you need to download the latest Platform SDK from msdn.microsoft.com

-pete
 
Hey, thanks! Didn't know about the Platform SDK update. I downloaded it and installed it.

Now my code compiles just fine, but when I run it, the GetDefaultPrinter() function always returns ERROR_NOT_ENOUGH_MEMORY. This can't be right, because my machine has a gigabyte of RAM, and it's just trying to get the name of the default printer. Have I set up the parameters wrong? Here's the code I'm using:

DWORD BytesCopied; //Number of bytes returned
char PrinterName[MAX_PATH] //Name of the printer

//Figure out the name of the default printer
if(!GetDefaultPrinter(PrinterName, &BytesCopied))
{
//Couldn't get the name of the default printer.
return false;
}

Thanks again for your help!
 
Code:
char printname[MAX_PATH + 1];
DWORD dw = MAX_PATH;
if( !::GetDefaultPrinter( printname, &dw))
	TRACE(&quot;FAILED GetDefaultPrinter\r\n&quot;);
else
	TRACE(&quot;DEFAULTPRINTER: %s\r\n&quot;, printname);

-pete
 
Ha! That worked! Was the problem that I needed to initialize my BytesCopied variable?

At any rate, thank you very much, palbano. You've been most helpful.
 
>> Was the problem that I needed to initialize my BytesCopied variable?

Yes it was.

&quot;Initialize always your variables. Otherwise conditions unexplained you may have.&quot; [yoda]

-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top