Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

environ("username") versus advapi32.dll

I know there are at least 2 ways of retrieving the username in an Access application.

You can use the environ function:

environ("username")

And you can use GetUsername in advapi32.dll

Public Declare Function GetUserName& Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long)

s = String(l, Chr(32))
GetUserName s, l
username = Left$(s, l - 1)

Which one of the above methods is the safest to use? And why?

Perhaps some background info, the applications are used both on the local computers and remote desktops.

like image 996
SouthL Avatar asked Nov 26 '25 19:11

SouthL


1 Answers

As Simon has said, Environ variables are open to manipulation, however some people also like to avoid the api calls, if this is the case then this is a simple to follow alternative:

Public Function GetUser() As String

    Dim WNet As Object

    Set WNet = CreateObject("WScript.Network")

    GetUser = WNet.UserName

    Set WNet = Nothing

End Function
like image 172
Matt Donnan Avatar answered Nov 29 '25 18:11

Matt Donnan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!