Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

printing directly to printer using CreateFile API function

Posted on 2004-10-29
4
Medium Priority
?
1,158 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
Comment
Question by:SaadDani
[X]
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
  • 2
  • 2
4 Comments
 

Author Comment

by:SaadDani
ID: 12442699
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:
Amritpal Singh earned 500 total points
ID: 12442893
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
ID: 12455861
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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

650 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