Solved

get/set default printer

Posted on 1997-10-02
2
371 Views
Last Modified: 2013-11-19
How can I determine the default printer?  And changing it?
0
Comment
Question by:dmetzler
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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…
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 is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

630 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