How do you set the default printer programatically?

I want to set the default printer from my Win32 program.
The program should primarily run under Windows NT, but
it would be nice if it'd work under Windows 95/98 too.

Who is Participating?
chensuConnect With a Mentor Commented:
The KB article does apply to Windows NT. Read it carefully please.

"This is the preferred method of changing the printer if the code is to be platform independent; this method will work on Windows 3.1, Windows 95 and Windows NT."

Thank you and also agreen.
How To Get and Set the Default Printer in Windows

How to Set the Default Printer Programmatically in Windows 95

dananderssonAuthor Commented:
The primary platform I want to set the default printer is Windows NT. The given ansver from chensu did cover only 16-bit
Windows and Windows 95.
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.

In ALL versions of Windows, the appropriate way to get the default printer is to use GetProfileString, and the appropriate way to set the default printer is to use WriteProfileString.

The "HOWTO: Get and Set the Default Printer in Windows" applies to Win32 API which included with both NT and Win95.

So the chensu's answer is right.

I've already answered such question.
// To set the default printer for Windows NT use:
/* Note printer driver is usually WINSPOOL under Windows NT */

WriteProfileString("windows", "device", "My Printer, WINSPOOL,   "lpt1:");

   SMTO_NORMAL, 1000, NULL);
dananderssonAuthor Commented:
Thank you! Now I'm confident that this solves my problems :-)
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.

All Courses

From novice to tech pro — start learning today.