# 使用 File API

Posted by 小吴 on October 06, 1998 at 08:18:03:

In Reply to: 请教各位网友：如何在vb6.0中向epson-1900k打印机发送打印机控制命令 posted by John on October 05, 1998 at 16:23:06:

你可以使用 FileAPI 来开始 printer port (LPT1, LPT2, or PRN)，这样就可以直接输出到打印机了。

Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long
Declare Function lclose Lib "kernel32" Alias "_lclose" (ByVal hFile As Long) As Long
Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, lpOverlapped As Any) As Long
Public Const OFS_MAXPATHNAME = 128
Public Const OF_WRITE = &H1
Type OFSTRUCT
cBytes As Byte
fFixedDisk As Byte
nErrCode As Integer
Reserved1 As Integer
Reserved2 As Integer
szPathName(OFS_MAXPATHNAME) As Byte
End Type

Private Sub Test()
Dim hFile As Integer
Dim strPath As String
Dim strucFname As OFSTRUCT
Dim strhFiles As String
Dim strPort As String
Dim bytData() As Byte
Dim lrc As Long
Dim lngBytesWritten As Long
strPort = "LPT1"
hFile = OpenFile(strPort, strucFname, OF_WRITE)
If hFile = -1 Then
MsgBox "开启印表机埠发生错误", vbExclamation
Else
Open txtFilename For Binary As #1
bytData = InputB(LOF(1), 1)
lrc = WriteFile(hFile, bytData(0), LOF(1), lngBytesWritten, ByVal 0&)
Close #1
End If
lclose hFile
End Sub

Follow Ups: