CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 1999
    Posts
    51

    Getting List of Installed Printers

    Anybody have an example of getting a list of installed printer names on a computer.

    Steven M. McNeese
    [email protected]

  2. #2
    Join Date
    May 1999
    Location
    Houston - TX - US
    Posts
    29

    Re: Getting List of Installed Printers

    Hi,
    below is the pice of code for getting the list of printers!


    // RETURNS: TRUE if successful,
    // FALSE otherwise

    BOOL CPrinterDialog:isplayAllAvailablePrinters()
    {
    CListBox * pAvailablePrintersLB = (CListBox *)GetDlgItem( IDC_AVAILABLE_PRINTERS ) ;
    DWORD dwBytesNeeded;
    DWORD dwPrtRet1 ;
    LPPRINTER_INFO_1 pPrtInfo1 = NULL ;

    BOOL bReturn = TRUE;

    BeginWaitCursor() ;

    // get byte count needed for buffer, alloc buffer, the enum the printers

    EnumPrinters ( PRINTER_ENUM_LOCAL
    , NULL , 1 , (LPBYTE) pPrtInfo1 , 0 , &dwBytesNeeded , &dwPrtRet1);

    // (simple error checking, if these work assume rest will too)
    pPrtInfo1 = (LPPRINTER_INFO_1) LocalAlloc (LPTR, dwBytesNeeded) ;
    if ( !pPrtInfo1 )
    {
    bReturn = FALSE;
    LocalFree (LocalHandle (pPrtInfo1));
    RestoreWaitCursor();
    EndWaitCursor() ;
    return bReturn ;
    }

    if ( !EnumPrinters ( PRINTER_ENUM_LOCAL
    , NULL, 1, (LPBYTE) pPrtInfo1, dwBytesNeeded, &dwBytesNeeded, &dwPrtRet1))
    {
    LPVOID lpMsgBuf ;
    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
    NULL,GetLastError(),MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), //Default language
    (LPTSTR)&lpMsgBuf, 0,NULL );
    //Display the string.
    AfxMessageBox( (LPCTSTR)lpMsgBuf );
    // Free the buffer.
    LocalFree( lpMsgBuf );
    bReturn = FALSE;
    LocalFree (LocalHandle (pPrtInfo1));
    RestoreWaitCursor();
    EndWaitCursor() ;
    return bReturn;
    }

    // If we don't get any printers from the Level == 4 call, there is
    // no point in continuing... report it, free memory, and return.
    for ( DWORD i = 0 ; i < dwPrtRet1 ; i++ )
    {
    pAvailablePrintersLB->AddString( pPrtInfo1[i].pName ) ;
    }



    EnumPrinters ( PRINTER_ENUM_CONNECTIONS
    , NULL , 1 , (LPBYTE) pPrtInfo1 , 0 , &dwBytesNeeded , &dwPrtRet1);

    LocalFree (LocalHandle (pPrtInfo1));

    // (simple error checking, if these work assume rest will too)
    pPrtInfo1 = (LPPRINTER_INFO_1) LocalAlloc (LPTR, dwBytesNeeded) ;
    if ( ! pPrtInfo1 )
    {
    bReturn = FALSE;
    LocalFree (LocalHandle (pPrtInfo1));
    RestoreWaitCursor();
    EndWaitCursor() ;
    return bReturn ;
    }

    if ( !EnumPrinters ( PRINTER_ENUM_CONNECTIONS // PRINTER_ENUM_NETWORK
    , NULL, 1, (LPBYTE) pPrtInfo1, dwBytesNeeded, &dwBytesNeeded, &dwPrtRet1))
    {
    LPVOID lpMsgBuf ;
    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
    NULL,GetLastError(),MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), //Default language
    (LPTSTR)&lpMsgBuf, 0,NULL );
    //Display the string.
    AfxMessageBox( (LPCTSTR)lpMsgBuf );
    // Free the buffer.
    LocalFree( lpMsgBuf );
    bReturn = FALSE;
    LocalFree (LocalHandle (pPrtInfo1));
    RestoreWaitCursor();
    EndWaitCursor() ;
    return bReturn;
    }

    // If we don't get any printers from the Level == 4 call, there is
    // no point in continuing... report it, free memory, and return.
    for ( i = 0 ; i < dwPrtRet1 ; i++ )
    {
    pAvailablePrintersLB->AddString( pPrtInfo1[i].pName ) ;
    }


    LocalFree (LocalHandle (pPrtInfo1));
    EndWaitCursor() ;
    RestoreWaitCursor();
    return bReturn;

    }


    Hope this helps !

    Good Luck.
    Yash.



  3. #3
    Join Date
    Apr 1999
    Posts
    51

    Re: Getting List of Installed Printers

    Where is the definition of LPPRINTER_INFO_1? Is there a way to do this with MFC classes?

    Steven M. McNeese
    [email protected]

  4. #4
    Join Date
    May 1999
    Location
    Houston - TX - US
    Posts
    29

    Re: Getting List of Installed Printers

    Hi Steven,
    Look the include file "winspool.h" for the defn of LPPRINTER_INFO_?? .

    Good Luck.
    Yash.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured