Visual Basic从注册表中读取DefaultUserName键值的问题

时间:2015-11-12 23:48:20

标签: vbscript

几周前我写了一个vbscript来获取远程计算机的DefaultUserName键值。该脚本运行良好。我试图从VB.net应用程序做同样的事情,我得到Nothing结果。我也从我的vb应用程序启动相同的脚本,我也是一样的。看起来该脚本可以单独运行,但如果从vb.net应用程序启动它则不行。 Bellow是我用来获取注册表值的vb代码,该代码也获得Nothing结果。我需要在vb环境中设置一些东西吗?可能会发生什么?任何帮助都很感激。感谢

Private Sub askComputer2(ByVal computer, ByRef text)
    Dim strKeyPath As String, strEntryName As String, comment As String, strValue As String
    'Dim objReg As Object
    Const HKEY_LOCAL_MACHINE = &H80000002
    strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
    strEntryName = "ReportBootOk"
    comment = ""
    strValue = ""
    'text = "Computer Name = " & computer & vbNewLine & vbNewLine

    On Error Resume Next

    Dim objReg As Object = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & computer & "\root\cimv2")

    If Err.Number <> 0 Then
        text = "It was not possible to get Autologin information for " & computer & vbNewLine & "Error: " & Err.Number & "  Source: " & Err.Source & "  Description: " & Err.Description
        Err.Clear()
    Else
        'MsgBox(objReg.Value(HKEY_LOCAL_MACHINE, strKeyPath, strEntryName, strValue).ToString())
        strValue = objReg.Value(HKEY_LOCAL_MACHINE, strKeyPath, strEntryName, strValue).ToString()
        MsgBox(strValue)
        If strValue = "1" Then
            comment = " (Autologin machine)"
        ElseIf strValue = "0" Then
            comment = " (Non Autologin machine)"
        End If
        text = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\AutoAdminLogon = " & strValue & comment
        strEntryName = "DefaultUserName"
        objReg.GetStringValue(HKEY_LOCAL_MACHINE, strKeyPath, strEntryName, strValue)
        text = text & "  HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultUserName = " & strValue
        MsgBox(text, , "Autologin Check")
    End If

End Sub

2 个答案:

答案 0 :(得分:0)

您可能遇到registry differences for 64-bit vs. 32-bit processesHKLM\SOFTWARE下的密钥从HKLM\SOFTWARE\Wow6432Node读取32位进程,导致结果混乱。如果使用RegEdit修改HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultUserName,您应该会看到它是您的代码读取的值。

您可以使用RegistryKey.OpenBaseKey方法和相应的view选项(.NET 4.0 +)从32位(或64位)进程读取64位注册表项:< / p>

Dim hklm = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64)
Dim winlogon = hklm.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon")
Dim val = winlogon.GetValue("DefaultUserName")
Console.WriteLine(val)

答案 1 :(得分:0)

感谢Mark,我可以获得此代码。它将能够获取网络上任何计算机的AutoAdminLogon和DefaultUserName注册表值

 Private Sub askComputer1(ByVal computer)

    MsgBox(computer)
    Dim environmentKey As RegistryKey
    Dim val1 As String, val2 As String
    environmentKey = RegistryKey.OpenRemoteBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, computer, RegistryView.Registry64).OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon")

    val1 = environmentKey.GetValue("AutoAdminLogon").ToString()
    val2 = environmentKey.GetValue("DefaultUserName").ToString()
    MsgBox(val1 & "   " & val2)
    environmentKey.Close()

End Sub