Q

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)
        Else
            Console.WriteLine("GetUserNameEx failed.")
        End If
    End Function

    Private Enum EXTENDED_NAME_FORMAT
        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

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.

0 comments

Oldest 

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:

-ADS BY GOOGLE

SearchCloudComputing

SearchSoftwareQuality

SearchSOA

TheServerSide

SearchCloudApplications

Close