La Web del Programador: Comunidad de Programadores
 
    Pregunta:  13391 - CAPTURAR EL USUARI NT
Autor:  bruno oab
!Que tal!
hay alguna forma de capturar en vb 6.0 el usuario de NT con que me logueo a la red. ..?

  Respuesta:  Luis Sanz Lucas
'-----------------------------------------
' Declaraciones Necesarias ---
'---------------------------------------

Private Declare Function GetVersionEx& Lib "kernel32" Alias "GetVersionExA" (lpStruct As OsVersionInfo)

Private OsVers As OsVersionInfo

Private Type OsVersionInfo
dwVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatform As Long
szCSDVersion As String * 128
End Type

Private CurrOS As String
' NT password setup info
Private Declare Function WNetGetUser Lib "mpr.dll" Alias "WNetGetUserA" (ByVal lpName As String, ByVal lpUserName As String, lpnLength As Long) As Long

Private Declare Function NetGetDCName Lib "NETAPI32.DLL" (ServerName As Byte, DomainName As Byte, DCName As Long) As Long

Private Declare Function PtrToStr Lib "kernel32" Alias "lstrcpyW" (RetVal As Byte, ByVal Ptr As Long) As Long

Private Declare Function NetAPIBufferFree Lib "NETAPI32.DLL" Alias "NetApiBufferFree" (ByVal Ptr As Long) As Long

' Password stuff
Private Declare Function PwdChangePassword& Lib "mpr" Alias "PwdChangePasswordA" (ByVal lpcRegkeyname$, ByVal hWnd&, ByVal uiReserved1&, ByVal uiReserved2&)

Private Declare Function WNetVerifyPassword Lib "mpr.dll" Alias "WNetVerifyPasswordA" (ByVal lpszPassword As String, ByRef pfMatch As Long) As Long

Private Declare Function NetUserChangePassword Lib "NETAPI32.DLL" (DomainName As Byte, Username As Byte, OldPassword As Byte, NewPassword As Byte) As Long

Y Luego te declaras las siguientes funciones:

Public Function NombreUsuario() As String
Dim pwlong, Res As Long
Dim DC, User, S1 As String
Dim bPDCName() As Byte
Dim bUserName() As Byte
Dim bOldPW() As Byte
Dim bNewPW() As Byte

CurrOS = GetVersion32

If CurrOS <> "NT" Then
' Si no es para NT no hago nada
Exit Function
End If
' Get the primary domain controller name
DC = GetPrimaryDCName("", "")
DC = Left(DC, (InStr(1, DC, Chr(0), vbBinaryCompare) - 1))
' Get the current user name
S1 = Space(512)
WNetGetUser vbNullString, S1, Len(S1)
User = Left(S1, (InStr(1, S1, Chr(0), vbBinaryCompare) - 1))

NombreUsuario = User
End Function

Public Function GetVersion32() As String
' Call to get the 32 Bit O/S ID. Returned values are either "95" or "NT" or "Unknown"
' Example - MyString = GetVersion32
'
OsVers.dwVersionInfoSize = 148&
GetVersionEx OsVers
If OsVers.dwPlatform = 1& Then
GetVersion32 = "95"
ElseIf OsVers.dwPlatform = 2& Then
GetVersion32 = "NT"
Else
GetVersion32 = "Unknown"
End If
End Function
Function GetPrimaryDCName(ByVal MName As String, ByVal DName As String) As String
Dim DCName As String, DCNPtr As Long
Dim DNArray() As Byte
Dim MNArray() As Byte
Dim DCNArray(100) As Byte
Dim Result As Long
MNArray = MName & vbNullChar
DNArray = DName & vbNullChar
Result = NetGetDCName(MNArray(0), DNArray(0), DCNPtr)
If Result <> 0 Then
MsgBox "Error: " & Result
Exit Function
End If
Result = PtrToStr(DCNArray(0), DCNPtr)
Result = NetAPIBufferFree(DCNPtr)
DCName = DCNArray()
GetPrimaryDCName = DCName
End Function

Y luego con llamar a la función NombreUsuario tienes el usuario de red.

Espero que esto te sirva.