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

发布于 2024-12-28 19:11:19 字数 447 浏览 4 评论 0原文

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

您可以使用environ函数:

environ("username")

并且您可以在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)

上述哪一种方法使用起来最安全?为什么?

也许有一些背景信息,这些应用程序既可以在本地计算机上使用,也可以在远程桌面上使用。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

救星 2025-01-04 19:11:19

正如 Simon 所说,环境变量可以进行操作,但是有些人也喜欢避免 api 调用,如果是这种情况,那么这是一个简单易行的替代方案:

Public Function GetUser() As String

    Dim WNet As Object

    Set WNet = CreateObject("WScript.Network")

    GetUser = WNet.UserName

    Set WNet = Nothing

End Function

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
如梦亦如幻 2025-01-04 19:11:19

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

我肯定会选择 advapi。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文