Click here to Skip to main content
14,866,950 members
Articles / Programming Languages / Visual Basic
Posted 8 Jul 2005


37 bookmarked

Retrieving Logged In User Name using a Windows Service

Rate me:
Please Sign up or sign in to vote.
2.94/5 (13 votes)
8 Jul 20052 min read
This is a small program which describes how can we retrieve the username of the currently logged in user without using the old API functions. This method uses WMI classes to retrieve this information.


This is a small Windows service program which retrieves the username of the currently logged in user. There might arise some situations when you need to have this information through a Windows service. If we use traditional API functions like GetUserName or GetUserNameEx, they simply return the user name of the process account under which that Windows service is running. It is the SYSTEM account in most cases. The above mentioned API functions always return SYSTEM as the currently logged in user.

Main Idea

To over come this problem, we can use Windows Management Instrumentation (WMI) classes. The main idea is that we get the username of the Explorer process under which it is running. And we know that the Explorer process is always running under the account with which the current user has been logged in.

To use this code you need to download a WMI plug-in from here. It will install WMI extensions for Visual Studio .NET 2003 Server Explorer. Now follow the steps as mentioned:


  1. Create a Windows Service project using VB.NET.
  2. Open Server Explorer and expand the tree node which says Management Classes.
  3. Right click on Processes node and select "Generate Managed Class". This will add a reference to the System.Management namespace.
  4. Now import System.Management namespace in your project (Service1.vb).
  5. In the OnStart procedure, add the following code:
    Dim mc As New ManagementClass("Win32_Process")
    Dim moc As ManagementObjectCollection = mc.GetInstances
    Dim mo As ManagementObject
    Dim processDomain, processUser As String
     For Each mo In moc
         Dim p As New ROOT.CIMV2.Process(mo)
         p.GetOwner(processDomain, processUser)
           If (p.Name.Trim = "explorer.exe") Then
              Return processUser
              Exit For
           End If

The simple explanation of the above code is that it loops through all the running processes on the system and as it finds the "explorer.exe" process, it returns its username. And this is the username of the currently logged in user as well.

You can print this user name in a file by adding the following code:

Dim myFile As New _



Install the Windows service using InstallUtil and run and test it. For this purpose, read other articles on how to install and run a Windows service.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Baber Saeed
Web Developer
Pakistan Pakistan
No Biography provided

Comments and Discussions

SuggestionWhy not use Win32_LogonSession and Win32_LoggedOnUser? Pin
Ash201324-Sep-13 6:49
MemberAsh201324-Sep-13 6:49 
GeneralMy vote of 1 Pin
ant14887-Mar-13 3:59
Memberant14887-Mar-13 3:59 
GeneralMy vote of 1 Pin
Dennis O.11-Jan-12 4:24
professionalDennis O.11-Jan-12 4:24 
GeneralMy vote of 1 Pin
Anton Lesnichenko13-Jul-11 5:03
MemberAnton Lesnichenko13-Jul-11 5:03 
GeneralHere's how to do this in C/C++ Pin
BigBenF102417-Oct-08 10:19
MemberBigBenF102417-Oct-08 10:19 
GeneralRe: Here's how to do this in C/C++ Pin
ant14888-Mar-13 0:13
Memberant14888-Mar-13 0:13 
GeneralTry This to get current logged on user using windows service in c# PinPopular
pramodgupta2413-Jul-08 20:12
Memberpramodgupta2413-Jul-08 20:12 
GeneralRe: Try This to get current logged on user using windows service in c# Pin
vinorathna5-Aug-08 5:07
Membervinorathna5-Aug-08 5:07 
GeneralRe: Try This to get current logged on user using windows service in c# Pin
Bart_Rennes23-Feb-10 6:42
MemberBart_Rennes23-Feb-10 6:42 
GeneralNot 100% accurate Pin
TheWizardOfOz31-Mar-08 6:21
MemberTheWizardOfOz31-Mar-08 6:21 
GeneralRetrieving current logged-in username from windows serivce in VC++2005 Pin
anumadhu10-Apr-07 1:42
Memberanumadhu10-Apr-07 1:42 
Questionretrieve password Pin
Shayl Sawmynaden14-Mar-07 20:30
MemberShayl Sawmynaden14-Mar-07 20:30 
QuestionRe: retrieve password Pin
Member 40413811-Dec-08 23:51
MemberMember 40413811-Dec-08 23:51 
GeneralThanks a lot + can u suggest something for Shutdown Pin
BSRK16-Nov-06 0:49
MemberBSRK16-Nov-06 0:49 
QuestionRetrieve Logged in User Name using a Windows Service Pin
Don_Sartain2-Sep-06 6:19
MemberDon_Sartain2-Sep-06 6:19 
AnswerRe: Retrieve Logged in User Name using a Windows Service Pin
BSRK16-Nov-06 0:46
MemberBSRK16-Nov-06 0:46 
QuestionWinXP proglem Pin
plaff14-Aug-06 10:54
Memberplaff14-Aug-06 10:54 
AnswerRe: WinXP proglem Pin
BrownStar16-Nov-06 4:29
MemberBrownStar16-Nov-06 4:29 
QuestionSame problem using ManagementClass to get MAC Address Pin
John Boero7-Dec-06 11:07
MemberJohn Boero7-Dec-06 11:07 
AnswerBoo-yah! Pin
John Boero7-Dec-06 11:45
MemberJohn Boero7-Dec-06 11:45 
GeneralGREAT Pin
Jeremys78-Jun-06 11:42
MemberJeremys78-Jun-06 11:42 
QuestionRetrieve process user name Pin
George Hendrickson29-May-06 13:03
professionalGeorge Hendrickson29-May-06 13:03 
GeneralUser Name Pin
Tier510-May-06 7:43
MemberTier510-May-06 7:43 
GeneralWMI Event log API Pin
Vitoto1-Jan-06 11:27
MemberVitoto1-Jan-06 11:27 
QuestionWhy? Pin
5150.Net10-Oct-05 7:19
Member5150.Net10-Oct-05 7:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.