• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1254
  • Last Modified:

printing directly to printer using CreateFile API function

I need to ptint to a printer without any driver
Can you send me a code sample that print directly to a printer using the API functions CreateFile and WriteFile...
0
SaadDani
Asked:
SaadDani
  • 2
  • 2
1 Solution
 
SaadDaniAuthor Commented:
I need a sample of an actual code...
I already know the definition of the functions but don't know how to use it
0
 
Amritpal SinghCommented:
hi

following code is in VB.Net ,but i think it'll be helpful


  Public Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" ( _
  ByVal lpFileName As String, ByVal dwDesiredAccess As Integer, _
  ByVal dwShareMode As Integer, _
  <MarshalAs(UnmanagedType.Struct)> ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES, _
  ByVal dwCreationDisposition As Integer, ByVal dwFlagsAndAttributes As Integer, _
  ByVal hTemplateFile As Integer) As Integer

  Public Declare Function CloseHandle Lib "kernel32" Alias "CloseHandle" (ByVal hObject As Integer) As Integer
  Dim retval As Integer

  <StructLayout(LayoutKind.Sequential)> Public Structure SECURITY_ATTRIBUTES
    Private nLength As Integer
    Private lpSecurityDescriptor As Integer
    Private bInheritHandle As Integer
  End Structure

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim Texxxt As String
    Dim SA As SECURITY_ATTRIBUTES
    Dim outFile As FileStream, hPortP As IntPtr

    LPTPORT = "LPT1"
    Texxxt = Me.RichTextBox1.Text
    hPort = CreateFile(LPTPORT, GENERIC_WRITE, FILE_SHARE_WRITE, SA, OPEN_EXISTING, 0, 0)

    hPortP = New IntPtr(hPort) 'convert Integer to IntPtr
    outFile = New FileStream(hPortP, FileAccess.Write, False) 'Create FileStream using Handle

    Dim fileWriter As New StreamWriter(outFile)
    'MessageBox.Show(RichTextBox1.Text)
    ' fileWriter.AutoFlush = False
    fileWriter.WriteLine(RichTextBox1.Text)
    fileWriter.WriteLine("Hello World2")
    fileWriter.WriteLine("Hello World1")
    fileWriter.WriteLine("Hello World2")
    fileWriter.Write(Chr(12)) '12
    fileWriter.Flush()
    fileWriter.Close()
    outFile.Close()

    retval = CloseHandle(hPort)
  End Sub
End Class
0
 
SaadDaniAuthor Commented:
I am trying this code in VB6:

Option Compare Database
Option Explicit

Public Const GENERIC_WRITE = &H40000000
Public Const GENERIC_READ = &H80000000
Public Const OPEN_EXISTING = 3
Public Const FILE_SHARE_WRITE = &H2

Public Type SECURITY_ATTRIBUTES
        nLength As Long
        lpSecurityDescriptor As Long
        bInheritHandle As Long
End Type

Public Type OVERLAPPED
        Internal As Long
        InternalHigh As Long
        offset As Long
        OffsetHigh As Long
        hEvent As Long
End Type

Dim sec As SECURITY_ATTRIBUTES
Dim over As OVERLAPPED

Public Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" ( _
                        ByVal lpFileName As String, _
                        ByVal dwDesiredAccess As Long, _
                        ByVal dwShareMode As Long, _
                        lpSecurityAttributes As SECURITY_ATTRIBUTES, _
                        ByVal dwCreationDisposition As Long, _
                        ByVal dwFlagsAndAttributes As Long, _
                        ByVal hTemplateFile As Long) As Long

Public Declare Function WriteFile Lib "kernel32" ( _
                        ByVal hFile As Long, _
                        lpBuffer As Any, _
                        ByVal nNumberOfBytesToWrite As Long, _
                        lpNumberOfBytesWritten As Long, _
                        lpOverlapped As OVERLAPPED) As Long

Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long


Public Sub aaa()
    Dim lngHandl As Long
    Dim lng2 As Long
    Dim myStr As String
    Dim nChar As Long
    Dim sa As SECURITY_ATTRIBUTES
    Dim over As OVERLAPPED
   
    lngHandl = CreateFile("LPT1", GENERIC_WRITE, FILE_SHARE_WRITE, sa, OPEN_EXISTING, 0, 0)
    'lngHandl = CreateFile("C:\MyFile", GENERIC_WRITE, FILE_SHARE_WRITE, sa, OPEN_EXISTING, 0, 0)
   
    myStr = "hello world"
    lng2 = WriteFile(lngHandl, myStr, Len(myStr), nChar, over)
       
    lng2 = CloseHandle(lngHandl)
End Sub

without result.
lngHandl get the value -1
also I tried it to write to a file - not printer (C:\MyFile), also without result.
What is wrong?
0
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.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now