Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Setting Default Printer

Posted on 1998-12-22
9
Medium Priority
?
268 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 800 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
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!

 
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

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!

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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.

671 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