how to get information about printer

hi,
i'am new user for cplusplus.
i have problem when i run this program, i need your help to solve this problem.
The problem is when i run this program and program is stop working but the output is out.
please help me.


#include <iostream>
#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <conio.h>
using namespace std;


void EnumeratePrinters(DWORD flags);

void main()
{
EnumeratePrinters(PRINTER_ENUM_LOCAL);
cout << "\nPress any key...";
int getch();
}

void EnumeratePrinters(DWORD flags)
{
PRINTER_INFO_2* prninfo;
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 i=0;i<5 ;i++)
{
cout << prninfo[i].pPrinterName << " on "
<< prninfo[i].pPortName << endl;
cout << "Status=" << prninfo[i].Status
<< ", jobs=" << prninfo[i].cJobs <<endl << endl;

}
}

GlobalFree(prninfo);
}
why do you think you have 5 printers?
to get the real number of printers you need needed / sizeof(PRINTER_INFO_2)
owh i don't think i have 5 printer..i just put it..but i don't understand for using this
needed / sizeof(PRINTER_INFO_2)..where can i put this..can you help me again..thanks.
Or you could use the 'returned' variable since that is the number of printers that were found... :p
Ok, you don't need this calculation. Just write it so:
1
2
3
cout << "Printers found: " << returned << endl << endl;
for (int i=0;i<returned ;i++) // Note
{
do you mean like this??

int main()
{
EnumeratePrinters(PRINTER_ENUM_LOCAL);
cout << "\nPress any key...";
int getch();
return 0;
}
but it's also has stopped working.
coder777..
thanks for help..this program has working now..:)
Topic archived. No new replies allowed.