Click to See Complete Forum and Search --> : Getting List of Installed Printers


Steve McNeese
April 13th, 1999, 12:24 PM
Anybody have an example of getting a list of installed printer names on a computer.

Steven M. McNeese
steven.mcneese@boeing.com

yash
April 13th, 1999, 03:44 PM
Hi,
below is the pice of code for getting the list of printers!


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

BOOL CPrinterDialog::DisplayAllAvailablePrinters()
{
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.

Steve McNeese
April 14th, 1999, 09:37 AM
Where is the definition of LPPRINTER_INFO_1? Is there a way to do this with MFC classes?

Steven M. McNeese
steven.mcneese@boeing.com

yash
April 14th, 1999, 01:56 PM
Hi Steven,
Look the include file "winspool.h" for the defn of LPPRINTER_INFO_?? .

Good Luck.
Yash.