Bonjour,
La fonction suivante avec la partie déclaration (obligatoire) te permet de récupérer le nom de l'utilisateur.
@+
Option Explicit
Option Base 1
' ======= Declaration des API pour récupérer le USER NAME ================
' Declare Function NetWkstaGetInfo Lib "NETAPI.DLL" (ByVal pszServer As String, ByVal sLevel As Integer, pbBuffer As Any, ByVal cbBuffer As Integer, pcbTotalAvail As Integer) As Integer
Declare Function WNetGetUser Lib "mpr" Alias "WNetGetUserA" (ByVal lpName As String, ByVal lpUserName As String, lpnLength As Long) As Long
Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
Type WKSTA_INFO_101
wki101_platform_id As Long
wki101_computername As Long
wki101_langroup As Long
wki101_ver_major As Long
wki101_ver_minor As Long
wki101_lanroot As Long
End Type
Type WKSTA_USER_INFO_1
wkui1_username As Long
wkui1_logon_domain As Long
wkui1_logon_server As Long
wkui1_oth_domains As Long
End Type
'*****************************************************
' Fonction GetUserName
' Récupère le nom de l'utilisateur connecté (réseau)
'*****************************************************
Function GetUserName() As String
Dim ret As Long, buffer(512) As Byte, i As Integer
Dim wk101 As WKSTA_INFO_101, pwk101 As Long
Dim wk1 As WKSTA_USER_INFO_1, pwk1 As Long
Dim cbusername As Long, UserName As String
Dim computername As String, langroup As String, logondomain As String
' Clear all of the display values.
computername = "": langroup = "": UserName = "": logondomain = ""
' Windows 95 or NT - call WNetGetUser to get the name of the user.
UserName = Space(256)
cbusername = Len(UserName)
ret = WNetGetUser("", UserName, cbusername)
UserName = UCase(UserName)
If ret = 0 Then
' Success - strip off the null.
UserName = left(UserName, InStr(UserName, Chr(0)) - 1)
Else
UserName = ""
End If
GetUserName = UserName
End Function