Solved

printing directly to printer using CreateFile API function

Posted on 2004-10-29
1,021 Views
Last Modified: 2008-01-16
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
Question by:SaadDani
    4 Comments
     
    LVL 6

    Expert Comment

    by:Amritpal Singh
    0
     

    Author Comment

    by:SaadDani
    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
     
    LVL 6

    Accepted Solution

    by:
    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
     

    Author Comment

    by:SaadDani
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
    I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

    875 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now