Printer driver source example

Posted on 1998-08-31
Last Modified: 2008-02-01
I need to create a printer driver to a generic text only printer. I would like a example source code and more inscructions ?
Question by:soares
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 1432056
You will not be able to create a printer driver from VB...

However, you could create a DLL that would send print to a specified printer, which could the the Windows Generic Text Printer driver.
LVL 14

Expert Comment

ID: 1432057
You can't create a printer driver.

Here, at work, we need to send postscript file to Xerox printer trough a generic text only driver, because we don't want interpretation. We had to modify a flag in the registry to do that, and print using API.

If it is that that you need, tell me, and I will post the code.


Expert Comment

ID: 1432058
If you want I can email you a generic text only printer driver.
Also tell me why do you need it.
I had some programs that needed a generic printer driver that are not common
and it works for me.

Author Comment

ID: 1432059
I want configure two generic text only driver in same machine with diferent setting of paper and fonts. But when I change the configuration of the first the second if automatically changed too. And don't want it !!! I want two generic printer driver in the same machine configured with diferent settings

LVL 14

Accepted Solution

waty earned 100 total points
ID: 1432060
The purpose here is sending postscript file without any interpretation.

We have added a generic Printer Text driver and set the following key to in the registry :
HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Print\Printers\voltest -> DataType = "RAW"

voltest is our printer name

When we need to set particular seetings on the printer, we add it in the postscript file.

I will work within 2 or 3 weeks on job tickets to set directly the printers.

NB : We have here 3 Xerox Docutech 6135 and we print a lot each days.

I use the following code to send to Xerox Docutech printer :
Option Explicit

' constants for DEVMODE structure
Public Const CCHDEVICENAME = 32
Public Const CCHFORMNAME = 32
Public Type DEVMODE
   dmDeviceName As String * CCHDEVICENAME
   dmSpecVersion As Integer
   dmDriverVersion As Integer
   dmSize As Integer
   dmDriverExtra As Integer
   dmFields As Long
   dmOrientation As Integer
   dmPaperSize As Integer
   dmPaperLength As Integer
   dmPaperWidth As Integer
   dmScale As Integer
   dmCopies As Integer
   dmDefaultSource As Integer
   dmPrintQuality As Integer
   dmColor As Integer
   dmDuplex As Integer
   dmYResolution As Integer
   dmTTOption As Integer
   dmCollate As Integer
   dmFormName As String * CCHFORMNAME
   dmLogPixels As Integer
   dmBitsPerPel As Long
   dmPelsWidth As Long
   dmPelsHeight As Long
   dmDisplayFlags As Long
   dmDisplayFrequency As Long
   dmICMMethod As Long        ' // Windows 95 only
   dmICMIntent As Long        ' // Windows 95 only
   dmMediaType As Long        ' // Windows 95 only
   dmDitherType As Long       ' // Windows 95 only
   dmReserved1 As Long        ' // Windows 95 only
   dmReserved2 As Long        ' // Windows 95 only
End Type
   pDatatype As Long
   pDevMode As DEVMODE
   DesiredAccess As Long
End Type

' constants for DesiredAccess member of PRINTER_DEFAULTS

' Win32 API Calls
Private Declare Function GetProfileString Lib "kernel32" Alias "GetProfileStringA" (ByVal lpAppName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long
Private Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName As String, phPrn As Long, pDefault As PRINTER_DEFAULTS) As Long
Private Declare Function StartDocPrinter Lib "winspool.drv" Alias "StartDocPrinterA" (ByVal hPrn As Long, ByVal Level As Long, pDocInfo As DOC_INFO_1) As Long
Private Declare Function StartPagePrinter Lib "winspool.drv" (ByVal hPrn As Long) As Long
Private Declare Function WritePrinter Lib "winspool.drv" (ByVal hPrn As Long, pBuf As Any, ByVal cdBuf As Long, pcWritten As Long) As Long
Private Declare Function EndPagePrinter Lib "winspool.drv" (ByVal hPrn As Long) As Long
Private Declare Function EndDocPrinter Lib "winspool.drv" (ByVal hPrn As Long) As Long
Private Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrn As Long) As Long
' Structure required by StartDocPrinter
Private Type DOC_INFO_1
   pDocName As String
   pOutputFile As String
   pDatatype As String
