Sign in to follow this  
Followers 0
MadBoy

FileOpen, FileClose

5 posts in this topic

Heylo,

I'm having some hard time with my code here. I don't know, been looking at the code for a while now and can't realy find why it crashes. Well the reason is obvious. Too many open files. But the thing is i close files all the time. Maybe i open the same file too fast. It's in the loop and it's updating log file. Function _AddLogToFile($Text) opens up file ads line and closes the file. And so on. But still it reaches 64 open files and it crashes. Maybe it's because i call it from within other function? I'm lost here. Please help :D

E:\Projects\Project.AU3\hardware_0.4.2.au3 (345) : ==> Unable to open file, the maximum number of open files has been exceeded.:

$file_log = FileOpen($log_file, 1)

Func LogToFile()
    If $logging_to_file = "Yes" Then
        If FileExists($log_file) Then
            If FileOpen($log_file, 1) <> - 1 Then
                
            Else
                _FileCreate("C:\hardware.log")
                Global $log_file = "C:\hardware.log"
            EndIf
            FileClose($file_log)
        Else
            If _FileCreate($log_file) = 0 Then
                _FileCreate("C:\hardware.log")
                Global $log_file = "C:\hardware.log"
            EndIf
        EndIf
    EndIf
    $ErrorIsThere = "No"
EndFunc  ;==>LogToFile

Func _AddLogToFile($Text)
    If $logging_to_file = "Yes" Then
        $file_log = FileOpen($log_file, 1)
        FileWriteLine($file_log, "[" & _NowTime(5) & "] - " & $Text & @CRLF)
        FileClose($file_log)
    EndIf
EndFunc  ;==>_AddLogToFile

Func _AddLineBox($Text)
    GUICtrlSetData($LogBox, "[" & _NowTime(5) & "] - " & $Text & "|")
    _GUICtrlListSelectIndex($LogBox, _GUICtrlListCount($LogBox) - 1)
    _AddLogToFile($Text)
EndFunc  ;==>_AddLineBox


