|
-
April 13th, 1999, 12:24 PM
#1
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]
-
April 13th, 1999, 03:44 PM
#2
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.
-
April 14th, 1999, 09:37 AM
#3
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]
-
April 14th, 1999, 01:56 PM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|