?
Solved

CRASH DUE TO ACCESS VIOLATION

Posted on 2000-03-02
5
Medium Priority
?
360 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 14

Accepted Solution

by:
mcrider earned 300 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

770 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