Solved

Setting Default Printer

Posted on 1998-12-22
9
263 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
  • 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

770 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