Export (0) Print
Expand All

SetDefaultPrinter function

The SetDefaultPrinter function sets the printer name of the default printer for the current user on the local computer.

Syntax


BOOL SetDefaultPrinter(
  _In_  LPCTSTR pszPrinter
);

Parameters

pszPrinter [in]

A pointer to a null-terminated string containing the default printer name. For a remote printer connection, the name format is \\server\printername. For a local printer, the name format is printername.

If this parameter is NULL or an empty string, that is, "", SetDefaultPrinter will select a default printer from one of the installed printers. If a default printer already exists, calling SetDefaultPrinter with a NULL or an empty string in this parameter might change the default printer.

Return value

If the function succeeds, the return value is a nonzero value.

If the function fails, the return value is zero.

Remarks

Note  This is a blocking or synchronous function and might not return immediately. How quickly this function returns depends on run-time factors such as network status, print server configuration, and printer driver implementation—factors that are difficult to predict when writing an application. Calling this function from a thread that manages interaction with the user interface could make the application appear to be unresponsive.

To set the default printer, call SetDefaultPrinter. To set the default printer in Windows NT 4.0 and earlier versions of Windows, call GetProfileString , WriteProfileString , and SendNotifyMessage , as shown in the following code:


if ((DWORD)(LOBYTE(LOWORD(GetVersion()))) >= 5) {
    // Call SetDefaultPrinter on Windows 2000 and later versions of Windows 
    SetDefaultPrinter (pszPrinterName);
} else {
    // If this is running on Windows NT4.0 or earlier, then 
    //   read PrinterPorts section from win.ini 
    //   returned string should be of the form "driver,port,timeout,timeout", i.e. "winspool,LPT1:,15,45". 
    GetProfileString(_T("Windows"),_T("Device"),_T(",,,"),m_szName, _countof(m_szName));
    WriteProfileString(_T("Windows"),_T("Device"), pszPrinterName );
    // Notify all open applications of the change. Note, only applications that handle the message will recognize the change. 
    if (WM_WININICHANGE != WM_SETTINGCHANGE) {
        // Old message type; for windows 95 
        SendNotifyMessage(HWND_BROADCAST,WM_WININICHANGE,0,(LPARAM)szWindows);
    }
    // New message type 
    SendNotifyMessage(HWND_BROADCAST,WM_SETTINGCHANGE,0,(LPARAM)szWindows);
}


When using this method, you must specify a valid printer, driver, and port. If they are invalid, the APIs do not fail but the result is not defined. This could cause other programs to set the printer back to the previous valid printer. You can use EnumPrinters to retrieve the printer name, driver name, and port name of all available printers.

Requirements

Minimum supported client

Windows 2000 Professional [desktop apps only]

Minimum supported server

Windows 2000 Server [desktop apps only]

Header

Winspool.h (include Windows.h)

Library

Winspool.lib

Unicode and ANSI names

SetDefaultPrinterW (Unicode) and SetDefaultPrinterA (ANSI)

See also

Printing
Print Spooler API Functions
EnumPrinters
GetDefaultPrinter

 

 

Community Additions

ADD
Show:
© 2014 Microsoft