Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to use EnumPrinters function to enum all printers

How to use EnumPrinters function to enum all printers

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GflPower
    wrote on last edited by
    #1

    What's wrong with my code ? CString PrintName; PRINTER_INFO_1 PrintInfo[3]; LPDWORD pNeedSize; LPDWORD pPrinters; DWORD cbSize; cbSize = sizeof(PrintInfo); if ( EnumPrinters(PRINTER_ENUM_NAME,(LPTSTR)&PrintName,1,(LPBYTE)PrintInfo,cbSize,pNeedSize,pPrinters) != 0 ) { this->MessageBox("SUCC"); } else { int err = ::GetLastError(); this->MessageBox("fail"); } d

    P I 2 Replies Last reply
    0
    • G GflPower

      What's wrong with my code ? CString PrintName; PRINTER_INFO_1 PrintInfo[3]; LPDWORD pNeedSize; LPDWORD pPrinters; DWORD cbSize; cbSize = sizeof(PrintInfo); if ( EnumPrinters(PRINTER_ENUM_NAME,(LPTSTR)&PrintName,1,(LPBYTE)PrintInfo,cbSize,pNeedSize,pPrinters) != 0 ) { this->MessageBox("SUCC"); } else { int err = ::GetLastError(); this->MessageBox("fail"); } d

      P Offline
      P Offline
      Prakash Nadar
      wrote on last edited by
      #2

      One thing i can see is

      CString PrintName;
      ...
      EnumPrinters(PRINTER_ENUM_NAME,(LPTSTR)&PrintName,1,(LPBYTE)PrintInfo,cbSize,pNeedSize,pPrinters)

      it should be

      EnumPrinters(PRINTER_ENUM_NAME,PrintName.GetBuffer(100),1,(LPBYTE)PrintInfo,cbSize,pNeedSize,pPrinters)

      Is there anyother error that you get ?


      MSN Messenger. prakashnadar@msn.com Tip of the day of visual C++ IDE. "We use it before you do! Visual C++ was developed using Visual C++"

      1 Reply Last reply
      0
      • G GflPower

        What's wrong with my code ? CString PrintName; PRINTER_INFO_1 PrintInfo[3]; LPDWORD pNeedSize; LPDWORD pPrinters; DWORD cbSize; cbSize = sizeof(PrintInfo); if ( EnumPrinters(PRINTER_ENUM_NAME,(LPTSTR)&PrintName,1,(LPBYTE)PrintInfo,cbSize,pNeedSize,pPrinters) != 0 ) { this->MessageBox("SUCC"); } else { int err = ::GetLastError(); this->MessageBox("fail"); } d

        I Offline
        I Offline
        Indivara
        wrote on last edited by
        #3

        I think you need to call EnumPrinters twice, the first time to find out how much memory is required to store the information returned, and the second time to actually get that information. It probably doesn't work unless you allocate enough memory for the information returned. Also, to enumerate all printers, use PRINTER_ENUM_LOCAL refer MSDN for details http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/prntspol_9fjn.asp[^] sample code

        DWORD dwSizeNeeded = 0;
        DWORD dwPrinterCount = 0;
        BOOL bEnumResult;
        CString strErr;
        HANDLE hProcessHeap = GetProcessHeap();
        LPPRINTER_INFO_2 lpPrinterData;
        
        // call once to find buffer size
        EnumPrinters(PRINTER_ENUM_LOCAL, NULL, 2, NULL, 0, &dwSizeNeeded, &dwPrinterCount);
        
        // allocate buffer memory
        if ((lpPrinterData = (LPPRINTER_INFO_2)HeapAlloc(hProcessHeap, 
        	HEAP_ZERO_MEMORY, dwSizeNeeded)) == NULL) 
        {
        	// error
        	return;
        }
        // call again with new buffer size
        bEnumResult = EnumPrinters (PRINTER_ENUM_LOCAL,
        	NULL,
        	2,
        	(LPBYTE)lpPrinterData,
        	dwSizeNeeded,
        	&dwSizeNeeded,
        	&dwPrinterCount);
        
        if (bEnumResult) {
        	if (dwPrinterCount == 0) {
        		// no printers installed
        		HeapFree(hProcessHeap, 0, lpPrinterData);
        		return;
        	}
        	// got data, process it here
        }
        // clean up and return
        HeapFree(hProcessHeap, 0, lpPrinterData);
        return;
        
        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups