Solved

Setting Default Printer

Posted on 1998-12-22
9
264 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
Dynamically allocate memory 9 62
I could not build boost code, 10 94
What is sub-make ? 2 71
DCT of  2D array using fftw in c++ 9 63
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

856 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