VB .NET and API

ashrobo

Well-known member
Joined
Apr 6, 2003
Messages
70
Location
Black Hole
hi all, ive got the following code from MSDN but couldnt get it to work. :(

Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, ByVal nSize As Integer) As Integer

Dim Ret As Integer
Dim UserName As String
Dim Buffer As String
Buffer = New String(CChar(" "), 25)
Ret = GetUserName(Buffer, 25)
UserName = Microsoft.VisualBasic.Left(Buffer, InStr(Buffer, Chr(0)) - 1)
MsgBox(UserName)

i added Microsoft.VisualBasic. because it wouldnt allow me to use Left

encountered the following error while trying to run it.

>> Ret = GetUserName(Buffer, 25) error occurs here

An unhandled exception of type System.NullReferenceException occurred in WindowsApplication3.exe

Additional information: Object reference not set to an instance of an object.

has anyone encountered this before? all help will be appreciated!

-ashrobo
 
Originally posted by quwiltw
Did you even take a second to look at the System.Environment before responding?:-\
just in case youre wondering, yes i have. Environment.UserName gets me the username of the user thats logged on, Environment.UserDomainName gives me the machine name. ive tried them before asking of course.
 
Not sure if it will work but try this:
Code:
System.Security.Principal.WindowsIdentity.GetCurrent.Name
 
System.Security.Principal.WindowsIdentity.GetCurrent() .Name works (it gives me the domain\username). guess ill just have to use this.. thanks mutant! (=
 
System.Environment.UserDomainName() returns the network domain that youre currently logged on to just as the documentation says. Not sure why it would return the machine name in your case.
 
i have no idea why that happens about System.Environment.UserDomainName() but i found out why it is so with the SystemInformation.UserDomainName()
The name of the user domain. If a local user account exists with the same name as the user name, this property gets the computer name.
 
Back
Top