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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Direct Crystal Printing to LPT port - Please Help

Direct Crystal Printing to LPT port - Please Help

Scheduled Pinned Locked Moved C#
sysadminhelptutorial
7 Posts 2 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.
  • C Offline
    C Offline
    Craig G Fraser
    wrote on last edited by
    #1

    Hi, This a a request for some seriously urgent help please... I have an application that needs to print a crystal report directly to a printer attached to a PC. The Print needs to be direct ie: the user is not presented with a preview or a print dialog box. The pc is standalone and not connected to any network. Printing a crystal report directly to a printer is easy if the pc is networked because you can set the PrintOptions.PrinterName to the name of the printer as identified on the network. I need to know how to do the equivalent of this but by specifying the LPT port number. Does anyone know how to do this. Here is my function where i set the print options private void SetPrintOptions() { CrystalDecisions.CrystalReports.Engine.PrintOptions printOptions = crDocument.PrintOption; printOptions.PrinterName = CURRENT_PRINTER; printOptions.PaperOrientation = PaperOrientation.DefaultPaperOrientation; printOptions.PaperSize = PaperSize.PaperA4; printOptions.PrinterDuplex = PrinterDuplex.Default; printOptions.PaperSource = PaperSource.Auto; } Any help will be greatly appreciated. Craigo ** Vision - The ability to look further than your eyes can see :suss:

    H 1 Reply Last reply
    0
    • C Craig G Fraser

      Hi, This a a request for some seriously urgent help please... I have an application that needs to print a crystal report directly to a printer attached to a PC. The Print needs to be direct ie: the user is not presented with a preview or a print dialog box. The pc is standalone and not connected to any network. Printing a crystal report directly to a printer is easy if the pc is networked because you can set the PrintOptions.PrinterName to the name of the printer as identified on the network. I need to know how to do the equivalent of this but by specifying the LPT port number. Does anyone know how to do this. Here is my function where i set the print options private void SetPrintOptions() { CrystalDecisions.CrystalReports.Engine.PrintOptions printOptions = crDocument.PrintOption; printOptions.PrinterName = CURRENT_PRINTER; printOptions.PaperOrientation = PaperOrientation.DefaultPaperOrientation; printOptions.PaperSize = PaperSize.PaperA4; printOptions.PrinterDuplex = PrinterDuplex.Default; printOptions.PaperSource = PaperSource.Auto; } Any help will be greatly appreciated. Craigo ** Vision - The ability to look further than your eyes can see :suss:

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      Have you considered just setting PrintOptions.PrinterName to an empty string (string.Empty, or just "") to use the default printer? In lieu of presenting the user with printer options this would provide a consistent user experience - print to the printer they selected as the default. Based on the documentation it doesn't seem like you can refer to an LPT port. If you want to find out the printer name on a specific LPT port, you could P/Invoke EnumPrinters http://msdn.microsoft.com/library/en-us/gdi/prntspol_9fjn.asp[^] and enumerate the printers till you find one installed on LPT_x_: (keeping in mind that a printer can be on more than one LPT port, so in that case a comma-delimited list is returned so be sure to parse that as well). This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Customer Product-lifecycle Experience Microsoft [My Articles] [My Blog]

      C 1 Reply Last reply
      0
      • H Heath Stewart

        Have you considered just setting PrintOptions.PrinterName to an empty string (string.Empty, or just "") to use the default printer? In lieu of presenting the user with printer options this would provide a consistent user experience - print to the printer they selected as the default. Based on the documentation it doesn't seem like you can refer to an LPT port. If you want to find out the printer name on a specific LPT port, you could P/Invoke EnumPrinters http://msdn.microsoft.com/library/en-us/gdi/prntspol_9fjn.asp[^] and enumerate the printers till you find one installed on LPT_x_: (keeping in mind that a printer can be on more than one LPT port, so in that case a comma-delimited list is returned so be sure to parse that as well). This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Customer Product-lifecycle Experience Microsoft [My Articles] [My Blog]

        C Offline
        C Offline
        Craig G Fraser
        wrote on last edited by
        #3

        Thanx for the quick reply Heath. I can't use an empty string for the printer name because (and i should have mentioned this in my origional post) there are 2 printers connected to the PC and i have to print a separate report to each of them. The one printer has pre-printed stationary and the other is plain paper - so I have to implicitly send the report to the correct printer. I will look at the EnumPrinters solution you have suggested. This may sound like a really stupid question but do all printers that are connected to a PC have a name, even if it is not on a network? Thanx again for your help. Cheers Craigo

        H 1 Reply Last reply
        0
        • C Craig G Fraser

          Thanx for the quick reply Heath. I can't use an empty string for the printer name because (and i should have mentioned this in my origional post) there are 2 printers connected to the PC and i have to print a separate report to each of them. The one printer has pre-printed stationary and the other is plain paper - so I have to implicitly send the report to the correct printer. I will look at the EnumPrinters solution you have suggested. This may sound like a really stupid question but do all printers that are connected to a PC have a name, even if it is not on a network? Thanx again for your help. Cheers Craigo

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          FWIW - though it may not be relevant to your problem - multi-tray printers will (or, at least, should) select the right tray based on the media in the tray.

          Craig G Fraser wrote:

          This may sound like a really stupid question but do all printers that are connected to a PC have a name, even if it is not on a network?

          Yes they do. Open your "Printers and Faxes" folder (or the equivalent on whatever OS you're running) and you'll see all the various names. These are the names of the printers as should be specified in PrintOptions.PrinterName. If the printers on the various machines (or just a single machine) are all the same and you require such a constraint, you could fix the printer names or, better yet, make them configurable in the application configuration file (yourexe.exe.config in the same folder as yourexe.exe). This way if a machine for some reason must use a different name you can configure it per-application/per-machine. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Customer Product-lifecycle Experience Microsoft [My Articles] [My Blog]

          C 1 Reply Last reply
          0
          • H Heath Stewart

            FWIW - though it may not be relevant to your problem - multi-tray printers will (or, at least, should) select the right tray based on the media in the tray.

            Craig G Fraser wrote:

            This may sound like a really stupid question but do all printers that are connected to a PC have a name, even if it is not on a network?

            Yes they do. Open your "Printers and Faxes" folder (or the equivalent on whatever OS you're running) and you'll see all the various names. These are the names of the printers as should be specified in PrintOptions.PrinterName. If the printers on the various machines (or just a single machine) are all the same and you require such a constraint, you could fix the printer names or, better yet, make them configurable in the application configuration file (yourexe.exe.config in the same folder as yourexe.exe). This way if a machine for some reason must use a different name you can configure it per-application/per-machine. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Customer Product-lifecycle Experience Microsoft [My Articles] [My Blog]

            C Offline
            C Offline
            Craig G Fraser
            wrote on last edited by
            #5

            Excellent..that sounds like the answer. In my app i had the LPT ports configurable in my app.config so i'll just change this to hold the printer name instead as you have suggested.

            Heath Stewart wrote:

            If you want to find out the printer name on a specific LPT port, you could P/Invoke EnumPrinters http://msdn.microsoft.com/library/en-us/gdi/prntspol\_9fjn.asp\[^\] and enumerate the printers till you find one installed on LPTx: (keeping in mind that a printer can be on more than one LPT port, so in that case a comma-delimited list is returned so be sure to parse that as well).

            Any chane you point me to some sample code that retrieves the printer names as mentioned above...I had a look at the link you sent but i'm afraid having not worked in this area a lot, it doesnt really make too much sense to me. Craigo

            H 1 Reply Last reply
            0
            • C Craig G Fraser

              Excellent..that sounds like the answer. In my app i had the LPT ports configurable in my app.config so i'll just change this to hold the printer name instead as you have suggested.

              Heath Stewart wrote:

              If you want to find out the printer name on a specific LPT port, you could P/Invoke EnumPrinters http://msdn.microsoft.com/library/en-us/gdi/prntspol\_9fjn.asp\[^\] and enumerate the printers till you find one installed on LPTx: (keeping in mind that a printer can be on more than one LPT port, so in that case a comma-delimited list is returned so be sure to parse that as well).

              Any chane you point me to some sample code that retrieves the printer names as mentioned above...I had a look at the link you sent but i'm afraid having not worked in this area a lot, it doesnt really make too much sense to me. Craigo

              H Offline
              H Offline
              Heath Stewart
              wrote on last edited by
              #6

              PInvoke.net[^] is an excellent resources for finding P/Invoke signatures. I myself add some new signatures from time to time. Specifically, you can find this one at http://pinvoke.net/default.aspx/winspool/EnumPrinters.html[^]. I haven't tested it but there is a sample so hopefully this will get you started. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Customer Product-lifecycle Experience Microsoft [My Articles] [My Blog]

              C 1 Reply Last reply
              0
              • H Heath Stewart

                PInvoke.net[^] is an excellent resources for finding P/Invoke signatures. I myself add some new signatures from time to time. Specifically, you can find this one at http://pinvoke.net/default.aspx/winspool/EnumPrinters.html[^]. I haven't tested it but there is a sample so hopefully this will get you started. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Customer Product-lifecycle Experience Microsoft [My Articles] [My Blog]

                C Offline
                C Offline
                Craig G Fraser
                wrote on last edited by
                #7

                Hi Heath, Thanx for the info....had a look and boy that stuff is really confusing for me....Havent worked with calls to unmaganged code before so struggling to get my head around it and how to implement it in my code. I have managed to sort out the problem by creating a class using System.Management functionality.....Not sure if this is the best way but it works so will do for now....Does my class below look ok or do you tink it will cause problems ? You really helped me to solve this so thanx a million...it really is appreciated.. Cheers Craig using System; using System.Management; using System.Collections; namespace VTG.Functions { public class PrintFunctions { public struct PrinterInfo { public string sPrinterName; public bool bShared; public string sSharedName; public string sServerName; public string sPortName; public bool bDefaultPrinter; } public static PrinterInfo[] GetPrintersCollection() { PrinterInfo[] pi = new PrinterInfo[10]; string searchQuery = "SELECT * FROM Win32_Printer"; ManagementObjectSearcher searchPrinters = new ManagementObjectSearcher(searchQuery); ManagementObjectCollection printerCollection = searchPrinters.Get(); int iPrinterIndex=0; foreach(ManagementObject printer in printerCollection) { pi[iPrinterIndex].sPrinterName=printer.Properties["Name"].Value.ToString(); pi[iPrinterIndex].bShared=Convert.ToBoolean(printer.Properties["Shared"].Value); if (pi[iPrinterIndex].bShared==true) { pi[iPrinterIndex].sSharedName=printer.Properties["ShareName"].Value.ToString(); pi[iPrinterIndex].sServerName=printer.Properties["ServerName"].Value.ToString(); } pi[iPrinterIndex].sPortName=printer.Properties["PortName"].Value.ToString(); pi[iPrinterIndex].bDefaultPrinter=Convert.ToBoolean(printer.Properties["Default"].Value); iPrinterIndex++; } return pi; } } }

                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