A VB program that print text-base file to a default printer. The vb program will be triggered from dos program

Posted on 2006-03-27
Last Modified: 2008-01-09
My Company uses an old DOS type application for its most critical functions. The way the program is written, it will only print to LPT1. There are basecally 2 type of printing.

a) Text-based report
b) Barcode label

We are planning to replace DOS Workstation with Microsoft Remote Desktop Client. For printing from DOS, we plan to write a VB program to redirect the text -base report and barcode label to a default printer.

We need help on the following:
1) Do you have any sample VB Program to print a text-base file to the default printer?
2) For barcode label printing, the barcode is controlled by DOS program thru ASCII control codes (escape sequesce). Do you have a printer driver that will not add or remove any of these control codes? So that, to the barcode printer, it is codes if it is connected directly to the LPT port.

Question by:ian_khoo_sc
    LVL 6

    Expert Comment

    You can print by executing your old dos apps, or by using the DOS print command, or by copying data directly to LPT1.

    1) VB6 program to print a text file using the VB Printer object

        Dim s As String
        Dim f As Integer
        f = FreeFile
        Open "filename.txt" For Input As #f
        Line Input #f, s
        Do While Not EOF(f)
            Printer.Print s
            Line Input #f, s
        Close #f
        Printer.Print s

    2)  VB6 program to print a binary file of escape codes using DOS copy command

        Shell "cmd.exe /c copy /b " & "filename.txt" & " lpt1"

    or on the ancient Win9x/ME

        Shell "command /c copy /b " & "filename.txt" & " lpt1"
    LVL 6

    Expert Comment

    Sorry, apologies.  This is the .NET area  :)

    2) VB.NET program to print a binary file of escape codes using DOS copy command

            Dim proc As Process = New Process
            proc.StartInfo.FileName = "cmd.exe"
            proc.StartInfo.Arguments = "/c copy /b " & "filename.dat" & " lpt"


    Author Comment

    Comment on Q1:

    We try the same codes before. We managed to print to the printer but, for whatever reasons, the output per line got truncated for those beyond 110 characters..  Do you know the reason why?

    Comment on Q2:

    We are running Remote Desktop on Windows 2003 Terminal Server. All the DOS sessions shared the same LPT1 port, but they need to print to different printers. So, we cannot print direct to LPT1. To overcome this problem, we output DOS printer output to a file before sending to the default printer queue.  The issue here is that the printer queue will alter the escape codes.

    LVL 6

    Expert Comment

    Q1.  Is 110 characters the carriage width of the printer?

    Q2.  You will need to get involved in the API then.  Have a look at the RawPrintHelper class in the solution below ...

    Author Comment

    Can anyone here help for the Raw data Printing?
    I need to read data from a file and send it a barcode printer.
    How do i specify the printer or channel it to a default printer?
    LVL 6

    Expert Comment

    The command below will do what you want to a locally connected printer on LPT1
        copy /b filename.dat lpt1
    unfortunately you can't send raw data to a printer share like ....
        copy /b filename.dat \\machinename\printername

    Could you remotely execute the  copy /b filename.dat lpt1  command on the machine which has the actual printer.  Or, assuming the barcode printer is connected to LPT1 on the Windows Server write a small service or daemon that your VB6 client apps can call or trigger.

    eg.  Create a app that monitors a shared folder for a particular file.  When the file appears the app will do a  copy /b partifular_file.dat LPT1 to send it directly to the printer on LPT1, then delete it.  Install that app and leave it running on the machine that the barcode printer is connected to.  Now your VB client program, when it wants to print, just needs to copy the file into the shared folder.  The daemon app will pick it up and print it.

    Author Comment

    As we are using Terminal Server/Client, the client was no access to LPT1 directly.
    So can we send the file to a default printer queue and by pass the setting and formating by the printer driver?
    LVL 6

    Expert Comment

    No, you can't.  Not completely.


    Author Comment

    I am trying to use the code below to send  Send Raw Data to a Printer Using the Win32 API from Visual Basic. I get the code from microsoft support website. When i execute the VB program, i was able to see "AAAAAA" being sent to my printer queue but it wasn't print out actually.

    I did tried to replace the "AAAAAA" to a file name, it is not working as well.
    Anyone here can advise about this part of the code? --> (MyDocInfo.pDocName = "AAAAAA")

    many many thanks

          Private Type DOCINFO
              pDocName As String
              pOutputFile As String
              pDatatype As String
          End Type
          Private Declare Function ClosePrinter Lib "winspool.drv" (ByVal _
             hPrinter As Long) As Long
          Private Declare Function EndDocPrinter Lib "winspool.drv" (ByVal _
             hPrinter As Long) As Long
          Private Declare Function EndPagePrinter Lib "winspool.drv" (ByVal _
             hPrinter As Long) As Long
          Private Declare Function OpenPrinter Lib "winspool.drv" Alias _
             "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, _
              ByVal pDefault As Long) As Long
          Private Declare Function StartDocPrinter Lib "winspool.drv" Alias _
             "StartDocPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, _
             pDocInfo As DOCINFO) As Long
          Private Declare Function StartPagePrinter Lib "winspool.drv" (ByVal _
             hPrinter As Long) As Long
          Private Declare Function WritePrinter Lib "winspool.drv" (ByVal _
             hPrinter As Long, pBuf As Any, ByVal cdBuf As Long, _
             pcWritten As Long) As Long

          Private Sub Command1_Click()
              Dim lhPrinter As Long
              Dim lReturn As Long
              Dim lpcWritten As Long
              Dim lDoc As Long
              Dim sWrittenData As String
              Dim MyDocInfo As DOCINFO
              lReturn = OpenPrinter(Printer.DeviceName, lhPrinter, 0)
              If lReturn = 0 Then
                  MsgBox "The Printer Name you typed wasn't recognized."
                  Exit Sub
              End If
              MyDocInfo.pDocName = "AAAAAA"
              MyDocInfo.pOutputFile = vbNullString
              MyDocInfo.pDatatype = vbNullString
              lDoc = StartDocPrinter(lhPrinter, 1, MyDocInfo)
              Call StartPagePrinter(lhPrinter)
             sWrittenData = "How's that for Magic !!!!" & vbFormFeed
              lReturn = WritePrinter(lhPrinter, ByVal sWrittenData, _
                 Len(sWrittenData), lpcWritten)
              lReturn = EndPagePrinter(lhPrinter)
              lReturn = EndDocPrinter(lhPrinter)
              lReturn = ClosePrinter(lhPrinter)
          End Sub


    LVL 6

    Accepted Solution

    the pDocName property provides a 'friendly' name for the spooler to label your print job, that's all.  It can be set to any value you like.

    Yes ... this is the method for writing directly to the printer using the Win32 API, but as I think you've already discovered, in most cases the spooler still interferes with the data you're sending.


    Author Comment

    Ok... I did not send binary data yet, i was sending a string to the printer. Why it appeared in the printer queue but nothing get printed out???
    LVL 6

    Expert Comment

    That string is just a name for the printer queue ... it doesn't get printed.

    The WritePrinter function is what sends data to the printer ...   The printer would have printed "How's that for Magic !!!!" if everything was okay.

    Also, I think    ByVal sWrittenData     in your writeprinter line should perhaps be     ByRef sWrittenData.

    Try creating a byte array and loading it with your binary data, then send the array instead of the sWrittenData string.
    LVL 6

    Expert Comment

    C ?  :-(

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
    Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
    This video discusses moving either the default database or any database to a new volume.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now