Dec 232017

Use following PowerShell commands to list all printers installed on any machine…

First command uses WMI (Windows Management Instrumentation)…

Get-WmiObject -Class "Win32_Printer" -ComputerName "localhost" | ft -Property Name, DriverName, SystemName, ShareName -AutoSize
Command Output
Name                           DriverName                                  SystemName        ShareName   
----                           ----------                                  ----------        ---------   
Send To OneNote 2016           Send to Microsoft OneNote 16 Driver         MYMACHINE                    
Microsoft XPS Document Writer  Microsoft XPS Document Writer v4            MYMACHINE                    
Microsoft Print to PDF         Microsoft Print To PDF                      MYMACHINE                    
Fax                            Microsoft Shared Fax Driver                 MYMACHINE                     

The next command uses built-in PowerShell command Get-Printer

Get-Printer -ComputerName mymachine
Command Output
Name                           ComputerName    Type         DriverName                PortName        Shared   Published  DeviceType     
----                           ------------    ----         ----------                --------        ------   ---------  ----------     
Send To OneNote 2016           mymachine       Local        Send to Microsoft OneN... xxx:            False    False      Print          
Microsoft XPS Document Writer  mymachine       Local        Microsoft XPS Document... xxxxxxxxxx:     False    False      Print          
Microsoft Print to PDF         mymachine       Local        Microsoft Print To PDF    xxxxxxxxxx:     False    False      Print          
Fax                            mymachine       Local        Microsoft Shared Fax D... xxxxxx:         False    False      Print
Please note that I’ve removed some printers from this list due to security issues.
May 142008

After going through this post, you should be having your first printed page, that you printed through code like me!

There are a set of API’s provided by Microsoft for working with printers. Some functions are listed below…

  1. GetDefaultPrinter
  2. SetDefaultPrinter
  3. OpenPrinter – Returns a handle for a given printer name
  4. ClosePrinter – Use this instead of CloseHandle
  5. GetPrinter – Returns printer details based on the level that you have chosen
  6. CreateDC – Create a printer DC to draw onto it, windows works with devices using device contexts as you should be knowing
  7. StartDoc – Start fresh document
  8. StartPage – Start a page
  9. EndPage – End a page
  10. EndDoc – End of document

I’ve used these functions in this function that I am demonstrating here, it’s well commented so should be easy for you to understand.

void PrintTest( LPCTSTR lpctszStringToPrint )
    ASSERT( lpctszStringToPrint );
    // Get default printer name
    TCHAR szDefPrinterName[MAX_PATH] = { 0 };
    DWORD Size = MAX_PATH;
    GetDefaultPrinter( szDefPrinterName, &Size );

    // Get a handle to default printer by passing it's name to OpenPrinter
    HANDLE hPrinter = NULL;
    VERIFY( OpenPrinter( szDefPrinterName, &hPrinter, NULL ));

    // Get printer information, like driver name and port number
    LPPRINTER_INFO_2 pPrnInfo2 = 0;
    Size = 0;
    // First get size to be allocated
    GetPrinter( hPrinter, 2, NULL, 0, &Size );
    // Allocate based on returned size
    pPrnInfo2 = (LPPRINTER_INFO_2)new BYTE[Size];
    // Now get real information
    GetPrinter( hPrinter, 2, (LPBYTE)pPrnInfo2, Size, &Size );

    // Prepare a document information structure for our document
    DOCINFO DocInf = { 0 };
    DocInf.cbSize = sizeof( DocInf );
    DocInf.lpszDocName = _T( "Test doc" ); // Name of document

    // We use CreateDC to create a printer dc by passing
    // device name of our default printer
    CDC dcPrn;
    VERIFY( dcPrn.Attach( CreateDC( pPrnInfo2->pDriverName, pPrnInfo2->pPrinterName, pPrnInfo2->pPortName, pPrnInfo2->pDevMode )));

    // Start printing stuff //
    StartDoc( dcPrn, &DocInf );// Fresh document
    StartPage( dcPrn ); // Fresh page
    // Draw a sample string
    dcPrn.TextOut( 5, 5, lpctszStringToPrint, _tcslen( lpctszStringToPrint ));
    EndPage( dcPrn );// Done with this page
    EndDoc( dcPrn );// Done with this document
    // End printing stuff //

    // Free allocated buffer
    delete [] LPBYTE( pPrnInfo2 );
    // Note: always pair OpenPrinter and ClosePrinter, do not use CloseHandle for
    // closing opened printer handle
    ClosePrinter( hPrinter );
}// End PrintTest

// The calling part
int main()
   PrintTest( _T( "Hello Mr. Printer, this is my first page, thanks to Nibu."));

You should be having a printer for testing this 😉 , if you do have one then check out 🙂 . As always these are just basic principles to get you going. The whole printer thingie is worth a book! This much should get you going. All the best!