Solved

get/set default printer

Posted on 1997-10-02
2
360 Views
Last Modified: 2013-11-19
How can I determine the default printer?  And changing it?
0
Comment
Question by:dmetzler
2 Comments
 
LVL 8

Accepted Solution

by:
gelbert earned 50 total points
ID: 1307377
Artical from MSDN: keyword "default printer"

Getting/Setting the Default Printer from Windows
 
SUMMARY
The "device=" entry in the [windows] section of WIN.INI refers to the default printer. The way to change it is to call WriteProfileString("windows", "device", szNewPrinterSpec), where szNewPrinterSpec is of the form "printer name, printer driver base filename, port connected to printer". For example:

   HP LaserJet IIISi,HPPCL5MS,LPT1:



MORE INFORMATION
The currently installed printers are contained in the WIN.INI [devices] section. It is possible to build a list of the installed printers by calling GetProfileString("devices", NULL, "", szDriverNames, sizeof(szDriverNames)). This will fill the szDriverNames with the names of all printers installed separated by a null byte, with a double null byte at the end of the list.

Choose the new printer from the list provided and call

GetProfileString("devices", szDriverRequested, "", szPrinter, sizeof(szPrinter)) with szDriverRequested set to the printer name requested. Once the call returns, the szPrinter will contain the rest of the information needed to create the szNewPrinterSpec. Create a new szPrinterSpec from both szPrinterRequested and szPrinter and make the call.
Once WIN.INI is updated, call SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, 0L) to send the correct notification to all top level windows after WIN.INI gets changed programmatically. Applications that care about WIN.INI listen for this message and reread WIN.INI as needed.

The following code gets and sets the default printer:

   #include <windows.h>
   #include <string.h>

   #define MAXSTRING 255

   int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrevInst,
                      LPSTR lpszCmdLine, int CmdShow)
   {
      char szFmtStr  [] = "%s,%s";
      char szDevice  [] = "device";
      char szDevices [] = "devices";
      char szWindows [] = "windows";
      char szDefault [] = "default";
      char szInstPrn [] = "Set this installed printer as default?";
      char szDefPrn  [] = "The default printer";
      char szBuff    [MAXSTRING];
      char szRetBuff [MAXSTRING];
      char szVal     [MAXSTRING];
      int nBytes;
      HLOCAL hLocal;
      PSTR pszBuf, pszKey;

      nBytes = GetProfileString(szWindows, szDevice, szDefault, szRetBuff,
                                sizeof(szRetBuff));

      MessageBox(NULL,szRetBuff, szDefPrn, MB_OK | MB_ICONINFORMATION);

      hLocal = LocalAlloc(LHND, 1024);
      pszBuf = (PSTR) LocalLock(hLocal);

      GetProfileString(szDevices, NULL, szDefault, pszBuf, 1024);

      for (pszKey = pszBuf; *pszKey != '\0'; pszKey += strlen(pszKey) + 1)
      {
         int nID;

         GetProfileString(szDevices, pszKey, szDefault,
                          szVal, sizeof(szVal));
         wsprintf(szBuff, szFmtStr, (LPSTR) pszKey, (LPSTR) szVal);
         nID = MessageBox(NULL, szBuff, szInstPrn, MB_YESNOCANCEL
                         | MB_ICONQUESTION);
         switch(nID)
         {
            case IDNO:
               continue;

            case IDYES:
               WriteProfileString(szWindows, szDevice, szBuff);
               WriteProfileString(NULL, NULL, NULL);
               SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, 0L);

            default:
               break;

         }
         break;
      }
      LocalUnlock(hLocal);
      LocalFree(hLocal);
      return(0);
   }

0
 

Author Comment

by:dmetzler
ID: 1307378
Thanks for the detailed response!
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now