May 21, 2014 at 2:04pm UTC
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);
}
May 21, 2014 at 2:13pm UTC
why do you think you have 5 printers?
to get the real number of printers you need needed / sizeof (PRINTER_INFO_2)
May 21, 2014 at 2:25pm UTC
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.
May 21, 2014 at 2:32pm UTC
Or you could use the 'returned' variable since that is the number of printers that were found... :p
May 21, 2014 at 2:38pm UTC
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.
May 21, 2014 at 2:43pm UTC
coder777..
thanks for help..this program has working now..:)