Results 1 to 7 of 7

Thread: Multiple API calls crashing NT

  1. #1

    Thread Starter
    Member docHoliday's Avatar
    Join Date
    Sep 1999
    Location
    Stamford, CT, USA
    Posts
    48

    Multiple API calls crashing NT

    I and a VB buddy have done some independent digging though a bit of code used to open the contents of a machine into a Tree View control using API calls. The problem is the app crashes in NT because more than 1 API call are being made to the same DLL at the same time. They are inside a DO loop, and the it fires an API call it's first time through, but doesn't seem to close it before the second Loop. What I need help with now is identifying which API call made within this function may be persisting through the loop to the next interation.

    Here's the function at fault. Again, any help is greatly appreciated. If anyone would like the entire VBP file to see it in action, you can download it here Message Posted in genral Questions thread

    And now the error filled code:
    Code:
    'This function simply checks a directroy to see if it has any sub directories
    'It returns the first sub dir name and then exits
    Function HasSubDirs(ByVal sStartDir As String) As String
    
    Dim lpFindFileData As WIN32_FIND_DATA, lFileHdl  As Long, lRet As Long
    Dim sTemp As String
    On Error Resume Next
    If sStartDir = "" Then Exit Function
    
    If Right$(sStartDir, 1) <> "\" Then sStartDir = sStartDir & "\"
    sStartDir = sStartDir & "*.*"
    lFileHdl = FindFirstFile(sStartDir, lpFindFileData)
    
    If lFileHdl <> 0 Then
        Do Until lRet = ERROR_NO_MORE_FILES
            If (lpFindFileData.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = 16 Then
                sTemp = StripTerminator(lpFindFileData.cFileName)
                If sTemp <> "." And sTemp <> ".." Then
                    HasSubDirs = StrConv(sTemp, vbProperCase)
                    Exit Do
                End If
            End If
            lRet = FindNextFile(lFileHdl, lpFindFileData)
            If lRet = 0 Then Exit Function
        Loop
    End If
    lRet = FindClose(lFileHdl)
    End Function
    Last edited by docHoliday; May 24th, 2001 at 10:28 PM.
    "A balm? That's a dangerous animal. Throw it in the trough!"
    - Monty Python

  2. #2
    jim mcnamara
    Guest
    I started reading your code - a cooment

    Consider using this api to get the computer name, not RegEnumKey which will not work universally the way you coded it

    Code:
    Public Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
    
    Function GetCompName() as String
        Dim tmp As String
        Dim i As Long
        GetCompName = "My Computer"
        tmp = Space(255)
        i = GetComputerName(tmp, 255)
        If i Then
           GetCompName  = Left(tmp, InStr(tmp, vbNullChar) - 1)
        End If
    End Function

  3. #3
    jim mcnamara
    Guest
    Found the problem - you are not always closing the file search context handle.

    Code:
    If lFileHdl <> 0 Then
        Do Until lRet = ERROR_NO_MORE_FILES
            If (lpFindFileData.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = 16 Then
                sTemp = StripTerminator(lpFindFileData.cFileName)
                If sTemp <> "." And sTemp <> ".." Then
                    HasSubDirs = StrConv(sTemp, vbProperCase)
                    Exit Do
                End If
            End If
            lRet = FindNextFile(lFileHdl, lpFindFileData)
           If lRet = 0 Then Exit Function 
           'should be  Exit Do 
        Loop
    End If
    lRet = FindClose(lFileHdl)  'this line not executed
                            
    End Function
    This leaves the context screwed up for subsequent calls.

  4. #4

    Thread Starter
    Member docHoliday's Avatar
    Join Date
    Sep 1999
    Location
    Stamford, CT, USA
    Posts
    48

    Thumbs up

    That looks correct. Geez the small things. I had a feeling it was skipping the FindClose function, but I couldn't find where. Always helps to have some fresh eyes peak at the code.

    So far, that fix and the new GetCompName() works great in Win98. It actually displays the computer name now which it didn't before. Very cool! I will test it out on WinNT tomorrow morning and post an update whether it worked or not.

    Quick follow-up question. Is there a way through the API to determine the path to the current Windows desktop. Obviously in Win95/98 it can be easy, get the Win directory and then say "\desktop". But with WinNT, you have multiple user profiles where only one is the desktop for the active user. Is this possible to figure out the current desktop from the API or would I have to get the win directory, then get the current user and build it myself? Either way, if that sounds feasible I may take a stab at it.

    Thanks for the help so far Jim. VERY much appreciated. You don't happen to have any suggestions for fixing a Word2000 Automation error too, do ya? Microsoft Word Automation Thread
    "A balm? That's a dangerous animal. Throw it in the trough!"
    - Monty Python

  5. #5

    Thread Starter
    Member docHoliday's Avatar
    Join Date
    Sep 1999
    Location
    Stamford, CT, USA
    Posts
    48
    Ok, it now works, only the HasSubDirs function required some more modifications, which I picked out of the FindFirstFile documentation of www.vbapi.com, my new favorite web site.(for today at least). Here's the modified function that works:

    Code:
    'This function simply checks a directroy to see if it has any sub directories
    'It returns the first sub dir name and then exits
    ' MODIFIED 5/25/01 BY JEFF FOX ([email protected])
    ' ADDED SUCCESS VARIABLE TO CAPTURE THE VALUE RETURNED BY FindNextFile 
    ' MODIFIED THE "IF lFileHdl" STATEMENT TO BETTER HANDLE THE RETURNED VALUE
    ' MODIFIED THE DO LOOP ADDING THE Until success = 0 TO BETTER HANDLE THE RETURNED VALUE oF THE THE FindNextFile API CALL
    
    Function HasSubDirs(ByVal sStartDir As String) As String
    
    Dim lpFindFileData As WIN32_FIND_DATA, lFileHdl  As Long, lRet As Long
    Dim sTemp As String, success As Long
    
    If sStartDir = "" Then Exit Function
    
    If Right$(sStartDir, 1) <> "\" Then sStartDir = sStartDir & "\"
    sStartDir = sStartDir & "*.*"
    lFileHdl = FindFirstFile(sStartDir, lpFindFileData)
    
    If lFileHdl = -1 Then
        HasSubDirs = ""
    Else
        Do
            If (lpFindFileData.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = 16 Then
                sTemp = StripTerminator(lpFindFileData.cFileName)
                If sTemp <> "." And sTemp <> ".." Then
                    HasSubDirs = StrConv(sTemp, vbProperCase)
                    Exit Do
                End If
            End If
            success = FindNextFile(lFileHdl, lpFindFileData)
        Loop Until success = 0
    End If
    lRet = FindClose(lFileHdl)  'this line not executed
                            
    End Function
    "A balm? That's a dangerous animal. Throw it in the trough!"
    - Monty Python

  6. #6
    jim mcnamara
    Guest
    Go here. Search for folder

    The sample code you want is:

    SHGetSpecialFolderLocation to Find Popular Shell Folders
    So, this will become your next daily favorite site....

  7. #7

    Thread Starter
    Member docHoliday's Avatar
    Join Date
    Sep 1999
    Location
    Stamford, CT, USA
    Posts
    48

    Smile

    ALl I have to say is wow, that may be my new fav web site for a while!!! Good stuff. That browse for special folder function works great. Found a few tiddy up functions there as well.

    Well, thanks for your help Jim. Do you think you'd be interested in participating in the Beta for the app I'm dumping all this code into? I'm planning to release the first Beta June 1. If your interested, e-mail me your e-mail address and I'll add you to the release announcment list.
    "A balm? That's a dangerous animal. Throw it in the trough!"
    - Monty Python

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width