Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

check if a printed is installed and set it to default

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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

916 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