Solved

Setting Default Printer

Posted on 1998-12-22
9
266 Views
Last Modified: 2010-04-02
Does anyone know how to set the default printer in a windows nt4.0 environment in C++/VC++.
0
Comment
Question by:TAMC
[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
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
ID: 1180847
You have to set HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows\device to a valid 'printer,driver,port' string.
0
 
LVL 23

Expert Comment

by:chensu
ID: 1180848
The correct way is described in the following KB article.

HOWTO: Get and Set the Default Printer in Windows
http://support.microsoft.com/support/kb/articles/Q135/3/87.asp

How to Set the Default Printer Programmatically in Windows 95
http://support.microsoft.com/support/kb/articles/q140/5/60.asp
0
 
LVL 1

Author Comment

by:TAMC
ID: 1180849
chensu --> are you sure this will work in NT, it does say only windows 95 in "How to Set the Default Printer Programmatically in Windows 95", and I beleive the first is for OS's with an ini file, and NT 4.0, your not suppose to use that.
0
Industry Leaders: 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!

 
LVL 86

Expert Comment

by:jkr
ID: 1180850
>>it does not work on Windows NT or on a 16-bit platform

The docs are a bit ambivalent on this, but the registry key i mentioned is where NT retrieves the information about the default printer...
0
 
LVL 23

Expert Comment

by:chensu
ID: 1180851
In the article

HOWTO: Get and Set the Default Printer in Windows
http://support.microsoft.com/support/kb/articles/Q135/3/87.asp

It says "This is the preferred method of changing the printer if the code is to be platform independent; this method will work on Windows 3.1, Windows 95 and Windows NT." It also gives you Windows NT sample code.
0
 
LVL 1

Author Comment

by:TAMC
ID: 1180852
jkr - personnally i like your answer better, no offense chensu you obviosly know what your talking about, but i don't agree with you here.  jkr if you could show me the code, i'm not much of a registry editing programmer, i will give you the points.
0
 
LVL 86

Expert Comment

by:jkr
ID: 1180853
Ok, here it is:
HRESULT ChangePrinter   (   LPCTSTR psz) // unused, string is hard-coded for demonstration purposes
{

    HRESULT     hret    =   NOERROR;

    DWORD       dwType;
    DWORD       dwSize;
    LPCTSTR     pszKey      =   _T  (   "Software\\ProXima\\InsaReport\\PrintForms");//fixme
    LPCTSTR     pszPrinter  =   _T  (   "\\\\pserv\\HP DeskJet 500C,winspool,Ne01:");

    if  (   ERROR_SUCCESS   !=  RegOpenKeyEx    (   HKEY_CURRENT_USER,
                                                    pszKey,
                                                    0,
                                                    KEY_ALL_ACCESS,
                                                    &hKey
                                                )
        )   return  (   E_FAIL);

    dwType  =   REG_SZ;
    dwSize  =   _tcslen (   pszPrinter);

    if  (   ERROR_SUCCESS   !=  (   lnErr   =   RegSetValueEx   (   hKey,
                                                                    _T  (   "Device"),
                                                                    NULL,
                                                                    dwType,
                                                                    ( BYTE*) pszPrinter,
                                                                    dwSize
                                                                )
                                )
        )   hret    =   GetLastError    ();

    RegCloseKey (   hKey);

    return  (   hret);
}
0
 
LVL 1

Author Comment

by:TAMC
ID: 1180854
Thanks, jkr, worked out wonderfully.
0
 

Expert Comment

by:Sreejith
ID: 1180855
You should set the printer name in the registory key.
First you call EnumPrinter funtion and fill an arry or combo with the printer name using the folloing code..
 
LPPRINTER_INFO_2 pPrinters;
DWORD cbPrinters;

 pPrinters=new PRINTER_INFO_2[500];
 cbPrinters = 500 * sizeof(PRINTER_INFO_2);
   EnumPrinters (dwFlags, NULL, 2, (LPBYTE) pPrinters,
                     cbPrinters, &cbPrinters, &cReturned);
  lp =pPrinters;
   char buf[50];
if (cReturned > 0)
  {
    for (int i = 0; i<cReturned; i++)
    {
       strcpy (buf, (pPrinters + i)->pPrinterName);
      m_Combo.AddString(csPrinterName);//m_Combo --combo object
 }
  }
      delete[] pPrinters;
After that select the printer from the array or combo and set the registory entry with the printer name as default .
HKEY_LOCAL_MACHINE\CONFIG\SYSTEM\CurrentControlSet\cONTROL\print\
printers\printername

Printer name from our program array..

we shold write code for setting the regitory enties..

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

734 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