May 21st, 2003, 08:26 AM
#1
Thread Starter
Fanatic Member
Can get User and Domain in Win2000 - but how in NT?
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!
May 21st, 2003, 09:50 AM
#2
Fanatic Member
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
Last edited by Slow_Learner; May 21st, 2003 at 09:56 AM .
May 21st, 2003, 09:55 AM
#3
Fanatic Member
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
Attached Files
May 21st, 2003, 10:12 AM
#4
Thread Starter
Fanatic Member
Wow! Thats a whole lot of functionality in there!! Cool.
May 21st, 2003, 10:19 AM
#5
Fanatic Member
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.
Jun 18th, 2003, 01:24 PM
#6
Junior Member
Dim username As String
username = System.Environment.UserName
Dim domainname As String
domainname = System.Environment.UserDomainName
Feb 6th, 2004, 05:17 AM
#7
Junior Member
Originally posted by bilko73
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..
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??
Feb 6th, 2004, 04:14 PM
#8
Frenzied Member
When i tried .UserDomainName it gave me my machine name, but i'm not on a domain - just a workgroup.
~Peter
Feb 6th, 2004, 04:31 PM
#9
Junior Member
I was just wondering.. the above post did it for me.. I just needed the username..
This is the .NET way to get the username. I know of no disadvantages to doing it this way.
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width