Solved

check if a printed is installed and set it to default

Posted on 2014-10-06
4
134 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 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now