Solved

check if a printed is installed and set it to default

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

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
C++ to C# code conversion issue 4 132
White board coding practice 3 98
Installshield for Embarcadero EX 10.1 Berlin 4 92
Adding items to a C# list incrementally 5 82
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

739 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