Problem solve Get help with specific problems with your technologies, process and projects.

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 last published in January 2004

Dig Deeper on VB 6 to VB .NET Migration

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

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

Please create a username to comment.