Solved

Setting Default Printer

Posted on 1998-12-22
9
261 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

705 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