Func DeviceDrivers($path_to_drivers)
    If $method = "RegistryDevicePath" Then
        _AddLineBox("Setting path into registry using RegistryDevicePath method.")
       ; Resets DevicePath to DEFAULT
        RegWrite($HKLM & "\SOFTWARE\Microsoft\Windows\CurrentVersion", "DevicePath", "REG_EXPAND_SZ", "%SystemRoot%\inf;")
        $DirOutput = Run(@ComSpec & " /c DIR /A:D /S " & $path_to_drivers, '', @SW_HIDE, 2)
        While 1
            $DirData = StdoutRead($DirOutput)
            If @error Then ExitLoop
            If $DirData Then
                $DirOutputOnce &= $DirData
            Else
                Sleep(10)
            EndIf
        WEnd
       ; Remove spaces from output
        $DirOutputOnce = StringStripWS($DirOutputOnce, 3)
       ; Split output into array
        $DirSplit = StringSplit($DirOutputOnce, @CRLF, 1)
        For $i = 1 To $DirSplit[0]
            If StringInStr($DirSplit[$i], $path_to_drivers) Then
                $registrystring = StringSplit($DirSplit[$i], ": ", 1)
                If $registrystring[0] = 2 Then; Testing amount of elements in array, if more then 2 Exits
                    If StringInStr($registrystring[2], $path_to_drivers) Then; Making sure that Drivers path exists in string
                        $drivers_directory = $registrystring[2]
                        $search_ini = $drivers_directory & "\*.inf"
                        If FileFindFirstFile($search_ini) <> - 1 Then
                            $registryadd = $drivers_directory & ";"
                           ;Add things into registry
                            $oldkey = RegRead($HKLM & "\SOFTWARE\Microsoft\Windows\CurrentVersion", "DevicePath")
                            $newkey = RegWrite($HKLM & "\SOFTWARE\Microsoft\Windows\CurrentVersion", "DevicePath", "REG_EXPAND_SZ", $oldkey & $registryadd)
                            If $logging_option = "Advanced" Then
                                _AddLineBox("Added to registry: " & $drivers_directory)
                            EndIf
                        EndIf
                        FileClose($search_ini)
                    EndIf
                EndIf
            EndIf
        Next
        $DeviceDrivers = "PASSED"
        _AddLineBox("Drivers path was set successfully into registry.")
    EndIf
    If $method = "SetupCopyOemInf" Then
        _AddLineBox("Integrating drivers with SetupCopyOemInf method.")
       ; Removes all Unknown devices from system and gets output
        $DirOutput = Run(@ComSpec & " /c DIR /A:D /S " & $path_to_drivers, '', @SW_HIDE, 2)
        While 1
            $DirData = StdoutRead($DirOutput)
            If @error Then ExitLoop
            If $DirData Then
                $DirOutputOnce &= $DirData
            Else
                Sleep(10)
            EndIf
        WEnd
       ; Remove spaces from output
        $DirOutputOnce = StringStripWS($DirOutputOnce, 3)
       ; Split output into array
        $DirSplit = StringSplit($DirOutputOnce, @CRLF, 1)
        $NrCopiedInfs = 0
        For $i = 1 To $DirSplit[0]
            If StringInStr($DirSplit[$i], $path_to_drivers) Then
                $registrystring = StringSplit($DirSplit[$i], ": ", 1)
                If $registrystring[0] = 2 Then; Testing amount of elements in array, if more then 2 Exits
                    If StringInStr($registrystring[2], $path_to_drivers) Then; Making sure that Drivers path exists in string
                        $drivers_directory = $registrystring[2]
                        
                        $search_ini = FileFindFirstFile($drivers_directory & "\*.inf")
                        If $search_ini = -1 Then
                        Else
                            $dll_exe = DllOpen("setupapi.dll")
                            While 1
                                $search_file = FileFindNextFile($search_ini)
                                $full_path_to_inf = $drivers_directory & "\" & $search_file
                                If @error Then ExitLoop
                                $dll_result = DllCall($dll_exe, "int", "SetupCopyOEMInf", "str", $full_path_to_inf, "str", "", "int", 1, "int", 8, "str", "", "int", 0, "int", 0, "str", "")
                                If $logging_option = "Advanced" Then
                                    If @error = 0 Then
                                        _AddLineBox("Inf integration passed: " & $drivers_directory & "\" & $search_file)
                                        $NrCopiedInfs = $NrCopiedInfs + 1
                                    ElseIf @error = 1 Then
                                        _AddLineBox("Inf integration failed: " & $drivers_directory & "\" & $search_file)
                                    ElseIf @error = 2 Or @error = 3 Then
                                        _AddLineBox("Unknown return type or Function not found in DLL. Tell author about it!")
                                    EndIf
                                EndIf
                            WEnd
                            DllClose($dll_exe)
                        EndIf
                        FileClose($search_ini)
                    EndIf
                EndIf
            EndIf
        Next
        If $NrCopiedInfs = 1 Then _AddLineBox("SetupCopyOemInf method completed. " & $NrCopiedInfs & " driver was integrated.")
        If $NrCopiedInfs = 0 Then _AddLineBox("SetupCopyOemInf method completed. No drivers were integrated.")
        If $NrCopiedInfs <> 0 And $NrCopiedInfs <> 1 Then _AddLineBox("SetupCopyOemInf method completed. " & $NrCopiedInfs & " drivers were integrated.")
        $DeviceDrivers = "PASSED"
    EndIf
EndFunc  ;==>DeviceDrivers

My little company: Evotec (PL version: Evotec)

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

i am kind of lost what this piece of code does but these changes will probably solve your problem:

$h_file = FileOpen($log_file, 1)
            If $h_file <> - 1 Then
                
            Else
                _FileCreate("C:\hardware.log")
                Global $log_file = "C:\hardware.log"
            EndIf
            FileClose($h_file)
Edited by JdeB

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Nope that's not it. I don't think function LogToFile() matters here because it's run only once checking if the file can be open for writting (if run from CD) and if not it will make file on drive C and use it.The problem here is with one of those functions:

Func _AddLogToFile($Text)
    If $logging_to_file = "Yes" Then
        $file_log = FileOpen($log_file, 1)
        FileWriteLine($file_log, "[" & _NowTime(5) & "] - " & $Text & @CRLF)
        FileClose($file_log)
    EndIf
EndFunc  ;==>_AddLogToFile

Func _AddLineBox($Text)
    GUICtrlSetData($LogBox, "[" & _NowTime(5) & "] - " & $Text & "|")
    _GUICtrlListSelectIndex($LogBox, _GUICtrlListCount($LogBox) - 1)
    _AddLogToFile($Text)
EndFunc  ;==>_AddLineBox

Second function is used in DeviceDrivers($path_to_drivers) function which uses _AddLineBox in FOR loop.

E:\Projects\Project.AU3\hardware_0.4.2.au3 (346) : ==> Unable to open file, the maximum number of open files has been exceeded.:

$file_log = FileOpen($log_file, 1)


My little company: Evotec (PL version: Evotec)

Share this post


Link to post
Share on other sites

you have the same issue with your FileFindFirstFile statements.

You need to save the Handle and use that.

$h_Search = FileFindFirstFile($search_ini)

If $h_Search <> - 1 Then

FileClose($h_Search)


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

you have the same issue with your FileFindFirstFile statements.

You need to save the Handle and use that.

$h_Search = FileFindFirstFile($search_ini)

If $h_Search <> - 1 Then

FileClose($h_Search)

You're so right :/ I didn't thought this can't be used that way :| Oh well Tnx for fixing it for me.


My little company: Evotec (PL version: Evotec)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0