Option Explicit
' Constants
Private Const GENERIC_READ = &H80000000
Private Const OPEN_EXISTING = 3
Private Const OPEN_ALWAYS = 4
Private Const FILE_SHARE_READ = &H1
Private Const FILE_SHARE_WRITE = &H2
Private Const FILE_ATTRIBUTE_NORMAL = &H80
Private Const PATH_FILE = "\\winever\testshare\test.txt"
' Functions
Private Declare Function CreateFile Lib "kernel32" _
Alias "CreateFileA" _
(ByVal lpFileName As String, ByVal dwDesiredAccess As Long, _
ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, _
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 ReadFile Lib "kernel32" _
(ByVal hFile As Long, _
lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, _
lpNumberOfBytesRead As Long, ByVal lpOverlapped As Long) As Long
Private Sub Command1_Click()
Open PATH_FILE For Binary As #1
Close #1 ' Run-time error '75': Path/File access error
End Sub
Private Sub Command2_Click()
Dim hFileHandle As Long
Dim bBuffer(10) As Byte
Dim lResult As Long
Dim i As Integer
' Open the file.
' !!! Be sure to modify PATH_FILE to your servers name!
hFileHandle = CreateFile(PATH_FILE, GENERIC_READ, 0&, 0&, _
OPEN_EXISTING, 0&, 0&)
' Check filehandle to see if we get an error back, a value of -1
' indicates an error.
MsgBox (hFileHandle)
' Read the first 10 bytes of the file.
lResult = ReadFile(hFileHandle, bBuffer(0), 10, cBytesRead, 0)
' Write the 10 bytes to the immediate pane.
For i = 0 To UBound(bBuffer)
Debug.Print bBuffer(i); " ";
Next i
Debug.Print
Debug.Print StrConv(bBuffer, vbUnicode)
' Close the file.
CloseHandle (hFileHandle)
End Sub
Private Sub Form_Load()
Command1.Caption = "Show Error"
Command2.Caption = "Workaround"
End Sub