Solved

check if a printed is installed and set it to default

Posted on 2014-10-06
4
144 Views
Last Modified: 2014-10-06
Hello. I'dont understand C++ so much. I need to check if a printer is locally installed in a PC and set it to default printer. Can you provide some example code?

Thanks in advance,
Leonel
0
Comment
Question by:leorocha
[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
4 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 40364535
If you are on Windows, you can check the default printer using 'GetDefaultPrinter()' (http://msdn.microsoft.com/en-us/library/windows/desktop/dd144876%28v=vs.85%29.aspx), e.g.

TCHAR acPrinter[MAX_PATH];
DWORD dwSize = MAX_PATH;

  if (GetDefaultPrinter(acPrinter,&dwSize)) {

    // check if 'acPrinter' contains the name of the printer that you need to be the default
  }

Open in new window

0
 

Author Comment

by:leorocha
ID: 40364740
jkr, I just need to know if a printer is installed and then set that printer as default, not getting the default printer.
0
 
LVL 86

Expert Comment

by:jkr
ID: 40364757
For the latter you can use 'SetDefaultPrinter()' (http://msdn.microsoft.com/en-us/library/windows/desktop/dd162971%28v=vs.85%29.aspx). But, I am a bit confused, since if one printer is installed, it most likely is set as the default printer. If you want to know if there is a default printer at all, the above snippet works as well, just check for 'GetDefaultPrinter()' returning FALSE.
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 40364777
BTW, to enumerate all installed printers, use the sample code from http://www.news2news.com/vfp/?example=146&ver=vcpp ("Using EnumPrinters function to enumerate locally installed printers"):
void EnumeratePrinters(DWORD flags)
{
    PRINTER_INFO_2* prninfo=NULL;
    DWORD needed, returned;
 
    //find required size for the buffer
    EnumPrinters(flags, NULL, 2, NULL, 0, &needed, &returned);
 
    //allocate array of PRINTER_INFO structures
    prninfo = (PRINTER_INFO_2*) GlobalAlloc(GPTR,needed);
 
    //call again
    if (!EnumPrinters(flags, NULL, 
        2, (LPBYTE) prninfo, needed, &needed, &returned))
    {
        cout << "EnumPrinters failed with error code " 
            << GetLastError() << endl;
    }
    else
    {
        cout << "Printers found: " << returned << endl << endl;
        for (int j=0; j)
        {
            cout << prninfo[j].pPrinterName << " on " 
                << prninfo[j].pPortName << endl;
            cout << "Status=" << prninfo[j].Status 
                << ", jobs=" << prninfo[j].cJobs << endl << endl;
        }
    }
 
    GlobalFree(prninfo);
}

Open in new window


The values returned in
prninfo[i].pPrinterName

Open in new window

can be used in your call to 'SetDefaultPrinter()'.
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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 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 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.

761 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