During development of an application that uses the parallel port for
communications if I send data when the port on the connected device has an
error I get an error that is handled with a try-catch-finally block. I can
then clear the error on the device and successfully send data to it. But if I
close the application after handling the error I get the following:
An unhandled exception of type 'System.IO.IOException' occurred in
mscorlib.dll
Additional information: The handle is invalid.
This occurs if I close with the exit function in the menu or if I click the
'X' in the form.
Here is the code that handles parallel port writing:
Public Sub ParallelSend(ByVal DataToSend As String)
'This subroutine prints data to the parallel port.
Dim SA As SECURITY_ATTRIBUTES
Dim outFile As FileStream, hPortP As IntPtr
Try
hPort = CreateFile(LPTPORT, GENERIC_WRITE, FILE_SHARE_WRITE, SA,
OPEN_EXISTING, 0, 0)
hPortP = New IntPtr(hPort) 'convert Int16 to IntPtr
outFile = New FileStream(hPortP, FileAccess.Write, False)
'Create FileStream using Handle
Dim fileWriter As New StreamWriter(outFile)
fileWriter.AutoFlush = False
fileWriter.WriteLine(DataToSend)
fileWriter.Flush()
fileWriter.Close()
outFile.Close()
retval = CloseHandle(hPort)
Catch ex As System.IO.IOException
MsgBox("Parallel Port Error" & vbCrLf & "Check printer and
connection", MsgBoxStyle.Critical, "Parallel Port Error")
Finally
If hPort <> -1 Then
retval = CloseHandle(hPort)
End If
End Try
End Sub
Any information would be appreciated.
Butch