使用VB6,如何在Windows XP上获取当前用户和域?

Jed*_*oky 5 vb6 windows-xp

我需要当前用户和域名.我正在使用VB 6应用程序.

谢谢

Tom*_*lak 15

一种方法是询问环境:

Dim UserName As String
Dim UserDomain As String
UserName   = Environ("USERNAME")
UserDomain = Environ("USERDOMAIN")
Run Code Online (Sandbox Code Playgroud)

(显然适用于Windows NT及以上版本.)


Ste*_*fan 8

和API版本:

Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long  

Declare Function LookupAccountName Lib "advapi32.dll" Alias "LookupAccountNameA" (lpSystemName As String, ByVal lpAccountName As String, sid As Any, cbSid As Long, ByVal ReferencedDomainName As String, cbReferencedDomainName As Long, peUse As Long) As Long

Private Sub Form_Load()  
     Dim sDomainName As String * 255   
     Dim lDomainNameLength As Long     
     Dim sUserName as String
     Dim bUserSid(255) As Byte      
     Dim lSIDType As Long 

    Rem Create a buffer
    sUserName = String(100, Chr$(0))  

    Rem Get the username
     GetUserName sUserName, 100  

    Rem strip the rest of the buffer
    sUserName = Left$(sUserName, InStr(sUserName, Chr$(0)) - 1)

     rem Show the temppath and the username
     MsgBox "Hello " + strUserName 

     lResult = LookupAccountName(vbNullString, sUserName, bUserSid(0), 255, sDomainName, lDomainNameLength, _
  lSIDType)
    if lResult <>0 then
       msgbox sDomainName
    end if
end sub
Run Code Online (Sandbox Code Playgroud)