Ray Posted October 24, 2011 Share Posted October 24, 2011 (edited) While looking through some stuff on YouTube a few days ago, I came across a video (w w w dot youtube dot com/watch?v=t-6dmZjDZjw&feature=digest_holdback_fri) by Brittec that demonstrated the re-registering of DLLs. Brittec's video is titled "Fix Windows Errors by Re-registering All Your DLL's by Britec" It was interesting and although I'm not sure if it's required in the scope in which the demonstration was performed, I decided that it'd make a good project for AutoIt. Here, is the code that I came up with (freely borrowing functions and ideas from some of you.) expandcollapse popup#AutoIt3Wrapper_UseX64=y #include <Array.au3> #include <RecFileListToArray.au3> ; Author: Melba23 #include <_FileCheckWinPEFormat.au3> ; Author: Ascend4nt Global $OperatingSystem = @OSVersion & " " & @OSArch Global $ExcludeFolder = "winsxs;$Patchcache$;temp" Global $ExcludeFiles = "" Global $SearchFIlesFilter = "*.dll;*.ocx" Global $SearchPath = @WindowsDir & "\" $iOldMode = _SetErrorMode(1) ; SEM_FAILCRITICALERRORS $DLLfiles = _RecFileListToArray($SearchPath, $SearchFIlesFilter, 0, 1, 0, 2, $ExcludeFiles, $ExcludeFolder) ;Search for files RemoveDirectoryEntries($DLLfiles) ;Remove directories found during search _ArrayResize($DLLfiles, 0, 3) ;Resize the array RegisterDLLs($DLLfiles) ;Execute Regsvr32 on file $iOldMode = _SetErrorMode(0) ; Back to Normal _ArrayDisplay($DLLfiles,"Re-registering of DLLs and OCXs", -1, 0, "", "|", "Count|Filename and Path|Target Machine|Regsvr32 Result") Exit Func RemoveDirectoryEntries(ByRef $array) For $count = 1 To UBound($array, 1) - 1 If StringRight($array[$count], 1) = "\" Then $array[$count] = "" $array[0] -= 1 EndIF Next _ArrayPack($array) EndFunc Func _ArrayPack(ByRef $avArray, $iCol = 0) ; Author: Spiff59 ArrayPack slightly modified by Ray If Not IsArray($avArray) Then Return SetError(1, 0, 0) Local $dims = UBound($avArray, 0), $rows = UBound($avArray, 1), $cols = UBound($avArray, 2) Local $pointer = -1, $keep = 0 Switch $dims Case 1 For $r = 0 To $rows - 1 If $avArray[$r] <> "" Then $pointer += 1 $avArray[$pointer] = $avArray[$r] $keep += 1 EndIF Next ReDim $avArray[$pointer + 1] Case 2 For $r = 0 To $rows - 1 If $avArray[$r][$iCol] <> "" Then $pointer += 1 $keep += 1 For $c = 0 To $cols - 1 $avArray[$pointer][$c] = $avArray[$r][$c] Next EndIF Next ReDim $avArray[$pointer][$cols] Case Else Return SetError(3, 0, 0) EndSwitch $deleted = $rows - $keep Return $deleted EndFunc Func _ArrayResize(ByRef $avArray, $cRows = 1, $cCols = 0) If Not IsArray($avArray) Then Return SetError(1, 0, 0) Local $dims = UBound($avArray, 0) Local $rows = UBound($avArray, 1) Local $cols = UBound($avArray, 2) local $elements[2] If $rows + $cRows <= 0 Or $cols + $cCols < 0 Then Return SetError(4, 0, 0) EndIF If $cols = 0 Then $cols = 1 Switch $dims Case 1 If $cCols > 0 Then Dim $tArray[$rows + $cRows][$cols + $cCols] For $r = 0 To $rows - 1 $tArray[$r][0] = $avArray[$r] Next ReDim $avArray[UBound($tArray)][UBound($tArray, 2)] For $r = 0 To $rows - 1 $avArray[$r][0] = $tArray[$r][0] Next Else ReDim $avArray[$rows + $cRows] EndIF $elements[0] = UBound($avArray, 1) $elements[1] = UBound($avArray, 2) Return $elements Case 2 If $cols + $cCols = 1 Then $tArray = $avArray ReDim $avArray[$rows + $cRows] For $r = 0 To UBound($tArray, 1) - 1 $avArray[$r] = $tArray[$r][0] Next Return Else ReDim $avArray[$rows + $cRows][$cols + $cCols] EndIF $elements[0] = UBound($avArray, 1) $elements[1] = UBound($avArray, 2) Return $elements Case Else Return SetError(3, 0, 0) EndSwitch EndFunc Func RegisterDLLs(ByRef $array) Local $result For $count = 1 To UBound($array, 1) - 1 ConsoleWrite($count & " of " & UBound($array, 1) - 1 & @TAB & $array[$count][0]) $array[$count][1] = GetMachineTarget($array[$count][0]) If StringInStr($OperatingSystem, "X64") Then If StringInStr($array[$count][1], "AMD64") Or StringInStr($array[$count][1], "IA64") Then $array[$count][2] = ReturnCode(RunWait(@WindowsDir & "\System32\Regsvr32.exe /s " & $array[$count][0])) EndIf If StringInStr($array[$count][1], "I386") Or StringInStr($array[$count][1], "I286") Then $array[$count][2] = ReturnCode(RunWait(@WindowsDir & "\SysWoW64\Regsvr32.exe /s " & $array[$count][0])) EndIf EndIf If StringInStr($OperatingSystem, "X86") Then $array[$count][2] = ReturnCode(RunWait(@WindowsDir & "\System32\Regsvr32.exe /s " & $array[$count][0])) EndIf ConsoleWrite(@CRLF & @TAB & @TAB & "Re-Registering " & $array[$count][2]) ConsoleWrite(@CRLF & @CRLF) Next ;if 64bit system then The 32-bit version is %systemroot%\SysWoW64\regsvr32.exe. ;if 64bit system then The 64-bit version is %systemroot%\System32\regsvr32.exe. else 32-bit version is %systemroot%\System32\regsvr32.exe EndFunc Func GetMachineTarget($file) Local $result = _FileCheckWinPEFormat($file) Local $return, $identified #region - Array $IMAGE_FILE_MACHINE setup Local $IMAGE_FILE_MACHINE[21][2] $IMAGE_FILE_MACHINE_UNKNOWN = 0 ;The contents of this field are assumed to be applicable to any machine type $IMAGE_FILE_MACHINE_AM33 = 1 ;Matsushita AM33 $IMAGE_FILE_MACHINE_AMD64 = 2 ;x64 (or x86-x64) ['AMD' prefix because they came up with architecture 1st] $IMAGE_FILE_MACHINE_ARM = 3 ;ARM little endian $IMAGE_FILE_MACHINE_ARMV7 = 4 ;ARMv7 (or higher) Thumb mode only $IMAGE_FILE_MACHINE_EBC = 5 ;EFI byte code $IMAGE_FILE_MACHINE_I386 = 6 ;Intel 386 or later processors and compatible processors $IMAGE_FILE_MACHINE_IA64 = 7 ;Intel Itanium processor family (or IA-64) $IMAGE_FILE_MACHINE_M32R = 8 ;Mitsubishi M32R little endian $IMAGE_FILE_MACHINE_MIPS16 = 9 ;MIPS16 $IMAGE_FILE_MACHINE_MIPSFPU = 10 ;MIPS with FPU $IMAGE_FILE_MACHINE_MIPSFPU16 = 11 ;MIPS16 with FPU $IMAGE_FILE_MACHINE_POWERPC = 12 ;Power PC little endian $IMAGE_FILE_MACHINE_POWERPCFP = 13 ;Power PC with floating point support $IMAGE_FILE_MACHINE_R4000 = 14 ;MIPS little endian $IMAGE_FILE_MACHINE_SH3 = 15 ;Hitachi SH3 $IMAGE_FILE_MACHINE_SH3DSP = 16 ;Hitachi SH3 DSP $IMAGE_FILE_MACHINE_SH4 = 17 ;Hitachi SH4 $IMAGE_FILE_MACHINE_SH5 = 18 ;Hitachi SH5 $IMAGE_FILE_MACHINE_THUMB = 19 ;Thumb $IMAGE_FILE_MACHINE_WCEMIPSV2 = 20 ;MIPS little-endian WCE v2 $IMAGE_FILE_MACHINE[$IMAGE_FILE_MACHINE_UNKNOWN][0] = 0x0 ;The contents of this field are assumed to be applicable to any machine type $IMAGE_FILE_MACHINE[$IMAGE_FILE_MACHINE_AM33][0] = 0x1d3 ;Matsushita AM33 $IMAGE_FILE_MACHINE[$IMAGE_FILE_MACHINE_AMD64][0] = 0x8664 ;x64 (or x86-x64) ['AMD' prefix because they came up with architecture 1st] $IMAGE_FILE_MACHINE[$IMAGE_FILE_MACHINE_ARM][0] = 0x1c0 ;ARM little endian $IMAGE_FILE_MACHINE[$IMAGE_FILE_MACHINE_ARMV7][0] = 0x1c4 ;ARMv7 (or higher) Thumb mode only $IMAGE_FILE_MACHINE[$IMAGE_FILE_MACHINE_EBC][0] = 0xebc ;EFI byte code $IMAGE_FILE_MACHINE[$IMAGE_FILE_MACHINE_I386][0] = 0x14c ;Intel 386 or later processors and compatible processors $IMAGE_FILE_MACHINE[$IMAGE_FILE_MACHINE_IA64][0] = 0x200 ;Intel Itanium processor family (or IA-64) $IMAGE_FILE_MACHINE[$IMAGE_FILE_MACHINE_M32R][0] = 0x9041 ;Mitsubishi M32R little endian $IMAGE_FILE_MACHINE[$IMAGE_FILE_MACHINE_MIPS16][0] = 0x266 ;MIPS16 $IMAGE_FILE_MACHINE[$IMAGE_FILE_MACHINE_MIPSFPU][0] = 0x366 ;MIPS with FPU $IMAGE_FILE_MACHINE[$IMAGE_FILE_MACHINE_MIPSFPU16][0] = 0x466 ;MIPS16 with FPU $IMAGE_FILE_MACHINE[$IMAGE_FILE_MACHINE_POWERPC][0] = 0x1f0 ;Power PC little endian $IMAGE_FILE_MACHINE[$IMAGE_FILE_MACHINE_POWERPCFP][0] = 0x1f1 ;Power PC with floating point support $IMAGE_FILE_MACHINE[$IMAGE_FILE_MACHINE_R4000][0] = 0x166 ;MIPS little endian $IMAGE_FILE_MACHINE[$IMAGE_FILE_MACHINE_SH3][0] = 0x1a2 ;Hitachi SH3 $IMAGE_FILE_MACHINE[$IMAGE_FILE_MACHINE_SH3DSP][0] = 0x1a3 ;Hitachi SH3 DSP $IMAGE_FILE_MACHINE[$IMAGE_FILE_MACHINE_SH4][0] = 0x1a6 ;Hitachi SH4 $IMAGE_FILE_MACHINE[$IMAGE_FILE_MACHINE_SH5][0] = 0x1a8 ;Hitachi SH5 $IMAGE_FILE_MACHINE[$IMAGE_FILE_MACHINE_THUMB][0] = 0x1c2 ;Thumb $IMAGE_FILE_MACHINE[$IMAGE_FILE_MACHINE_WCEMIPSV2][0] = 0x169 ;MIPS little-endian WCE v2 $IMAGE_FILE_MACHINE[$IMAGE_FILE_MACHINE_UNKNOWN][1] = "The contents of this field are assumed to be applicable to any machine type" $IMAGE_FILE_MACHINE[$IMAGE_FILE_MACHINE_AM33][1] = "AM33" $IMAGE_FILE_MACHINE[$IMAGE_FILE_MACHINE_AMD64][1] = "AMD64" $IMAGE_FILE_MACHINE[$IMAGE_FILE_MACHINE_ARM][1] = "ARM" $IMAGE_FILE_MACHINE[$IMAGE_FILE_MACHINE_ARMV7][1] = "ARMv7T" $IMAGE_FILE_MACHINE[$IMAGE_FILE_MACHINE_EBC][1] = "EBC" $IMAGE_FILE_MACHINE[$IMAGE_FILE_MACHINE_I386][1] = "I386" $IMAGE_FILE_MACHINE[$IMAGE_FILE_MACHINE_IA64][1] = "IA64" $IMAGE_FILE_MACHINE[$IMAGE_FILE_MACHINE_M32R][1] = "M32R" $IMAGE_FILE_MACHINE[$IMAGE_FILE_MACHINE_MIPS16][1] = "MIPS16" $IMAGE_FILE_MACHINE[$IMAGE_FILE_MACHINE_MIPSFPU][1] = "MIPSFPU" $IMAGE_FILE_MACHINE[$IMAGE_FILE_MACHINE_MIPSFPU16][1] = "MIPSFPU16" $IMAGE_FILE_MACHINE[$IMAGE_FILE_MACHINE_POWERPC][1] = "PowerPC" $IMAGE_FILE_MACHINE[$IMAGE_FILE_MACHINE_POWERPCFP][1] = "PowerPCFP" $IMAGE_FILE_MACHINE[$IMAGE_FILE_MACHINE_R4000][1] = "MIPS" $IMAGE_FILE_MACHINE[$IMAGE_FILE_MACHINE_SH3][1] = "SH3" $IMAGE_FILE_MACHINE[$IMAGE_FILE_MACHINE_SH3DSP][1] = "SH3DSP" $IMAGE_FILE_MACHINE[$IMAGE_FILE_MACHINE_SH4][1] = "SH4" $IMAGE_FILE_MACHINE[$IMAGE_FILE_MACHINE_SH5][1] = "SH5" $IMAGE_FILE_MACHINE[$IMAGE_FILE_MACHINE_THUMB][1] = "Thumb" $IMAGE_FILE_MACHINE[$IMAGE_FILE_MACHINE_WCEMIPSV2][1] = "WCEMIPSV2" #endregion If IsArray($result) Then $identified = False $return = "" For $type = 1 To UBound($IMAGE_FILE_MACHINE) - 1 If BitAND($result[0], $IMAGE_FILE_MACHINE[$type][0]) = $IMAGE_FILE_MACHINE[$type][0] Then ConsoleWrite(@CRLF & @TAB & @TAB & "Machine Target is " & $IMAGE_FILE_MACHINE[$type][1]) $identified = True If $return = "" Then $return &= $IMAGE_FILE_MACHINE[$type][1] Else $return &= " / " & $IMAGE_FILE_MACHINE[$type][1] EndIf EndIf Next If $return = "" Then EndIf Else ConsoleWrite(@CRLF & @TAB & @TAB & "Machine Target is " & "I286 or Unknown") $return = "I286 / Unknown" ; $version = GetWindowsVersion( EndIF Return $return ;_ArrayDisplay($result) EndFunc Func ReturnCode($code) Local $result Switch $code Case 0 $result = "Successful" Case 1 $result = "FAIL_ARGS" Case 2 $result = "FAIL_OLE" Case 3 $result = "FAIL_LOAD" Case 4 $result = "FAIL_ENTRY" Case 5 $result = "FAIL_REG" Case Else $result = $code EndSwitch Return $result EndFunc Func _SetErrorMode($iMode) ; Author: trancexx Local $aCall = DllCall("kernel32.dll", "dword", "SetErrorMode", "dword", $iMode) If @error Then Return SetError(1, 0, 0) Return $aCall[0] EndFunc I'd appreciate it if you guys would have a look and give suggestions on how to improve the script. Also, one thing that I haven't been able to get to work properly is the Function by trancexx that is designed to reroute errors so they aren't displayed. I don't understand how it works, I guess. I went to msdn.microsoft dot com and read some about SetErrorMode, even tried to use SetThreadErrorMode instead of SetErrorMode. NoGo - for me. The errors continue to pop up. The identification made by my function GetMachineTarget falsely identifies some files as I286 but that was by my design and suitable for my purposes. If someone knows of a way to correctly identify those files I'd like to hear of it. Thanks, Ray Edited October 24, 2011 by Ray Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now