Printer driver source example

I need to create a printer driver to a generic text only printer. I would like a example source code and more inscructions ?
Who is Participating?
watyConnect With a Mentor Commented:
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

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

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.
soaresAuthor Commented:
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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.