Results 1 to 9 of 9

Thread: Can get User and Domain in Win2000 - but how in NT?

  1. #1

    Thread Starter
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681

    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!

  2. #2
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    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:
    1. Private Function GetDomainViaWMI()
    2.         Dim csThisComputer As New APIFileSecurity.ROOT.CIMV2.ComputerSystem()
    3.         Dim objSysInfo As SystemInformation
    4.         Dim strPath As String
    5.         strPath = "\\" + objSysInfo.ComputerName + _
    6.             "\root\CIMV2:Win32_ComputerSystem.Name=""" + _
    7.             objSysInfo.ComputerName + """"
    8.         csThisComputer.Path = New System.Management.ManagementPath(strPath)
    9.         MsgBox("Domain is:" + ControlChars.Tab + csThisComputer.Domain)
    10.     End Function

    For user and machine name, these API calls were pretty easy to get to work (compared to NetWorkstationGetInfo anyhow)

    VB Code:
    1. Private Function GetComputerNameViaAPI()
    2.         Dim strComputerName As String = Space(33)
    3.         Dim iSize As Integer = 32
    4.         Win32APIFileSecurity.GetComputerName(strComputerName, iSize)
    5.         'get rid of trailing spaces
    6.         strComputerName = strComputerName.Trim()
    7.         Return strComputerName
    8.     End Function
    9.  
    10.     Private Function GetUserNameViaAPI()
    11.         Dim strUserName As String = Space(33)
    12.         Dim iSize As Integer = 32
    13.         Win32APIFileSecurity.GetUserName(strUserName, iSize)
    14.         'get rid of trailing spaces
    15.         strUserName = strUserName.Trim()
    16.         Return strUserName
    17.     End Function

    VB Code:
    1. 'in a module
    2.     Public Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _
    3.         (ByVal lpBuffer As String, _
    4.          ByRef nSize As Integer) As Integer
    5.  
    6.     Public Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" _
    7.         (ByVal lpBuffer As String, _
    8.          ByRef nSize As Integer) As Integer
    Last edited by Slow_Learner; May 21st, 2003 at 09:56 AM.

  3. #3
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    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 Attached Files

  4. #4

    Thread Starter
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    Wow! Thats a whole lot of functionality in there!! Cool.

  5. #5
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    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.

  6. #6
    Junior Member
    Join Date
    Sep 2002
    Location
    Florida
    Posts
    24
    Dim username As String
    username = System.Environment.UserName

    Dim domainname As String
    domainname = System.Environment.UserDomainName

  7. #7
    Junior Member ost's Avatar
    Join Date
    Jan 2004
    Posts
    19
    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??

  8. #8
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Question

    When i tried .UserDomainName it gave me my machine name, but i'm not on a domain - just a workgroup.
    ~Peter


  9. #9
    Junior Member
    Join Date
    Sep 2002
    Location
    Florida
    Posts
    24
    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
  •  



Click Here to Expand Forum to Full Width