vba 如何在 VB 中为 64 位/Access 2013 检索 Windows 用户 ID?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16847583/
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 21:22:28  来源:igfitidea点击:

How to retrieve Windows userID in VB for 64-bit/Access 2013?

vba64-bitms-access-2013

提问by philiptdotcom

I need to get code to retrieve the Windows userID for the current session in VB (for Access 2013) on a 64-bit system.

我需要获取代码以在 64 位系统上的 VB(对于 Access 2013)中检索当前会话的 Windows 用户 ID。

I've tried the solution suggested at How to get logged-in user's name in Access vba?, but apparently this doesn't work on my 64-bit machine. I've also tried to figure out how to integrate the info at http://msdn.microsoft.com/en-us/library/office/gg278832.aspx, but I can't figure it out.

我已经尝试了如何在 Access vba 中获取登录用户名中建议的解决方案,但显然这在我的 64 位机器上不起作用。我还试图弄清楚如何在http://msdn.microsoft.com/en-us/library/office/gg278832.aspx上集成信息,但我无法弄清楚。

I am a NOVICEVB programmer, so I really need the actual code to do this. (I can [probably] figure out how & why the code does what it does after I see it, but I can't come up with it from scratch at this point.)

我是一个新手VB 程序员,所以我真的需要实际的代码来做到这一点。(我可以[可能]在我看到代码后弄清楚它是如何以及为什么这样做的,但此时我无法从头开始想出它。)

I'm hoping this answer will be helpful to others, too.

我希望这个答案对其他人也有帮助。

Thanks so much!

非常感谢!

Aloha, -pt

阿罗哈,-pt

回答by assylias

The answer you linked to works on a 32 bit version of access. For 64 bit versions, you need to use a pointer-safe signature:

您链接到的答案适用于 32 位版本的访问。对于 64 位版本,您需要使用指针安全签名:

Private Declare PtrSafe Function GetUserName Lib "advapi32.dll" Alias
 "GetUserNameA" (ByVal lpBuffer As String, nSize As LongPtr) As Long

(it might work with nSize As Long- I don't have a 64-bit access at hand)

(它可能适用nSize As Long- 我手头没有 64 位访问权限)

回答by Gord Thompson

This should work, too:

这也应该有效:

Dim wshNet As Object
Set wshNet = CreateObject("WScript.Network")
MsgBox "Hello, " & wshNet.UserName & "!"
Set wshNet = Nothing