FileOpen(2, "lpt1", OpenMode.Binary)

  • Thread starter Thread starter Howard Kaikow
  • Start date Start date
H

Howard Kaikow

I upgraded a VB 6 project.

When I tried executing, I got error that "FileStream will not open Win32
devices such as disk partitions and tape drives".
How am I supposed to copy to the printer in binary mode?
 
Hi ,
You would need to use the CreatFile API and then use the handled returned
by this call in a FileStream constructor that takesn an handle. Here is the
code for it, basically create a windows app and add a button to it and past
the following code
Imports System.IO

Imports System.Runtime.InteropServices



Public Class Form1

Inherits System.Windows.Forms.Form



#Region " Windows Form Designer generated code "



Public Sub New()

MyBase.New()



'This call is required by the Windows Form Designer.

InitializeComponent()



'Add any initialization after the InitializeComponent() call



End Sub



'Form overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Dispose()

End If

End If

MyBase.Dispose(disposing)

End Sub



'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer



'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

Friend WithEvents Button1 As System.Windows.Forms.Button

<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

Me.Button1 = New System.Windows.Forms.Button()

Me.SuspendLayout()

'

'Button1

'

Me.Button1.Location = New System.Drawing.Point(52, 84)

Me.Button1.Name = "Button1"

Me.Button1.Size = New System.Drawing.Size(120, 36)

Me.Button1.TabIndex = 1

Me.Button1.Text = "Print Test"

'

'Form1

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(260, 166)

Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1})

Me.Name = "Form1"

Me.Text = "Form1"

Me.ResumeLayout(False)



End Sub



#End Region



Public Const GENERIC_WRITE = &H40000000

Public Const OPEN_EXISTING = 3

Public Const FILE_SHARE_WRITE = &H2



Dim LPTPORT As String

Dim hPort As Integer, hPortP As IntPtr

Dim retval As Integer



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



<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

LPTPORT = "LPT1"

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)

fileWriter.WriteLine("First test line.")

fileWriter.WriteLine("Second test line.")

fileWriter.WriteLine("Third and final test line.")

fileWriter.Write(Chr(12)) 'FormFeed Character

fileWriter.Flush()

fileWriter.Close()

outFile.Close()



retval = CloseHandle(hPort)

End Sub

End Class




Anand Balasubramanian
Microsoft, Visual Basic .NET

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks
 
Finally got around to trying the code.
Worked as expected.

There should be a Framework method to send a stream of bytes to ANY device,
without needing to use the API.
 
Back
Top