Ask the Expert

Retrieving a user ID using VB.NET

I have a program from which I need to retrieve the iser ID from the computer for further purposes. Is there a way in VB.NET that allows the program to get the ID that a user used to logged into a computer?
Imports System.Runtime.InteropServices

Module MainModule

    Function Main() As Int32
        Dim n As Int32 = 255
        Dim x As String = New String(" ", n)
        Dim ret As Int32 = 0
        ret = GetUserNameEx(EXTENDED_NAME_FORMAT.NameSamCompatible, x, n)
        If (ret = 1) Then
            Console.WriteLine("User: {0}", x)
            Console.WriteLine("GetUserNameEx failed.")
        End If
    End Function

        NameUnknown = 0
        NameFullyQualifiedDN = 1
        NameSamCompatible = 2
        NameDisplay = 3
        NameUniqueId = 6
        NameCanonical = 7
        NameUserPrincipal = 8
        NameCanonicalEx = 9
        NameServicePrincipal = 10
        NameDnsDomain = 12
    End Enum

    Private Declare Function GetUserNameEx Lib "secur32.dll" _
        Alias "GetUserNameExA" (ByVal NameFormat As EXTENDED_NAME_FORMAT, _
                    ByVal NameBuffer As String, _
                    ByRef nSize As Int32) As Integer

End Module

This was first published in January 2004

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: