Programatically change windows default printer 
Author Message
 Programatically change windows default printer

Howdy,

I need to write a little systray app that will the user change the default
printer for Windows 98 or Windows 2000.  I know how to enumerate the
available printers, but can anyone pass me a pointer to how to designate the
default printer from within an application?

thanks

Jim Howard



Mon, 19 Jul 2004 06:45:16 GMT  
 Programatically change windows default printer
SWAG:

SetPrinter(...) using a PRINTER_INFO_2 structure and setting Attributes to
PRINTER_ATTRIBUTE_DEFAULT.  This attribute specifies "Windows 95" after it,
but it might be worth a try.

TFM3

Note: Spam-resistant e-mail address


Quote:
> Howdy,

> I need to write a little systray app that will the user change the default
> printer for Windows 98 or Windows 2000.  I know how to enumerate the
> available printers, but can anyone pass me a pointer to how to designate
the
> default printer from within an application?

> thanks

> Jim Howard



Mon, 19 Jul 2004 07:38:46 GMT  
 Programatically change windows default printer
Hi Jim,

here we go...

this is an old code from the MSDN that has been growing

slowly... its quite well documented, so you can change it

to suit your needs.

enjoy,
/***************************************************************************
******/
BOOL DPSetDefaultPrinter( LPTSTR pPrinterName )
{
  BOOL bFlag;
  OSVERSIONINFO osv;
  DWORD dwNeeded = 0;
  HANDLE hPrinter = NULL;
  PRINTER_INFO_2 *ppi2 = NULL;
  LPTSTR pBuffer = NULL;
  LONG lResult;

  // What version of Windows are you running?
  osv.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  GetVersionEx(&osv);

  if (!pPrinterName)
    return FALSE;

  // If Windows 95 or 98, use SetPrinter...
  if (osv.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
  {
    // Open this printer so you can get information about it...
    bFlag = OpenPrinter(pPrinterName, &hPrinter, NULL);
    if (!bFlag || !hPrinter)
      return FALSE;

    // The first GetPrinter() tells you how big our buffer should
    // be in order to hold ALL of PRINTER_INFO_2. Note that this will
    // usually return FALSE. This only means that the buffer (the 3rd
    // parameter) was not filled in. You don't want it filled in here...
    GetPrinter(hPrinter, 2, 0, 0, &dwNeeded);
    if (dwNeeded == 0)
    {
      ClosePrinter(hPrinter);
      return FALSE;
    }

    // Allocate enough space for PRINTER_INFO_2...
    ppi2 = (PRINTER_INFO_2 *)GlobalAlloc(GPTR, dwNeeded);
    if (!ppi2)
    {
      ClosePrinter(hPrinter);
      return FALSE;
    }

    // The second GetPrinter() will fill in all the current information
    // so that all you need to do is modify what you're interested in...
    bFlag = GetPrinter(hPrinter, 2, (LPBYTE)ppi2, dwNeeded, &dwNeeded);
    if (!bFlag)
    {
      ClosePrinter(hPrinter);
      GlobalFree(ppi2);
      return FALSE;
    }

    // Set default printer attribute for this printer...
    ppi2->Attributes |= PRINTER_ATTRIBUTE_DEFAULT;
    bFlag = SetPrinter(hPrinter, 2, (LPBYTE)ppi2, 0);
    if (!bFlag)
    {
      ClosePrinter(hPrinter);
      GlobalFree(ppi2);
      return FALSE;
    }

    // Tell all open applications that this change occurred.
    // Allow each application 1 second to handle this message.
    lResult = SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0L,
(LPARAM)(LPCTSTR)"windows", SMTO_NORMAL, 1000, NULL);
  }

  // If Windows NT, use the SetDefaultPrinter API for Windows 2000,
  // or WriteProfileString for version 4.0 and earlier...
  else if (osv.dwPlatformId == VER_PLATFORM_WIN32_NT)
  {
#if(WINVER >= 0x0500)
    if (osv.dwMajorVersion >= 5) // Windows 2000 or later...
    {
      bFlag = SetDefaultPrinter(pPrinterName);
      if (!bFlag)
        return FALSE;
    }

    else // NT4.0 or earlier...
#endif
    {
      // Open this printer so you can get information about it...
      bFlag = OpenPrinter(pPrinterName, &hPrinter, NULL);
      if (!bFlag || !hPrinter)
        return FALSE;

      // The first GetPrinter() tells you how big our buffer should
      // be in order to hold ALL of PRINTER_INFO_2. Note that this will
      // usually return FALSE. This only means that the buffer (the 3rd
      // parameter) was not filled in. You don't want it filled in here...
      GetPrinter(hPrinter, 2, 0, 0, &dwNeeded);
      if (dwNeeded == 0)
      {
        ClosePrinter(hPrinter);
        return FALSE;
      }

      // Allocate enough space for PRINTER_INFO_2...
      ppi2 = (PRINTER_INFO_2 *)GlobalAlloc(GPTR, dwNeeded);
      if (!ppi2)
      {
        ClosePrinter(hPrinter);
        return FALSE;
      }

      // The second GetPrinter() fills in all the current<BR/>
      // information...
      bFlag = GetPrinter(hPrinter, 2, (LPBYTE)ppi2, dwNeeded, &dwNeeded);
      if ((!bFlag) || (!ppi2->pDriverName) || (!ppi2->pPortName))
      {
        ClosePrinter(hPrinter);
        GlobalFree(ppi2);
        return FALSE;
      }

      // Allocate buffer big enough for concatenated string.
      // String will be in form "printername,drivername,portname"...
      pBuffer = (LPTSTR)GlobalAlloc(GPTR,
        lstrlen(pPrinterName) +
        lstrlen(ppi2->pDriverName) +
        lstrlen(ppi2->pPortName) + 3);
      if (!pBuffer)
      {
        ClosePrinter(hPrinter);
        GlobalFree(ppi2);
        return FALSE;
      }

      // Build string in form "printername,drivername,portname"...
      lstrcpy(pBuffer, pPrinterName);  lstrcat(pBuffer, ",");
      lstrcat(pBuffer, ppi2->pDriverName);  lstrcat(pBuffer, ",");
      lstrcat(pBuffer, ppi2->pPortName);

      // Set the default printer in Win.ini and registry...
      bFlag = WriteProfileString("windows", "device", pBuffer);
      if (!bFlag)
      {
        ClosePrinter(hPrinter);
        GlobalFree(ppi2);
        GlobalFree(pBuffer);
        return FALSE;
      }
    }

    // Tell all open applications that this change occurred.
    // Allow each app 1 second to handle this message.
    lResult = SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0L, 0L,
SMTO_NORMAL, 1000, NULL);
  }

  // Cleanup...
  if (hPrinter)
    ClosePrinter(hPrinter);
  if (ppi2)
    GlobalFree(ppi2);
  if (pBuffer)
    GlobalFree(pBuffer);

  return TRUE;

Quote:
}

/***************************************************************************
******/

Quote:
> Howdy,

> I need to write a little systray app that will the user change the default
> printer for Windows 98 or Windows 2000.  I know how to enumerate the
> available printers, but can anyone pass me a pointer to how to designate
the
> default printer from within an application?

> thanks

> Jim Howard



Fri, 23 Jul 2004 21:09:48 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Windows Default Printers and Default Printer Setups

2. Changing the default windows printer

3. Set default printer programatically?

4. Programatically changing the default browser

5. Changing printing paper size programatically (not through the printer dialog)

6. Changing default printer

7. How to change the default printer

8. Changing the default printer

9. change system default printer?

10. Changing the default Printer Tray

11. Changing Default Printer Tray

12. Changing Default Printer

 

 
Powered by phpBB® Forum Software