EnumPrinters: How To

Hi,
I am pretty new to C++ and I have been trying to develop an application using printers.

I need to list every printer installed, and I thought doing it using EnumPrinters.
I have read MSDN documentation on this, as well as some topics on this forum and didn't find an answer that fit.

I keep on having this error:

C:\Programação\C++\lab\main.cpp 23 undefined reference to `EnumPrintersA@28'

I am using codeBlocks and no GUI, just a simple CLI program.

Here follows the code I tried:

#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
DWORD pcbNeeded, pcbReturned;
LPSTR printer = NULL;

EnumPrinters(PRINTER_ENUM_LOCAL,printer,1,NULL,0,&pcbNeeded,&pcbReturned);
}

I want to write the printers names on the screen. But I never got to that part because I can't have the program to build correctly as it is just yet.

Can anyone help me?

Thank you!
That's a linker error. Have you linked your project with winspool.lib?
Yes, after some digging I found out it could be the problem, but I don't know how to solve this.

it looks like when I include <windows.h> I am already including <winspool.h> (you can find the include line inside windows.h).

But I have forced it by also putting a #include <winspool.h> line.

I thought I could be misunderstanding how the function works and I updated my code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <windows.h>
#include <winspool.h>

using namespace std;
int main()
{
    PRINTER_INFO_5 pi;
    PBYTE buffer[99];
    DWORD bufferSize = 0;
    DWORD bufferNeeded = 0;
    DWORD Entries = 0;

    bool r;

    r = EnumPrinters(PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS, NULL, 5, NULL, bufferSize, &bufferNeeded, &Entries);

    if (!r)
    { cout << "No printer found" << endl; }
    else { cout << "Found printers" << endl; }
}


But the problem remains...
Last edited on
You've misunderstood. You need to link with the library, not just include it's header.

http://stackoverflow.com/questions/5862757/how-do-i-link-to-a-library-with-codeblocks

Hope this helps.
Perfect! Thanks a lot!
Topic archived. No new replies allowed.