If I do an enumprinters while the system is adding a printer via the
"Add Printer Wizard" the system reboots: this happens ALWAYS if there
was no previous printer installed (and obviously no default printer
installed).
I think that this problem affects Windows XP (HOME/PRO) too.
you can check this problem with the following simple test program:
#include <windows.h>
#include <stdio.h>
BOOL CALLBACK WaitProc(HWND hwnd,LPARAM lParam)
{
bool& found = *((bool*)lParam);
char buffer[1024];
memset(buffer,0,1024);
GetWindowText(hwnd,buffer,1024);
if(strstr(buffer,"Add Printer Wizard"))
{
found = true;
return false;
}
return true;
}
BOOL CALLBACK CloseProc(HWND hwnd,LPARAM lParam)
{
bool& found = *((bool*)lParam);
char buffer[1024];
memset(buffer,0,1024);
GetWindowText(hwnd,buffer,1024);
if(strstr(buffer,"Add Printer Wizard"))
{
found = true;
return false;
}
return true;
}
BOOL CALLBACK CopyProc(HWND hwnd,LPARAM lParam)
{
bool& found = *((bool*)lParam);
char buffer[1024];
memset(buffer,0,1024);
GetWindowText(hwnd,buffer,1024);
if(strstr(buffer,"Copying Files"))
{
found = true;
return false;
}
return true;
}
//Check if the number of printers changes...
void main(void)
{
//First check for the actual number of printers...
unsigned int bytes = 0;
unsigned char* buffer = new unsigned char[bytes + 1];
BOOL status = false;
PRINTER_INFO_2* Penum = (PRINTER_INFO_2*)buffer;
DWORD Psize = bytes;
DWORD Pcopy = sizeof(PRINTER_INFO_1);
DWORD Preq = 1;
DWORD Status = 0L;
while(!status && buffer)
{
Penum = (PRINTER_INFO_2*)buffer;
Psize = bytes;
status = EnumPrinters( (DWORD) PRINTER_ENUM_LOCAL,
(LPSTR) NULL,
(DWORD) 2,
(LPBYTE) Penum,
(DWORD) Psize,
(LPDWORD) &Pcopy,
(LPDWORD) &Preq);
if(!status)
{
bytes += Pcopy;
delete[] buffer;
buffer = new unsigned char[bytes + 1];
}
}
unsigned int NumPrinters = Preq;
//Wait for the "Add Printer Wizard" startup...
int found = false;
for(;

{
EnumWindows(WaitProc,(long)&found);
if(found) break;
Sleep(1000);
}
Sleep(1000);
//If Ok continue...
if(found)
{
//Wait for the "Add Printer Wizard" closure...
for(;

{
int found = false;
EnumWindows(CloseProc,(long)&found);
if(!found) break;
Sleep(1000);
}
Sleep(1000);
//Wait for the "Copy Files" startup...
for(;

{
int found = false;
EnumWindows(CopyProc,(long)&found);
if(!found) break;
Sleep(1000);
}
Sleep(1000);
}
//Check if the number of printers is changed...
for(unsigned int i=0;i < 60;i++)
{
unsigned int bytes = 0;
unsigned char* buffer = new unsigned char[bytes + 1];
BOOL status = false;
PRINTER_INFO_2* Penum = (PRINTER_INFO_2*)buffer;
DWORD Psize = bytes;
DWORD Pcopy = sizeof(PRINTER_INFO_1);
DWORD Preq = 1;
DWORD Status = 0L;
while(!status && buffer)
{
Penum = (PRINTER_INFO_2*)buffer;
Psize = bytes;
status = EnumPrinters( (DWORD) PRINTER_ENUM_LOCAL,
(LPSTR) NULL,
(DWORD) 2,
(LPBYTE) Penum,
(DWORD) Psize,
(LPDWORD) &Pcopy,
(LPDWORD) &Preq);
if(!status)
{
bytes += Pcopy;
delete[] buffer;
buffer = new unsigned char[bytes + 1];
}
}
if(Preq != NumPrinters)
{
//SOMETHING IS CHANGED...
NumPrinters = Preq;
break;
}
if(buffer) delete[] buffer;
buffer = NULL;
Sleep(1000);
}
Sleep(60 * 1000);
}