Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A call of WIN32 ReadFile in VBa crashes Excel

Status
Not open for further replies.

dixiematic

Technical User
Oct 14, 2008
37
SI
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
 
Where and how is lpBuffer defined/initialized ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks,PHV for your answer.I declared all the variables that appear within a code in front of the Win functions declarations, but I did not want to bother with declarations. So lpBuffer is declared as "Public lpBuffer As Long". I forgot to mention it is all about Excel 2007 under WinXP.
M777182
 
Again, Where and how is lpBuffer defined/[!]initialized ?[/!]

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hello, PHV
There is an Option Explicit on the top of my module so that all variables are declared and a value zero is assigned to the lpBuffer initially. As I understand the call ReadFile should return some nonzero value of the lpBuffer. If I should assign some initial value to the lpBuffer I have no idea how to do it. There are not many examples for nonoverlapped reading on the http. Can you give me a hint?
M777182
 
A starting point:
Code:
...
Dim Dim sReadBuffer As String
...
Sub MyTestFileRread()
hFile = CreateFile(MyTestFile, GENERIC_READ, FILE_SHARE_READ, nonsecure, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
MyLength = GetFileSize(hFile, FileLength)
sReadBuffer = String$(MyLength, 0)
ReadFileOK = ReadFile(hFile, sReadBuffer, MyLength, lpNumberOfBytesRead, 0)
CloseHandle (hcom)
End Sub

don't forget to replace this:
ByVal lpBuffer As Long, _
with this:
ByVal lpBuffer As Any, _

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hello PHV,
thanks for immediate response.I've done it as you wrote, but it did not worked. There was a message"Excel Office enccountered a problem and will be shut down"- as always.
I'll try to transfer the code to my home PC.
M777182
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top