Solved

Application Error crashes the program

Posted on 2000-03-09
8
177 Views
Last Modified: 2010-05-02
Hi,


I still have my Application Error when calling the Win32 ReadFile function. Below is my code. The WriteFile works well, and the file written to is correct.
So, whats wrong.

Public Declare Function ReadFile Lib "kernel32" Alias "ReadFile" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, lpOverlapped As OVERLAPPED) As Long
Private Type Record   ' Define user-defined type.
   ID As Integer
   LD As Long
   Name As String * 20
   Surname As String
End Type


Sub ReadBinary()
    Dim handle, res, bytesRead, length As Long
    Dim nul As Variant
    Dim sa As SECURITY_ATTRIBUTES
    Dim ov As OVERLAPPED
    Dim txt As String
    Dim buffer As String * 10
   
    Dim MyRecord As Record   ' Declare variable.
   
    sa.nLength = 0
    sa.lpSecurityDescriptor = 0
    sa.bInheritHandle = 0
   
    ov.Internal = 0
    ov.InternalHigh = 0
    ov.offset = 0
    ov.OffsetHigh = 0
    ov.hEvent = 0
   
    handle = CreateFile("c:\bas.bin", _
                        GENERIC_READ Or GENERIC_WRITE, _
                        0, _
                        sa, _
                        OPEN_EXISTING, _
                        0, _
                        0)
    If handle = INVALID_HANDLE_VALUE Then
        Win32Error "CreateFile", GetLastError
    Else
        length = Len(buffer)
        res = ReadFile(handle, buffer, length, bytesRead, vbNull)
' below the old code, which also does not work
'        length = Len(MyRecord)
'        res = ReadFile(handle, MyRecord, length, bytesRead, vbNull)
        If res = 0 Then
            Win32Error "ReadFile", GetLastError
        res = CloseHandle(handle)
        If res = 0 Then
            Win32Error "CloseHandle", GetLastError
        End If
    End If
                       
    txt = "MyRecord: Id=" & MyRecord.ID & " LD=" & MyRecord.LD & " name=" & MyRecord.Name & " Surname=" & MyRecord.Surname
    MsgBox txt, vbInformation, "ReadBinary"
End Sub

0
Comment
Question by:poupou
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 1

Expert Comment

by:prozak
ID: 2600864
Initially I think that it may be because bytesRead is not declared anywhere which would make it of type variant. Try using Option Explicit at the top of your module.
0
 
LVL 1

Expert Comment

by:PatrickVD
ID: 2600946
Hello,

I think you better chang your function call to ReadFile like this

res = ReadFile(handle, BYVAL buffer, length, bytesRead, CLng(0))

You must add the Byval keyword before passing the 'String' buffer to the ReadFile API function !

Have a look at this link, it's all explained and there is also a 'Visual Basic Specific' section on the page !!
http://www.vbapi.com/ref/r/readfile.html

Hope this will work.

Patrick.
0
 
LVL 1

Accepted Solution

by:
PatrickVD earned 50 total points
ID: 2600988
Hello,

I think you better change your function call to ReadFile like this:

res = ReadFile(handle, BYVAL buffer, length, bytesRead, CLng(0))

You must add the Byval keyword before passing the 'String' buffer to the ReadFile API function !
I don't this you can use the vbNull as last parameter either. If you don't want the overlapped functionality, then you should use CLng(0) instead.

Have a look at this link, it's all explained and there is also a 'Visual Basic Specific' section on the page !!
http://www.vbapi.com/ref/r/readfile.html

Give this a try.
Hope this will work.

Patrick.
0
 
LVL 1

Expert Comment

by:prozak
ID: 2601050
Oh yeah, I didn't see that. Definitely listen to PatrickVD, I'm sure that's it (although you should also check bytesRead but I'd give the points to PatrickVD).
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 14

Expert Comment

by:wsh2
ID: 2602148
Nice code.. but for the life of me I can't understand why you are using a Readfile API rather than just a simple VB Open Binary - Get - Put routine. What am I missing here?
0
 

Expert Comment

by:RoboRob
ID: 2603272

A few notes that may help:

1. The way you declare your Long variables is incorrect and will result in all but one of them being defined as Variants. In VB, you can't define a list of variables as a certain data type like you can in C++. Define each one individually. ("Dim handle As Long", "Dim res As Long", etc.)

2. Change your ReadFile API declaration's lpOverlapped argument to "ByVal lpOverlapped As Long" instead of "lpOverlapped As OVERLAPPED". Pass a value of zero to the function for this argument instead of VbNull (which has a value of 1).

Finally, like wsh2 I have to wonder why you're going through the trouble of using the ReadFile function instead of using regular Binary file access functions.

Good luck,
Rob

0
 
LVL 14

Expert Comment

by:wsh2
ID: 2603290
<Smiling, batting eyelashes and speaking in sultry southern voice>.. "C'mon poupou,".. <wink> <wink>.. "Enquiring Expert minds wanna know!".. <smile>.
0
 

Author Comment

by:poupou
ID: 2611694
thanks PatrickVD for your help,

just adding the ByVal before the buffer parameter stops my pg to crash.


comment for rob and wsh2:
using the standard put and get is effectively a better way to access binary file.
But I just don't like my applications to crash without finding the problem.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now