- 分享
- 0
- 人气
- 0
- 主题
- 57
- 帖子
- 1160
- UID
- 41984
- 积分
- 10187
- 阅读权限
- 25
- 注册时间
- 2006-8-23
- 最后登录
- 2024-6-21
- 在线时间
- 12198 小时
|
不用batch files的方法
Option Strict Off
Option Explicit On
Imports System.IO
Imports System.Runtime.InteropServices
Friend Class Form1Class Form1
Inherits System.Windows.Forms.Form
Public Const GENERIC_WRITE = &H40000000
Public Const OPEN_EXISTING = 3
Public Const FILE_SHARE_WRITE = &H2
Dim LPTPORT As String
Dim hPort As Integer
Public Declare Function CreateFile()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()Function CloseHandle Lib "kernel32" Alias "CloseHandle" (ByVal hObject As Integer) As Integer
Dim retval As Integer
<StructLayout(LayoutKind.Sequential)> Public Structure SECURITY_ATTRIBUTESStructure SECURITY_ATTRIBUTES
Private nLength As Integer
Private lpSecurityDescriptor As Integer
Private bInheritHandle As Integer
End Structure
Private Sub Command1_Click()Sub Command1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command1.Click
Dim SA As SECURITY_ATTRIBUTES
Dim outFile As FileStream, hPortP As IntPtr
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) 'Create FileStream using Handle
Dim fileWriter As New StreamWriter(outFile)
fileWriter.Write(Chr(7))
fileWriter.Flush()
fileWriter.Close()
outFile.Close()
retval = CloseHandle(hPort)
End Sub
End Class |
|