Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

check if a printed is installed and set it to default

Posted on 2014-10-06
4
Medium Priority
?
151 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

719 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