Anyone know how to get the Username and Domain name of the user in windows NT 4.0?
My app is crashing because of this. Apparently, it is not the same as getting this information in Win2000.
Any code, thoughts, or suggestions are greatly appreciated!
Printable View
Anyone know how to get the Username and Domain name of the user in windows NT 4.0?
My app is crashing because of this. Apparently, it is not the same as getting this information in Win2000.
Any code, thoughts, or suggestions are greatly appreciated!
How are you getting it in Windows 2000? If you have WMI installed, and are using it, this is what I've gotten to work:
VB Code:
Private Function GetDomainViaWMI() Dim csThisComputer As New APIFileSecurity.ROOT.CIMV2.ComputerSystem() Dim objSysInfo As SystemInformation Dim strPath As String strPath = "\\" + objSysInfo.ComputerName + _ "\root\CIMV2:Win32_ComputerSystem.Name=""" + _ objSysInfo.ComputerName + """" csThisComputer.Path = New System.Management.ManagementPath(strPath) MsgBox("Domain is:" + ControlChars.Tab + csThisComputer.Domain) End Function
For user and machine name, these API calls were pretty easy to get to work (compared to NetWorkstationGetInfo anyhow)
VB Code:
Private Function GetComputerNameViaAPI() Dim strComputerName As String = Space(33) Dim iSize As Integer = 32 Win32APIFileSecurity.GetComputerName(strComputerName, iSize) 'get rid of trailing spaces strComputerName = strComputerName.Trim() Return strComputerName End Function Private Function GetUserNameViaAPI() Dim strUserName As String = Space(33) Dim iSize As Integer = 32 Win32APIFileSecurity.GetUserName(strUserName, iSize) 'get rid of trailing spaces strUserName = strUserName.Trim() Return strUserName End Function
VB Code:
'in a module Public Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _ (ByVal lpBuffer As String, _ ByRef nSize As Integer) As Integer Public Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" _ (ByVal lpBuffer As String, _ ByRef nSize As Integer) As Integer
Woops, by the way the above WMI bit depends on the following attached file as part of your project. This file was added by the WMI extensions, downloadable from here:
http://www.microsoft.com/downloads/d...8-6E22115FFAF0
Wow! Thats a whole lot of functionality in there!! Cool.
Yeah that's kind of a problem with it really, it's a big monolithic chunk of code that the WMI extensions adds to your app whether you use it or not. It inflates your EXE size quite a bit, although the stuff you're not using gets compiled out at runtime.
You ought to download the rest of the kit, the docs are sorely lacking but when the WMI extension kit is installed you can drag/drop stuff from Server Explorer and get an idea of the syntax of the various classes.
Dim username As String
username = System.Environment.UserName
Dim domainname As String
domainname = System.Environment.UserDomainName
I was just wondering.. the above post did it for me.. I just needed the username..Quote:
Originally posted by bilko73
Dim username As String
username = System.Environment.UserName
Dim domainname As String
domainname = System.Environment.UserDomainName
It just seemed so simple when you compare it to the other solutions in this thread.
Are there any downsides to the System.Environment.UserName
call??
When i tried .UserDomainName it gave me my machine name, but i'm not on a domain - just a workgroup.
This is the .NET way to get the username. I know of no disadvantages to doing it this way.Quote:
I was just wondering.. the above post did it for me.. I just needed the username..