ashrobo Posted April 8, 2003 Posted April 8, 2003 hi all, i've got the following code from MSDN but couldn't 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 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 Quote
Leaders quwiltw Posted April 8, 2003 Leaders Posted April 8, 2003 I'm not sure what the problem is but you can get the username from System.Environment. Quote --tim
ashrobo Posted April 8, 2003 Author Posted April 8, 2003 i'm trying to get the domain that the user is logged on. Quote
Leaders quwiltw Posted April 8, 2003 Leaders Posted April 8, 2003 Did you even take a second to look at the System.Environment before responding?:-\ Quote --tim
ashrobo Posted April 9, 2003 Author Posted April 9, 2003 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. Quote
Guest mutant Posted April 9, 2003 Posted April 9, 2003 Not sure if it will work but try this: System.Security.Principal.WindowsIdentity.GetCurrent.Name Quote
ashrobo Posted April 9, 2003 Author Posted April 9, 2003 System.Security.Principal.WindowsIdentity.GetCurrent() .Name works (it gives me the domain\username). guess i'll just have to use this.. thanks mutant! (= Quote
Leaders quwiltw Posted April 9, 2003 Leaders Posted April 9, 2003 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. Quote --tim
ashrobo Posted April 9, 2003 Author Posted April 9, 2003 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. Quote
subbu_vairavan Posted July 22, 2003 Posted July 22, 2003 Also if the user name and machine name are same, this property gets the computer name Quote
k_murali_k Posted September 13, 2003 Posted September 13, 2003 what if I want to findout user who logged in another system in the network? Murali Krishna Quote
Recommended Posts