Thread: Printer enumeration problem

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    329

    Printer enumeration problem

    Hello.
    I have a problem with printer enumeration...The following code works ok on XP, but not on Win2k.
    On Win2k network printers don't show up in the dropdown list, even though they are installed on the local machine. Printers that are physically connected to a port on the local machine, however, show up..!

    Code:
    if(EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS, "", 5, (UCHAR*)pI1, sizeof(PRINTER_INFO_2)*MAX_PRINTERS, &lSize, &lRet)){
      for(int i=0;i<(int)lRet && i<MAX_PRINTERS;i++){
        SendMessage(GetDlgItem(hwThis, CBO_PRT), CB_ADDSTRING, 0, (long)pI1[i].pPrinterName);
        strcpy(achPorts[i], pI1[i].pPortName);
        if(!strcmp(pI1[i].pPrinterName, sil->achPrinter))
          chIx=i;
      }
    }else
      GetErr("EnumPrinters");

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    http://msdn.microsoft.com/library/de...tspol_9fjn.asp

    Try add an extra flag to the first param...probably PRINTER_ENUM_NETWORK

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    I've tried all different flag versions.
    The function call returns successfully, but does not return printers connected through the network...

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Quote Originally Posted by knutso
    I've tried all different flag versions.
    The function call returns successfully, but does not return printers connected through the network...
    Hmm...

    Try this...I havent checked it on Win2000 but see if it works

    Code:
    #include <windows.h>
    #include <iostream>
    
    int main()
    {
    	DWORD Needed = 0,Found = 0;
    	union PRINT_INFO_ENUM
    	{
    		PRINT_INFO_ENUM():AsBytes(0){}
    		LPBYTE AsBytes;
            PRINTER_INFO_1 *AsInfo;
    	}PrinterInfo;
    
    	//Get size of array
    	EnumPrinters( PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS ,0,1,0,1,&Needed,&Found);
    	
    	try
    	{
    		PrinterInfo.AsBytes = new BYTE[Needed];
    	}
    	catch(...)
    	{
    		std::cout << "Memory Error";
    		return -1;
    	}
    	
    	//Enum
    	if(!EnumPrinters(PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS ,0,1,
    		PrinterInfo.AsBytes,Needed,&Needed,&Found))
    	{
    		std::cout << "Unable to enumerate " << GetLastError();
    		return -1;
    	}
    
    	for(DWORD i = 0;i < Found;++i)
    		std::cout << PrinterInfo.AsInfo[i].pName << std::endl;
    
    	delete [] PrinterInfo.AsBytes;
    }

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    I use PRINTER_ENUM_FAVORITE
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    Now i have tried all of what you have suggested, with the same results...
    The OS is win2k server, i wonder if this is the problem..?

  7. #7
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    I use a print info 2 on NT based OS and print info 5 on 9X.

    EnumPrinters (PRINTER_ENUM_FAVORITE | PRINTER_ENUM_LOCAL, NULL, 2, (PBYTE) pinfo2, dwNeeded, &dwNeeded, &dwReturned) ;

    I have also found the name returned in the Devinfo can be truncated. (if you are trying to match them exactly)
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  8. #8
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    Still returns nothing...I'm fetching the printers directly from the registry instead.
    Thank you for your time anyway...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enumeration problem
    By baniakjr in forum C++ Programming
    Replies: 8
    Last Post: 11-11-2006, 02:32 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM