dixiematic
Technical User
I would like to use a WIN32 ReadFile(...) to read a large BMP file in VBa. For test purposes I am using a small text file, containing only 22 characters from A...Z.
In VBa I successfuly use a call CreateFile(..) and get a handle to my test file, I use a WIN call GetFileSize() to receive the length of the file but calling WIN ReadFile(...) crashes Excel.I do not see the reason.
This is a minimal code:
[Public Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, _
ByVal dwDesiredAccess As Long, _
ByVal dwShareMode As Long, _
ByVal nonsecure As Long, _
ByVal dwCreationDisposition As Long, _
ByVal dwFlagsAndAttributes As Long, _
ByVal hTemplateFile As Long) As Long
Public Declare Function ReadFile Lib "kernel32" Alias "CreateFileA", _
(ByVal hFile As Long, _
ByVal lpBuffer As Long, _
ByVal nNumberOfBytesToRead As Long, _
ByVal lpNumberOfBytesRead As Long, _
nonOverlapped As Long) As Boolean
Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Long, _
lpFileSizeHigh As Long) As Long
Sub MyTestFileRread()
hFile = CreateFile(MyTestFile, GENERIC_READ, FILE_SHARE_READ, nonsecure, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0): REM returns the handle
MyLength = GetFileSize(hFile, FileLength): REM correctly returns the length
ReadFileOK = ReadFile(hFile, lpBuffer, mylength, lpNumberOfBytesRead, 0): REM crash
CloseHandle (hcom)
End Sub]
Any suggestions?
M777182
In VBa I successfuly use a call CreateFile(..) and get a handle to my test file, I use a WIN call GetFileSize() to receive the length of the file but calling WIN ReadFile(...) crashes Excel.I do not see the reason.
This is a minimal code:
[Public Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, _
ByVal dwDesiredAccess As Long, _
ByVal dwShareMode As Long, _
ByVal nonsecure As Long, _
ByVal dwCreationDisposition As Long, _
ByVal dwFlagsAndAttributes As Long, _
ByVal hTemplateFile As Long) As Long
Public Declare Function ReadFile Lib "kernel32" Alias "CreateFileA", _
(ByVal hFile As Long, _
ByVal lpBuffer As Long, _
ByVal nNumberOfBytesToRead As Long, _
ByVal lpNumberOfBytesRead As Long, _
nonOverlapped As Long) As Boolean
Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Long, _
lpFileSizeHigh As Long) As Long
Sub MyTestFileRread()
hFile = CreateFile(MyTestFile, GENERIC_READ, FILE_SHARE_READ, nonsecure, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0): REM returns the handle
MyLength = GetFileSize(hFile, FileLength): REM correctly returns the length
ReadFileOK = ReadFile(hFile, lpBuffer, mylength, lpNumberOfBytesRead, 0): REM crash
CloseHandle (hcom)
End Sub]
Any suggestions?
M777182