Solved

CRASH DUE TO ACCESS VIOLATION

Posted on 2000-03-02
5
333 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MsgBox 2 59
Exit a vb6 apps when a calling it apps closes 15 52
Excel Automation VBA 19 91
Inserting a datasheet query into the body of an email using VBA? 4 20
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…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

832 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