Solved

CRASH DUE TO ACCESS VIOLATION

Posted on 2000-03-02
5
317 Views
Last Modified: 2008-03-10
Can anyone tell me why the following code crashes due to access violation. It crashes on the FindFirst instruction.

Option Explicit
Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Private Type FILETIME
        dwLowDateTime As Long
        dwHighDateTime As Long
End Type

Private Type WIN32_FIND_DATA
        dwFileAttributes As Long
        ftCreationTime As FILETIME
        ftLastAccessTime As FILETIME
        ftLastWriteTime As FILETIME
        nFileSizeHigh As Long
        nFileSizeLow As Long
        dwReserved0 As Long
        dwReserved1 As Long
        cFileName As String ' MAX_PATH
        cAlternate As String ' 14
       
End Type

Private Sub Form_Load()
Dim X
Dim RS As WIN32_FIND_DATA
Dim db
db = "c:\TEMP\FILE2.PCA"
X = FindFirstFile(db, RS)
If X = -1 Then
    MsgBox ("ERROR")
Else
    MsgBox (RS.cFileName)
End If
End
End Sub
0
Comment
Question by:askew
  • 3
  • 2
5 Comments
 
LVL 14

Accepted Solution

by:
mcrider earned 100 total points
ID: 2577220
Try this, it works on my system:

Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Private Const MAX_PATH = 260
Private Type FILETIME
    dwLowDateTime As Long
    dwHighDateTime As Long
End Type
Private Type WIN32_FIND_DATA
    dwFileAttributes As Long
    ftCreationTime As FILETIME
    ftLastAccessTime As FILETIME
    ftLastWriteTime As FILETIME
    nFileSizeHigh As Long
    nFileSizeLow As Long
    dwReserved0 As Long
    dwReserved1 As Long
    cFileName As String * MAX_PATH
    cAlternate As String * 14
End Type
Private Sub Form_Load()
Dim X As Long
Dim RS As WIN32_FIND_DATA
Dim db As String

db = "c:\TEMP\FILE2.PCA"
X = FindFirstFile(db, RS)
If X = -1 Then
    MsgBox ("ERROR")
Else
    MsgBox (RS.cFileName)
End If
End Sub


Cheers!
0
 

Author Comment

by:askew
ID: 2578124
Thanks a lot. Guess I really needed to define those string lengths in WIN32_FIND_DATA
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2578814
Yep... Thanks for the points... but, why did you grade this with a "B"?


Cheers!
0
 

Author Comment

by:askew
ID: 2580204
sorry about the B, does that affect your rating?.  I am trying to understand how api's work and an explanation of why I was wrong would have been helpful to me, though probably not to someone who just wanted to have code that worked. I will always grade as excellent in the future. Sorry again and thanks again.
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2580400
It affects my score... Here is what an expert receives when a question is graded assuming that the question is posted for 100 points:

   A= 400 quality points
   B= 300 quality points
   C= 200 quality points
   D= dont even go there...


Cheers!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

705 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