Page 1 of 1

Posted: Sun Apr 21, 2002 6:34 pm
by BackupUser
Restored from previous forum. Originally posted by freak.

Hi all,

The following code should scan drive c and write the Directory tree to the File
c:\scan.log, but it always crashes on a certain Directory.
Is there someting wrong in this code? Can someone help me?

Code: Select all

; ++++++++++++++++++++++++++++++++++++++++++
  
Procedure SearchDisk(path.s)
  WriteStringN(path)
  name.s = path + "*"
  handle.l = FindFirstFile_(name, info.WIN32_FIND_DATA)
  If handle  #INVALID_HANDLE_VALUE
    Repeat
      Entry.s = PeekS(@info\cFileName[0]) 
      If info\dwFileAttributes & #FILE_ATTRIBUTE_DIRECTORY
        If Entry  "." And Entry  ".."
          SearchDisk(path + Entry + "\")
        EndIf
      EndIf
    Until FindNextFile_(handle, info)= #FALSE
    FindClose_(handle)
  EndIf
EndProcedure
  
CreateFile(0, "c:\scan.log")
  SearchDisk("C:\")
CloseFile(0)
  
; ++++++++++++++++++++++++++++++++++++++++++
Timo



A debugged program is one for which you have not yet found the conditions that make it fail.

Posted: Sun Apr 21, 2002 6:52 pm
by BackupUser
Restored from previous forum. Originally posted by merendo.

Interesting. Hold on, please... . I´ve tested the programme. It also crashes. When does it crash when you run it on your PC? My crash-dir is: C:\WINDOWS\SYSTEM\COLOR\

Greetings, merendo

We always need to hear both sides of the story (by Phil Collins)

Posted: Sun Apr 21, 2002 6:54 pm
by BackupUser
Restored from previous forum. Originally posted by tinman.

Should this line:

Code: Select all

    handle.l = FindFirstFile_(name, info.WIN32_FIND_DATA)
Have at @ symbol in front of the info?

--
It's not minimalist - I'm increasing efficiency by reducing input effort.

Posted: Sun Apr 21, 2002 7:16 pm
by BackupUser
Restored from previous forum. Originally posted by freak.

@merendo

Hi,
Well, it always crashes on "C:\WINDOWS\Anwendungsdaten\Microsoft\Office\Zuletzt verwendet\".
But. if I add someting like

Code: Select all

If Path  "C:\WINDOWS\Anwendungsdaten\Microsoft\Office\Zuletzt verwendet\" 
to the Code (so this dir is not scanned) it crashes at another Dir.
I really have no Idea, what's wrong.

BTW: Thanks for the fast replies!

Timo

A debugged program is one for which you have not yet found the conditions that make it fail.

Posted: Sun Apr 21, 2002 7:42 pm
by BackupUser
Restored from previous forum. Originally posted by Pupil.
@merendo

Hi,
Well, it always crashes on "C:\WINDOWS\Anwendungsdaten\Microsoft\Office\Zuletzt verwendet\".
But. if I add someting like

Code: Select all

If Path  "C:\WINDOWS\Anwendungsdaten\Microsoft\Office\Zuletzt verwendet\" 
to the Code (so this dir is not scanned) it crashes at another Dir.
I really have no Idea, what's wrong.

BTW: Thanks for the fast replies!

Timo

A debugged program is one for which you have not yet found the conditions that make it fail.
It all has to do with the constant #MAX_PATH that you reported in earlier. As can be seen in the win32 docs WIN32_FIND_DATA is defined as:

Code: Select all

typedef struct _WIN32_FIND_DATA { // wfd  
    DWORD dwFileAttributes; 
    FILETIME ftCreationTime; 
    FILETIME ftLastAccessTime; 
    FILETIME ftLastWriteTime; 
    DWORD    nFileSizeHigh; 
    DWORD    nFileSizeLow; 
    DWORD    dwReserved0; 
    DWORD    dwReserved1; 
    TCHAR    cFileName[ MAX_PATH ]; Here lies the problem!!!
    TCHAR    cAlternateFileName[ 14 ]; 
} WIN32_FIND_DATA;
If you use SizeOf(WIN32_FIND_DATA) you'll see that this structure is only 90 bytes in size, if you have long path names and such this is too small and you'll end up writing to memory outside your variable's structure and thats what's causing the crashes. One solution could be to redefine this structure and use that instead until next version of PB comes out..

Code: Select all

;new version of WIN32_FIND_DATA

structure WIN32_FIND_DATA_TMP
  dwFileAttributes.l
  ftCreationTime.FILETIME
  ftLastAccessTime.FILETIME
  ftLastWriteTime.FILETIME
  nFileSizeHigh.l
  nFileSizeLow.l
  dwReserved0.l
  dwReserved1.l
  cFileName.b[260]
  cAlternateFileName.b[14]
EndStructure

Posted: Sun Apr 21, 2002 8:19 pm
by BackupUser
Restored from previous forum. Originally posted by freak.

Thanks, now it works!
I knew, that #MAX_PATH was wrong, but I thought it had no effect on the code since scanning a single Directorys worked fine.
But with this structure it works.

Fred, this must be also corrected for the next version!

Thanks all, for replying so fast!#

BTW: here ist the working code if anyone want's to use it:

Code: Select all

; ++++++++++++++++++++++++++++++++++++++++++
; This code scans drive C and writes the directory-tree to a file.

Structure WIN32_FIND_DATA_TMP
  dwFileAttributes.l
  ftCreationTime.FILETIME
  ftLastAccessTime.FILETIME
  ftLastWriteTime.FILETIME
  nFileSizeHigh.l
  nFileSizeLow.l
  dwReserved0.l
  dwReserved1.l
  cFileName.b[260]
  cAlternateFileName.b[14]
EndStructure
  
Procedure SearchDisk(path.s)
  WriteStringN(path)
  name.s = path + "*"
  handle.l = FindFirstFile_(name, @info.WIN32_FIND_DATA_TMP)
  If handle  #INVALID_HANDLE_VALUE
    Repeat
      Entry.s = PeekS(@info\cFileName[0]) 
      If info\dwFileAttributes & #FILE_ATTRIBUTE_DIRECTORY
        If Entry  "." And Entry  ".."
          SearchDisk(path + Entry + "\")
        EndIf
      EndIf
    Until FindNextFile_(handle, @info)= #FALSE
    FindClose_(handle)
  EndIf
EndProcedure
  
CreateFile(0, "c:\scan.log")
  SearchDisk("C:\")
CloseFile(0)

MessageRequester("","done",0)

; ++++++++++++++++++++++++++++++++++++++++++
Timo




A debugged program is one for which you have not yet found the conditions that make it fail.