Solved

get/set default printer

Posted on 1997-10-02
2
364 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to calculate times for developing software? 8 65
Adapt this command to show who installed 29 106
sumDigits challenge 9 125
how to split multiple lines delimiter : 8 79
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
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.

895 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

11 Experts available now in Live!

Get 1:1 Help Now