Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

hi all, i've got the following code from MSDN but couldn't get it to work. :(

 

  Quote

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 wouldn't 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

Posted
  quwiltw said:
Did you even take a second to look at the System.Environment before responding?:-\

just in case you're wondering, yes i have. Environment.UserName gets me the username of the user that's logged on, Environment.UserDomainName gives me the machine name. i've tried them before asking of course.

Guest mutant
Posted

Not sure if it will work but try this:

System.Security.Principal.WindowsIdentity.GetCurrent.Name

Posted
System.Security.Principal.WindowsIdentity.GetCurrent() .Name works (it gives me the domain\username). guess i'll just have to use this.. thanks mutant! (=
  • Leaders
Posted
System.Environment.UserDomainName() returns the network domain that you're currently logged on to just as the documentation says. Not sure why it would return the machine name in your case.
--tim
Posted

i have no idea why that happens about System.Environment.UserDomainName() but i found out why it is so with the SystemInformation.UserDomainName()

  Quote
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.
  • 3 months later...
  • 1 month later...
Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...