当前位置:  开发笔记 > 后端 > 正文

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

如何解决《使用VB6,如何在WindowsXP上获取当前用户和域?》经验,为你挑选了2个好方法。

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

谢谢



1> Tomalak..:

一种方法是询问环境:

Dim UserName As String
Dim UserDomain As String
UserName   = Environ("USERNAME")
UserDomain = Environ("USERDOMAIN")

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



2> Stefan..:

和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

推荐阅读
Life一切安好
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有