Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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

Question by:ian_khoo_sc
  • 8
  • 5

Expert Comment

ID: 16307788
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"

Expert Comment

ID: 16307813
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

ID: 16307883
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.


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


Expert Comment

ID: 16308048
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

ID: 16504265
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?

Expert Comment

ID: 16504469
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

ID: 16504594
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?

Expert Comment

ID: 16504606
No, you can't.  Not completely.


Author Comment

ID: 16505191
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



Accepted Solution

DeadlyTrev earned 1000 total points
ID: 16505264
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

ID: 16505344
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???

Expert Comment

ID: 16505488
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.

Expert Comment

ID: 16603335
C ?  :-(

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

571 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