-
October 14th, 2010, 05:10 AM
#1
System Process : Get currently logged on user?
We have a small windows application that we need to run as a system process. However, somewhere in the application code, we need to get the login ID of the currently logged on user.
If we use the GetUserName function, it will return SYSTEM since this is running as a system application. So, is there a way for an application running as a system process to get the login ID / username of the currently logged-on user?
-
October 14th, 2010, 10:52 AM
#2
Re: System Process : Get currently logged on user?
Try this:
Code:
Option Explicit
Private Enum EXTENDED_NAME_FORMAT
NameUnknown = 0
NameFullyQualifiedDN = 1
NameSamCompatible = 2
NameDisplay = 3
NameUniqueId = 6
NameCanonical = 7
NameUserPrincipal = 8
NameCanonicalEx = 9
NameServicePrincipal = 10
End Enum
Private Declare Function GetUserNameEx Lib "secur32.dll" Alias "GetUserNameExA" (ByVal NameFormat As EXTENDED_NAME_FORMAT, ByVal lpNameBuffer As String, ByRef nSize As Long) As Long
Private Sub Form_Load()
'KPD-Team 2001
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@allapi.net
Dim sBuffer As String, Ret As Long
sBuffer = String(256, 0)
Ret = Len(sBuffer)
If GetUserNameEx(NameSamCompatible, sBuffer, Ret) <> 0 Then
MsgBox "Username: " + Left$(sBuffer, Ret)
Else
MsgBox "Error while retrieving the username"
End If
End Sub
-
October 14th, 2010, 09:54 PM
#3
Re: System Process : Get currently logged on user?
Originally Posted by dglienna
Try this:
Code:
Option Explicit
Private Enum EXTENDED_NAME_FORMAT
NameUnknown = 0
NameFullyQualifiedDN = 1
NameSamCompatible = 2
NameDisplay = 3
NameUniqueId = 6
NameCanonical = 7
NameUserPrincipal = 8
NameCanonicalEx = 9
NameServicePrincipal = 10
End Enum
Private Declare Function GetUserNameEx Lib "secur32.dll" Alias "GetUserNameExA" (ByVal NameFormat As EXTENDED_NAME_FORMAT, ByVal lpNameBuffer As String, ByRef nSize As Long) As Long
Private Sub Form_Load()
'KPD-Team 2001
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@allapi.net
Dim sBuffer As String, Ret As Long
sBuffer = String(256, 0)
Ret = Len(sBuffer)
If GetUserNameEx(NameSamCompatible, sBuffer, Ret) <> 0 Then
MsgBox "Username: " + Left$(sBuffer, Ret)
Else
MsgBox "Error while retrieving the username"
End If
End Sub
This returns NTADMIN\<computer name>$
-
October 14th, 2010, 11:53 PM
#4
Re: System Process : Get currently logged on user?
-
October 24th, 2010, 05:05 PM
#5
Re: System Process : Get currently logged on user?
You can use the WSH library, if you want.
Code:
strUser = CreateObject("WScript.Network").UserName
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
-
October 27th, 2010, 08:43 AM
#6
Re: System Process : Get currently logged on user?
There's also the GetUserName API, plus a few other ways:
http://www.vbforums.com/showthread.php?t=357723
Please remember to rate the posts and threads that you find useful.
How can something be both new and improved at the same time?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|