We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now


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

ian_khoo_sc asked
Medium Priority
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.

Watch Question

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"
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"


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.

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 ...


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?
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.


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?
No, you can't.  Not completely.


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


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.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


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???
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.
C ?  :-(
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.