首页 /编程语言和算法/VB6/VBA/ASP
 如果遇到Code Organizer用regsvr32命令注册dll时返回0x80004005的解决方法
今天 14:40

注册DLL.vbs的代码(记得用ANSI格式保存,dll的文件名CodeOrganizer.dll可以修改成对应的dll名):

'有中文,必须用ANSI编码格式写本vbs

With CreateObject("Scripting.FileSystemObject")
    ' 获取当前脚本所在目录
    Dim currentPath: currentPath = .GetParentFolderName(WScript.ScriptFullName)
    
    ' 确定regsvr32.exe的正确路径(兼容32/64位系统)
    Dim RegSvr
    If .FileExists(.GetSpecialFolder(0).Path & "\SysWOW64\regsvr32.exe") Then
        RegSvr = .GetSpecialFolder(0).Path & "\SysWOW64\regsvr32.exe"  ' 64位系统上的32位regsvr32
    ElseIf .FileExists(.GetSpecialFolder(0).Path & "\System32\regsvr32.exe") Then
        RegSvr = .GetSpecialFolder(0).Path & "\System32\regsvr32.exe"   ' 32位系统或64位系统上的64位regsvr32
    Else
        MsgBox "无法找到regsvr32.exe!", vbCritical, "错误"
        WScript.Quit
    End If
    
    ' 定义需要注册的DLL文件列表
    Dim dllFiles(0)  ' 数组大小为实际文件数量-1
    dllFiles(0) = "CodeOrganizer.dll"
    'dllFiles(1) = "SecondDLL.dll"
    'dllFiles(2) = "ThirdDLL.dll"
    
    ' 创建文件列表字典
    Dim fileList
    Set fileList = CreateObject("Scripting.Dictionary")
    
    ' 检查每个文件是否存在
    Dim i, missingFiles
    missingFiles = ""
    
    For i = LBound(dllFiles) To UBound(dllFiles)
        Dim fileName: fileName = dllFiles(i)
        Dim filePath: filePath = currentPath & "\" & fileName
        
        If .FileExists(filePath) Then
            fileList.Add fileName, filePath
        Else
            missingFiles = missingFiles & fileName & vbLF
        End If
    Next
    
    ' 检查是否找到任何文件
    If fileList.Count = 0 Then
        MsgBox "未找到任何DLL文件!" & vbLF & vbLF & "查找路径:" & currentPath, vbExclamation, "错误"
        WScript.Quit
    End If
    
    ' 显示缺失的文件(如果有)
    If missingFiles <> "" Then
        MsgBox "以下文件未找到:" & vbLF & vbLF & missingFiles & vbLF & _
               "将继续处理找到的文件。", vbExclamation, "警告"
    End If
    
    ' 显示将要处理的文件
    Dim fileNames: fileNames = Join(fileList.Keys, vbLF)

    ' 注册所有文件
    For Each fileName In fileList.Keys
        RunAsAdmin RegSvr, "/s """ & fileList(fileName) & """"
        WScript.Sleep 1000
    Next
    
    MsgBox "已完成DLL文件的注册操作!" & vbLF & vbLF & _
           "成功处理了 " & fileList.Count & " 个文件:" & vbLF & fileNames & vbLF & vbLF & _
           IIf(missingFiles <> "", "未找到的文件:" & vbLF & missingFiles, ""), vbInformation, "完成"
End With

' 以管理员权限运行程序的函数
Sub RunAsAdmin(program, args)
    CreateObject("Shell.Application").ShellExecute program, args, "", "runas", 1
End Sub

' 三元运算符函数
Function IIf(condition, trueValue, falseValue)
    If condition Then
        IIf = trueValue
    Else
        IIf = falseValue
    End If
End Function

卸载DLL.vbs:

'有中文,必须用ANSI编码格式写本vbs

'有中文,必须用ANSI编码格式写本vbs

With CreateObject("Scripting.FileSystemObject")
    ' 获取当前脚本所在目录
    Dim currentPath: currentPath = .GetParentFolderName(WScript.ScriptFullName)
    
    ' 确定regsvr32.exe的正确路径(兼容32/64位系统)
    Dim RegSvr
    If .FileExists(.GetSpecialFolder(0).Path & "\SysWOW64\regsvr32.exe") Then
        RegSvr = .GetSpecialFolder(0).Path & "\SysWOW64\regsvr32.exe"  ' 64位系统上的32位regsvr32
    ElseIf .FileExists(.GetSpecialFolder(0).Path & "\System32\regsvr32.exe") Then
        RegSvr = .GetSpecialFolder(0).Path & "\System32\regsvr32.exe"   ' 32位系统或64位系统上的64位regsvr32
    Else
        MsgBox "无法找到regsvr32.exe!", vbCritical, "错误"
        WScript.Quit
    End If
    
    ' 定义需要卸载的DLL文件列表
    Dim dllFiles(0)  ' 数组大小为实际文件数量-1
    dllFiles(0) = "CodeOrganizer.dll"
    'dllFiles(1) = "SecondDLL.dll"
    'dllFiles(2) = "ThirdDLL.dll"
    
    ' 创建文件列表字典
    Dim fileList
    Set fileList = CreateObject("Scripting.Dictionary")
    
    ' 检查每个文件是否存在
    Dim i, missingFiles
    missingFiles = ""
    
    For i = LBound(dllFiles) To UBound(dllFiles)
        Dim fileName: fileName = dllFiles(i)
        Dim filePath: filePath = currentPath & "\" & fileName
        
        If .FileExists(filePath) Then
            fileList.Add fileName, filePath
        Else
            missingFiles = missingFiles & fileName & vbLF
        End If
    Next
    
    ' 检查是否找到任何文件
    If fileList.Count = 0 Then
        MsgBox "未找到任何DLL文件!" & vbLF & vbLF & "查找路径:" & currentPath, vbExclamation, "错误"
        WScript.Quit
    End If
    
    ' 显示缺失的文件(如果有)
    If missingFiles <> "" Then
        MsgBox "以下文件未找到:" & vbLF & vbLF & missingFiles & vbLF & _
               "将继续处理找到的文件。", vbExclamation, "警告"
    End If
    
    ' 显示将要处理的文件
    Dim fileNames: fileNames = Join(fileList.Keys, vbLF)
    
    ' 卸载所有文件
    For Each fileName In fileList.Keys
        RunAsAdmin RegSvr, "/u /s """ & fileList(fileName) & """"
        WScript.Sleep 1000
    Next
    
    MsgBox "已完成DLL文件的卸载操作!" & vbLF & vbLF & _
           "成功卸载了 " & fileList.Count & " 个文件:" & vbLF & fileNames & vbLF & vbLF & _
           IIf(missingFiles <> "", "未找到的文件(已跳过):" & vbLF & missingFiles, ""), vbInformation, "完成"
End With

' 以管理员权限运行程序的函数
Sub RunAsAdmin(program, args)
    CreateObject("Shell.Application").ShellExecute program, args, "", "runas", 1
End Sub

' 三元运算符函数
Function IIf(condition, trueValue, falseValue)
    If condition Then
        IIf = trueValue
    Else
        IIf = falseValue
    End If
End Function


 
全部回复(0)
首页 | 电脑版 |