主题:[讨论]CreateFile的lpSecurityAttributes提示ByRef参数类型不符
我试图用VB编写串口程序,可CreateFile的lpSecurityAttributes参数总提示我“ByRef参数类型不符”,下面是我的代码:
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function GetCommState Lib "kernel32" (ByVal nCid As Long, lpDCB As DCB) As Long
Private Declare Function SetCommState Lib "kernel32" (ByVal hCommDev As Long, lpDCB As DCB) As Long
Private Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, lpOverlapped As OVERLAPPED) As Long
Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, lpOverlapped As OVERLAPPED) As Long
Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Private Type DCB
DCBlength As Long
BaudRate As Long
fBitFields As Long 'See Comments in Win32API.Txt
wReserved As Integer
XonLim As Integer
XoffLim As Integer
ByteSize As Byte
Parity As Byte
StopBits As Byte
XonChar As Byte
XoffChar As Byte
ErrorChar As Byte
EofChar As Byte
EvtChar As Byte
wReserved1 As Integer 'Reserved; Do Not Use
End Type
Private Type OVERLAPPED
Internal As Long
InternalHigh As Long
offset As Long
OffsetHigh As Long
hEvent As Long
End Type
Private Const GENERIC_READ = &H80000000
Private Const GENERIC_WRITE = &H40000000
Private Const OPEN_EXISTING = 3
Private Com1_h As Long
Private Sub Form_Load()
Dim DCB0 As DCB
Com1_h = CreateFile("com1", (GENERIC_READ Or GENERIC_WRITE), 0, ByVal 0&, OPEN_EXISTING, 0, 0)
GetCommState Com1_h, DCB0
DCB0.BaudRate = 4800
DCB0.ByteSize = 8
DCB0.Parity = 1
SetCommState Com1_h, DCB0
End Sub
我查阅了网上的资料,尝试了很多方法:
1、在声明的lpSecurityAttributes前加上ByVal
2、将CreateFile的第四个参数改为ByVal 0&
3、将CreateFile的第四个参数改为Null
这3个方法都没有用,VB会提示我:“用户定义类型不能用”
我使用的是VB 6.0(请不要要求我改用高版本,也不要要求我用MSComm控件,因为目前的实现要求就是在这个环境下用Windows API实现)
请问,该如何解决这个问题,谢谢!
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function GetCommState Lib "kernel32" (ByVal nCid As Long, lpDCB As DCB) As Long
Private Declare Function SetCommState Lib "kernel32" (ByVal hCommDev As Long, lpDCB As DCB) As Long
Private Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, lpOverlapped As OVERLAPPED) As Long
Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, lpOverlapped As OVERLAPPED) As Long
Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Private Type DCB
DCBlength As Long
BaudRate As Long
fBitFields As Long 'See Comments in Win32API.Txt
wReserved As Integer
XonLim As Integer
XoffLim As Integer
ByteSize As Byte
Parity As Byte
StopBits As Byte
XonChar As Byte
XoffChar As Byte
ErrorChar As Byte
EofChar As Byte
EvtChar As Byte
wReserved1 As Integer 'Reserved; Do Not Use
End Type
Private Type OVERLAPPED
Internal As Long
InternalHigh As Long
offset As Long
OffsetHigh As Long
hEvent As Long
End Type
Private Const GENERIC_READ = &H80000000
Private Const GENERIC_WRITE = &H40000000
Private Const OPEN_EXISTING = 3
Private Com1_h As Long
Private Sub Form_Load()
Dim DCB0 As DCB
Com1_h = CreateFile("com1", (GENERIC_READ Or GENERIC_WRITE), 0, ByVal 0&, OPEN_EXISTING, 0, 0)
GetCommState Com1_h, DCB0
DCB0.BaudRate = 4800
DCB0.ByteSize = 8
DCB0.Parity = 1
SetCommState Com1_h, DCB0
End Sub
我查阅了网上的资料,尝试了很多方法:
1、在声明的lpSecurityAttributes前加上ByVal
2、将CreateFile的第四个参数改为ByVal 0&
3、将CreateFile的第四个参数改为Null
这3个方法都没有用,VB会提示我:“用户定义类型不能用”
我使用的是VB 6.0(请不要要求我改用高版本,也不要要求我用MSComm控件,因为目前的实现要求就是在这个环境下用Windows API实现)
请问,该如何解决这个问题,谢谢!