2008-08-12 32 views

Respuesta

6

Try this article - Tengo algo de código en el trabajo que el MTC, el trabajo si esto no es así ...

cita relevante:

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

Tuve la tentación de rechazar su respuesta de solo enlace, pero meh, citemos el afortunado enlace no podrido;) –

+0

Todo bien. Fue una respuesta bastante de mierda en la reflexión. Fue en los primeros días que, no solo vincular las respuestas, las cosas no eran tan rígidas como lo son hoy, en mi defensa;) De todos modos, gracias por la edición. – JamesSugrue

2

Depende de las variables de entorno para seguir siendo válidas es una mala idea, ya que pueden cambiarse fácilmente dentro de una sesión de usuario.

+0

¡Muy buen punto! – Yarik

2

David hizo una muy buena observación sobre el riesgo de usar variables de entorno. Solo puedo agregar que puede haber otros problemas con las variables de entorno. Basta con mirar a este fragmento de código real de nuestro proyecto de 5 años de edad:

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 
+0

No hay nada de malo con CurrentUser() si realmente está utilizando la seguridad de nivel de usuario de Jet. Por otro lado, si no necesita Jet ULS, el inicio de sesión de Windows es una excelente alternativa (aunque puede que tenga que mantener algún tipo de membresía de grupo en su aplicación). –

3

aquí está mi versión: se buscará lo que quiera:

'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.

Cuestiones relacionadas