End Type

Public Sub SelectDefaultPrinter(Lst As ComboBox)
   Dim sRet As String
   Dim nRet As Integer
   Dim i As Integer
   ' Look for default printer in WIN.INI
   sRet = Space(255)
   nRet = GetProfileString("Windows", ByVal "device", "", _
                           sRet, Len(sRet))
   ' Truncate default printer name.
   If nRet Then
      sRet = UCase(Left(sRet, InStr(sRet, ",") - 1))
      ' Cycle list looking for matching entry.
      For i = 0 To Lst.ListCount
         If Left(UCase(Lst.List(i)), Len(sRet)) = sRet Then
            ' Found it. Set index and bail.
            Lst.ListIndex = i
            Exit For
         End If
      Next i
   End If
End Sub

Public Sub SpoolFile(sFile As String, PrnName As String, Optional AppName As String = "")
   Dim hPrn As Long
   Dim Buffer() As Byte
   Dim hFile As Integer
   Dim Written As Long
   Dim di As DOC_INFO_1
   Dim i As Long
   Const BufSize As Long = &H4000
   ' Extract filename from passed spec, and build job name.
   ' Fill remainder of DOC_INFO_1 structure.
   If InStr(sFile, "\") Then
      For i = Len(sFile) To 1 Step -1
         If Mid(sFile, i, 1) = "\" Then Exit For
         di.pDocName = Mid(sFile, i, 1) & di.pDocName
      Next i
      di.pDocName = sFile
   End If
   If Len(AppName) Then
      di.pDocName = AppName & ": " & di.pDocName
   End If
   di.pOutputFile = vbNullString
   di.pDatatype = "RAW"
   ' Open printer for output to obtain handle.
   ' Set it up to begin recieving raw data.
   ' set the PRINTER_DEFAULTS members
   pd.pDatatype = 0&
   pd.DesiredAccess = PRINTER_ALL_ACCESS
   Debug.Print OpenPrinter(PrnName, hPrn, pd)
   Call StartDocPrinter(hPrn, 1, di)
   Call StartPagePrinter(hPrn)
   ' Open file and pump it to the printer.
   hFile = FreeFile
   Open sFile For Binary Access Read As hFile
      ' Read in 16K buffers and spool.
      ReDim Buffer(1 To BufSize) As Byte
      For i = 1 To LOF(hFile) \ BufSize
         Get #hFile, , Buffer
         Call WritePrinter(hPrn, Buffer(1), BufSize, Written)
      Next i
      ' Get last chunk of file if it doesn't
      ' fit evenly into a 16K buffer.
      If LOF(hFile) Mod BufSize Then
         ReDim Buffer(1 To (LOF(hFile) Mod BufSize)) As Byte
         Get #hFile, , Buffer
         Call WritePrinter(hPrn, Buffer(1), UBound(Buffer), Written)
      End If
   Close #hFile
   ' Shut down spooling process.
   Call EndPagePrinter(hPrn)
   Call EndDocPrinter(hPrn)
   Call ClosePrinter(hPrn)
End Sub

Public Function IsFile(SpecIn As String) As Boolean
   Dim Attr As Byte
   ' Guard against bad SpecIn by ignoring errors.
   On Error Resume Next
   ' Get attribute of SpecIn.
   Attr = GetAttr(SpecIn)
   If Err = 0 Then
      ' No error, so something was found.
      ' If Directory attribute set, then not a file.
      If (Attr And vbDirectory) = vbDirectory Then
         IsFile = False
         IsFile = True
      End If
   End If
End Function


Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month7 days, 5 hours left to enroll

623 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