使用WMI获得VBA当前Windows用户名 [英] Use WMI to get current Windows username in VBA
本文介绍了使用WMI获得VBA当前Windows用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道是否有使用WMI让你与域当前的Windows用户名的简单方法。 Windows API调用只是让你短用户名,所以你最终做另一个呼叫的域名。我有一些code,但我得到一个自动化错误。
有任何想法吗?我觉得我在正确的道路上,但我是一个有点新的WMI。
I was wondering if there was a simple way to use WMI to get you the current windows user name with domain. The Windows API call just gets you the short username, so you end up doing another call for the domain name. I have some code, but I get an automation error. Any ideas? I think I'm on the right path, but I am a little new to WMI.
Function GetFullName() As String
Dim computer As String
computer = "."
Dim objWMIService, colProcessList As Object
Set objWMIService = GetObject("winmgmts:\\" & computer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("SELECT TOP 1 * FROM Win32_Process WHERE Name = 'EXCEL.EXE'")
Dim uname, udomain As String
Dim objProcess As Object
For Each objProcess In colProcessList
objProcess.GetOwner uname, udomain
Next
GetFullName = UCase(udomain) & "\" & UCase(uname)
End Function
更新:看到接受的答案评论
UPDATE: see comments on accepted answer
推荐答案
有在WQL没有TOP 1子句。离开它和您的查询应该工作:
There is no TOP 1 clause in WQL. Leave it out and your query should work:
"SELECT * FROM Win32_Process WHERE Name = 'EXCEL.EXE'"
这篇关于使用WMI获得VBA当前Windows用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文