2008-08-12 46 views

回答

6

Try this article - 我有,将企业风险管理,工作,如果这不起作用的一些代码...

相关报价:

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

Function ThisUserName() As String 
    Dim LngBufLen As Long 
    Dim strUser As String 

    strUser = String$(15, " ") 
    LngBufLen = 15 

    If GetUserName(strUser, LngBufLen) = 1 Then 
     ThisUserName = Left(strUser, LngBufLen - 1) 
    Else 
     ThisUserName = "Unknown" 
    End If 
End Function 

Function ThisComputerID() As String 
    Dim LngBufLen As Long 
    Dim strUser As String 

    strUser = String$(15, " ") 
    LngBufLen = 15 

    If GetComputerName(strUser, LngBufLen) = 1 Then 
     ThisComputerID = Left(strUser, LngBufLen) 
    Else 
     ThisComputerID = 0 
    End If 
End Function 
+0

我很想试着回答你的链接专有的答案,但是,只要引用幸运的话不要烂链接;) – 2017-04-07 06:10:09

+0

所有的好。是一个非常糟糕的回答反思。是在早期的时候,不仅仅是把答案连接起来,现在的情况并不像现在这样严格,我的辩护;)无论如何,感谢编辑。 – JamesSugrue 2017-04-09 21:15:13

2

取决于环境变量保持有效是一个坏主意,因为它们可以在用户会话中轻松更改。

+0

非常好的一点! – Yarik 2008-10-30 10:14:12

2

David对使用环境变量的风险提出了一个非常好的观点。我只能补充说环境变量可能存在其他问题。只要看看这个实际的代码片段从我们5岁的项目:

Public Function CurrentWorkbenchUser() As String 

    ' 2004-01-05, YM: Using Application.CurrentUser for identification of 
    ' current user is very problematic (more specifically, extremely 
    ' cumbersome to set up and administer for all users). 
    ' Therefore, as a quick fix, let's use the OS-level user's 
    ' identity instead (NB: the environment variables used below must work fine 
    ' on Windows NT/2000/2003 but may not work on Windows 98/ME) 
    ' CurrentWorkbenchUser = Application.CurrentUser 
    ' 
    ' 2005-06-13, YM: Environment variables do not work in Windows 2003. 
    ' Use Windows Scripting Host (WSH) Networking object instead. 
    ' CurrentWorkbenchUser = Environ("UserDomain") & "\" & Environ("UserName") 
    ' 
    ' 2007-01-23, YM: Somewhere between 2007-01-09 and 2007-01-20, 
    ' the WshNetwork object stopped working on CONTROLLER3. 
    ' We could not find any easy way to fix that. 
    ' At the same time, it turns out that environment variables 
    ' do work on Windows 2003. 
    ' (Apparently, it was some weird configuration problem back in 2005: 
    ' we had only one Windows 2003 computer at that time and it was 
    ' Will's workstation). 
    ' 
    ' In any case, at the time of this writing, 
    ' returning to environment variables 
    ' appears to be the simplest solution to the problem on CONTROLLER3. 
    ' Dim wshn As New WshNetwork 
    ' CurrentWorkbenchUser = wshn.UserDomain & "\" & wshn.UserName 

    CurrentWorkbenchUser = Environ("USERDOMAIN") & "\" & Environ("USERNAME") 

End Function 
3

这里是我的版本:它会取你喜欢的东西:

'gets firstname, lastname, fullname or username 
Public Function GetUser(Optional whatpart = "username") 
    Dim returnthis As String 
    If whatpart = "username" Then GetUser = Environ("USERNAME"): Exit Function 
    Set objSysInfo = CreateObject("ADSystemInfo") 
    Set objUser = GetObject("LDAP://" & objSysInfo.USERNAME) 
    Select Case whatpart 
     Case "fullname": returnthis = objUser.FullName 
     Case "firstname", "givenname": returnthis = objUser.givenName 
     Case "lastname": returnthis = objUser.LastName 
     Case Else: returnthis = Environ("USERNAME") 
    End Select 
    GetUser = returnthis 
End Function 

I got the original idea from Spiceworks

相关问题