vb.net中声明createfile
正在进行的一个项目需要操作热敏打印机,为了客户使用的方便,支持各种类型的方式进行打印,今天需要使用LTP方式进行打印。打印过程中需要使用createfile,
首先我们在一个模块文件或者类文件中进行createfile的声明:
Public Structure SECURITY_ATTRIBUTES Dim nLength As Long Dim lpSecurityDescriptor As Long Dim bInheritHandle As Long End Structure Public Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Integer, ByVal dwShareMode As Integer, ByVal lpSecurityAttributes As SECURITY_ATTRIBUTES, ByVal dwCreationDisposition As Integer, ByVal dwFlagsAndAttributes As Integer, ByVal hTemplateFile As Integer) As Integer Public Const GENERIC_READ = &H80000000 Public Const GENERIC_WRITE = &H40000000 Public Const OPEN_EXISTING = 3 Public Const INVALID_HANDLE_VALUE = -1
注意前面的SECURITY_ATTRIBUTES的结构声明,如果没有这个会报未定义类型:SECURITY_ATTRIBUTES
在使用时我们参照下面vb.net代码:
Dim iHandle As IntPtr
Dim lps As SECURITY_ATTRIBUTES
iHandle = CreateFile("", GENERIC_READ Or GENERIC_WRITE, 0, lps, OPEN_EXISTING, 0, 0)
以上内容适用于.net framework老版本,新版本已经调整,使用方法如下:
<DllImport("kernel32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)> _
Public Function CreateFile(ByVal lpFileName As String, ByVal dwDesiredAccess As System.UInt32, ByVal dwShareMode As System.UInt32, ByVal lpSecurityAttributes As IntPtr, ByVal dwCreationDisposition As System.UInt32, ByVal dwFlagsAndAttributes As System.UInt32, ByVal hTemplateFile As IntPtr) As Microsoft.Win32.SafeHandles.SafeFileHandle
End Function
Public Const FILE_ATTRIBUTE_NORMAL As Short = &H80
Public Const INVALID_HANDLE_VALUE As Short = -1
Public Const GENERIC_READ As Long = &H80000000
Public Const GENERIC_WRITE As UInteger = &H40000000
Public Const CREATE_NEW As UInteger = 1
Public Const CREATE_ALWAYS As UInteger = 2
Public Const OPEN_EXISTING As UInteger = 3
Dim handleValue As Microsoft.Win32.SafeHandles.SafeFileHandle = Nothing
handleValue = CreateFile("", GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero)
Dim fs As New FileStream(handleValue, FileAccess.ReadWrite)
- vb.net调用java axis2生成的soap webservices(2015-04-03)
- vb.net的接口定义及使用(2015-02-21)
- vb.net中自定义结构structure的定义及使用(2015-02-17)
- vb.net的位移及与、或操作(2015-02-01)
- vb.net的char数组转成String(2015-02-01)
- vb.net char转integer与integer转char(2015-01-17)
- vb.net中实现java字符串的hashcode(2015-01-17)
- vb.net 、java、vb等获取guid的方法(2015-01-11)
- vb.net读取及写入文件的方法(2015-01-11)