vba 环境(“用户名”)与 advapi32.dll

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8987561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 15:01:30  来源:igfitidea点击:

environ("username") versus advapi32.dll

vbaaccess-vbams-access-2003

提问by SouthL

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

我知道至少有两种方法可以在 Access 应用程序中检索用户名。

You can use the environ function:

您可以使用环境功能:

environ("username")

And you can use GetUsername in advapi32.dll

你可以在 advapi32.dll 中使用 GetUsername

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.

也许是一些背景信息,这些应用程序在本地计算机和远程桌面上都使用。

回答by Matt Donnan

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:

正如 Simon 所说,Environ 变量对操作是开放的,但是有些人也喜欢避免 api 调用,如果是这种情况,那么这是一个简单的替代方案:

Public Function GetUser() As String

    Dim WNet As Object

    Set WNet = CreateObject("WScript.Network")

    GetUser = WNet.UserName

    Set WNet = Nothing

End Function

回答by Simon Richter

Environment variables can be set and unset by anyone, go missing and whatnot, and these cases tend to be difficult to reproduce if anyone even thinks of it as a source of errors.

环境变量可以由任何人设置和取消设置,丢失等等,如果有人甚至认为它是错误的来源,这些情况往往难以重现。

I'd definitely go with advapi.

我肯定会选择 advapi。