Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
4.00/5 (3 votes)
See more:
I have this script i have been using for windows 7 to get the last logged in user. It works great as a vbscript but when i try and convert it to work in my application i get a number back vice the users name. Please help. below is the script i have been using.

VB
Const HKEY_LOCAL_MACHINE = &H80000002

strComputer = "127.0.0.1"
 
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
    strComputer & "\root\default:StdRegProv")
 
strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI"
strValueName = "LastLoggedOnUser"
oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
Wscript.Echo "Current LastLoggedOnUser Value: " & strValue


now i have tried converting it and that code is below. All i get is a number 1 not the name. Help would be great.

VB
Const HKEY_LOCAL_MACHINE = &H80000002

strComputer = "127.0.0.1"

Dim oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
    strComputer & "\root\default:StdRegProv")

Dim strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI"
Dim strValueName = "LastLoggedOnUser"
Dim strvalue = oReg.GetStringValue(HKEY_LOCAL_MACHINE, strKeyPath, strValueName)
MsgBox("Current LastLoggedOnUser Value: " & strvalue.ToString)
Posted
Updated 5-Apr-17 19:20pm

1 solution

This is probably because you are on x64 machine.

Check out this code.


VB
Imports Microsoft.Win32

Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim value As String
        Dim localKey = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64)

        localKey = localKey.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI")

        Dim value64 = localKey.GetValue("LastLoggedOnUser")

        Dim localKey32 = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry32)
        localKey32 = localKey32.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI")

        Dim value32 = localKey32.GetValue("LastLoggedOnUser")

        If value64 = Nothing Then
            value = value32
        Else
            value = value64
        End If

        MsgBox(value.ToString())
    End Sub
End Class


EDIT post comments:

for a solution that access the remote machines registry check this:

VB
Dim value As String
       Dim strComputer As String = "Zack-PC"

       Dim localKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, strComputer, RegistryView.Registry64)
       localKey = localKey.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI")
       Dim value64 = localKey.GetValue("LastLoggedOnUser")

       Dim localKey32 = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, strComputer, RegistryView.Registry32)
       localKey32 = localKey32.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI")
       Dim value32 = localKey32.GetValue("LastLoggedOnUser")

       If value64 = Nothing Then
           value = value32
       Else
           value = value64
       End If

       MsgBox(value.ToString)



Cheers
 
Share this answer
 
v2
Comments
Zachary.shupp 10-Apr-12 12:28pm    
this works great for the local machine but i need to be able to read a remote machine how would i add that ability to what you have?
Mario Majčica 10-Apr-12 13:09pm    
I'm via cell phone. However check this http://msdn.microsoft.com/en-us/library/8zha3xws(v=vs.71).aspx
Zachary.shupp 10-Apr-12 19:33pm    
I just got it working and all i had to do was change the OpenbaseKey to OpenRemoteBasekey and add the machine name. Thank you so much for the help.

Dim value As String
Dim strComputer As String = "Zack-PC"

Dim localKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, strComputer, RegistryView.Registry64)
localKey = localKey.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI")
Dim value64 = localKey.GetValue("LastLoggedOnUser")

Dim localKey32 = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, strComputer, RegistryView.Registry32)
localKey32 = localKey32.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI")
Dim value32 = localKey32.GetValue("LastLoggedOnUser")

If value64 = Nothing Then
value = value32
Else
value = value64
End If

MsgBox(value.ToString)
Mario Majčica 11-Apr-12 3:28am    
I updated the solution. Vote if willing ;)

Cheers
member60 11-Apr-12 3:29am    
my 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900