Link to home
Start Free TrialLog in
Avatar of Drumsticks
Drumsticks

asked on

SearchTreeForFile crashing VB6, using Windows XP Pro

Hello,

I have been using SearchTreeForFile API for quite some time in a VB6 program, but all of a sudden, the SearchTreeForFile API is making VB6 crash.  I thought it may be related to the imagehlp.dll file being corrupt, so I swapped it with a new one, and I am still getting the same results.  I then had some older versions of the dll file, and had SearchTreeForFile referenced to it, and VB6 will not crash, but it will not search an entire drive, it will only find the file if I point to the exact directory in which it is located.

Has anyone experienced this phenomena before?  I'm open to any kind of suggestions, because I'm feeling a little dead in the water at this moment.

Thank You,
Drumsticks
Avatar of anthonywjones66
anthonywjones66

How do you know it is SearchTreeFOrFile that is causing this?
What exactly is the exception in the crash?
Can you show a little code leading up to the Call to this API?

Anthony.
Avatar of Drumsticks

ASKER

Hello anthony,

Thank you for the quick response!

The only reason why I know it has something to do with SearchTreeForFile is because I stripped an example of only the SearchTreeForFile API and made an exclusive VB project.  The example crashes VB6 as well.

Here is the example:

******PLACE THIS CODE IN A MODULE******
Option Explicit

Declare Function SearchTreeForFile Lib "IMAGEHLP.DLL" _
    (ByVal lpRootPath As String, _
    ByVal lpInputName As String, _
    ByVal lpOutputName As String) As Long
Public Const MAX_PATH = 260

Public Function FindFile(RootPath As String, FileName As String) As String
Dim lNullPos As Long
Dim lResult As Long
Dim sBuffer As String
     
On Error GoTo FileFind_Error
    sBuffer = Space(MAX_PATH * 2)
     
    'Find the file
    lResult = SearchTreeForFile(RootPath, FileName, sBuffer)
     
    If lResult Then 'Trim null, if exists
        lNullPos = InStr(sBuffer, vbNullChar)
        If Not lNullPos Then
            sBuffer = Left(sBuffer, lNullPos - 1)
        End If
        FindFile = sBuffer 'Return filename
    Else
        FindFile = vbNullString 'Nothing found
    End If
     
    Exit Function
     
FileFind_Error:
    FindFile = vbNullString
     
End Function

******PLACE THIS CODE IN A FORM******

Option Explicit

Dim sCheckIt As String, sDrive As String, sFile As String

Private Sub Command1_Click()
    sDrive = "c:\" ' <= Change this or let user choose
    sFile = "comlog.txt" ' <= Change this or let user choose
    sCheckIt = FindFile(sDrive, sFile)

    If sCheckIt = "" Then
        MsgBox "File not found"
    Else
        Debug.Print sCheckIt
    End If
End Sub
I just replaced the msvcrt.dll with a new one, which imagehlp.dll depends on, I then ran a file search on an alternate drive 'D:\".  I placed a file in a subfolder, and ran the example.  The result was correct and it found the file in the subfolder correctly, and without crashing VB6.  
I then had it try to find the same file in the C:\ drive (my system drive) and it is back to crashing again.

The exception I receive is "An exception 'Unhandled Win32 Exception' has occurred in VB6.exe"
What operating system are you using?

Make MAX_PATH much bigger say 2000,  I'm pretty sure you can now get paths bigger than 260 characters long.

Anthony.
Just as it says in the topic,  I'm using Windows XP Pro.

I changed the MAX_PATH to 2000, and VB6 is still crashing on the C:\ drive.
I think it's kind of odd that it will search fine in my alternate drive D: and crash in drive C:, which C: is my system drive.

However, VB6 used to crash searching through drive D: before I swapped msvcrt.dll with a new one.

What do you think?
SOLUTION
Avatar of anthonywjones66
anthonywjones66

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yeah, I see what your saying.  It is bizaar.  Every SearchTreeForFile example made by various people use the imagehlp.dll over the dbghelp.dll.  I replaced the imagehlp.dll with the dbghelp.dll for the SearchTreeForFile library reference, and the results are still the same.  It's interesting to know that either dll works.  

I'm searching for the dbghelp.h file right at the moment, so far it has only found one reference which is placed in my .NET directory.  I'm wondering if this file is missing in the system directory.  But I would think that if it is required by any means, then it would not make sense that the D:\ drive search would be successful.  I would think that none of the drives would be successfully searched.  I wonder if there are other required dependency files for dbghelp.dll to operate properly, and if so, is one of the dependency files corrupt?

Very perplexing...

ASKER CERTIFIED SOLUTION
Avatar of Ark
Ark
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes, I agree Ark, it is quite possible that this is a bug.
are you receiving the same symptoms?
You test that by placing an additional pagefile on D: and see if you now get a problem on D: also.

Anthony.
It may not be only the pagefile.sys.  

Here is what I've done so far as a workaround:
I modified my code to work around the C:\ drive, which is my system drive that contains the pagefile.sys, by not allowing SearchTreeForFile to search the root drive.  Instead, the Dir() function now searches the root drive.  Then, I have the Dir() function in a loop to grab every subdirectory from the root drive, which then SearchTreeForFile will search through every folder and file of each subdirectory until there are no more subdirectories to search in the root drive.

It all worked fine until VB6 crashed somewhere within the 'My Documents' directory from the SearchTreeForFile function.  I then put a windows search to find pagefile.sys anywhere in the C:\ drive.  It came up with only pagefile.sys in c:\ root drive.  I'm going to test some more, and keep y'all posted.  

ok, it appears that there is also a limit to the size of the each directory when SearchTreeForFile is searching through the drive.  I pinpointed the 'Documents and Settings' issue, which was crashing VB6, to be a subdirectory that was over 75 characters in length.  When I changed it to be 75 characters or less, the SearchTreeForFile ran properly.

Also, if you look at paragraph three under the Remarks section in the following link, I think you'll see some similarities about the stipulations with the findfirstfile function compared to the symptoms with the SearchTreeForFile function.

I did try the C:\*, and it doesn't work for SearchTreeForFile, so I thought I would go ahead and let you know before you waiste your time on it.  Using the asterisk is a difference between the two functions, but it may be more than coincidental that neither function can use the C:\ root, unless FindFirstFile uses the asterisk.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base/findfirstfile.asp
Thank you both for you assistance.