Get Default Printer Name
(works in: Microsoft Windows 95/98/ME/NT/2000/XP)

It is sometimes necessary to fetch the name of the default printer (queue) name for Windows from within a program. The Win32 API provides the function GetDefaultPrinter for this, but unfortunately this function only works in Win/2000 or later.

The routine GET_DEF_PRINTER routine lets you get the default printer name for any version of Windows after (and including) Windows/95.

The routine is available as part of the C++ source file EXAM41.CPP. The routine has the following calling parameters:

        int GET_DEF_PRINTER(char *,int *)
where a character buffer is supplied to receive the default printer name, as well as a pointer to an integer that contains the max size of the character buffer. The second parameter (the max buffer size) is modified by the routine to contain the actual length of the printer name (in bytes). The default printer name is returned as a zero-terminated string.

The return value is zero if the printer name was retrieved successfully. See the comments in the source code for other possible return codes.

Note that this routine modifies the values for both the character string and the integer length.

You can download the file in ZIP format by clicking on the following link.

This works for Microsoft Windows 95/98/ME/NT/2000/XP. I have tested with Visual C++ v6.0 but it should work with other versions also. Please let me know of exceptions.

After downloading, unzip the file Exam41.cpp into any folder. It contains a short Windows console program which demonstrates how to call GET_DEF_PRINTER to find the default printer name.

If you get build time errors like "GetDefaultPrinter is undefined..." then you need to get a more recent version of the Windows SDK. You can get it from:

http://www.microsoft.com/msdownload/platformsdk/sdkupdate/

though not recommended over slow (i.e. modem) lines because it is a very large download.

If you have any questions please send them to the e-mail address below.

Further Reading:

Disclaimer:
This site contains some software that I've written over the years. It is believed to work, but there is no warranty. Please use at your own risk. If you notice any bugs please let me know by email at kochhar@physiology.wisc.edu.

Return to Ravi's Free Software Page
Return to Ravi's Home Page
Back to The Basement
This page last modified on : Aug. 28, 2003