Click here to Skip to main content
15,881,812 members
Articles / Programming Languages / Visual Basic
Article

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 255.1K   37   43
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.

Introduction

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:

Steps

  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:
    VB
    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
      
     Next

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:

VB
Dim myFile As New _
  System.Diagnostics.TextWriterTraceListener("C:\UserName.txt")

Trace.Listeners.Add(myFile)

myFile.WriteLine(processUser)

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.

License

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


Written By
Web Developer
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
AnswerRe: Return? Pin
Baber Saeed28-Aug-05 5:42
Baber Saeed28-Aug-05 5:42 
GeneralRe: Return? Pin
Don_Sartain2-Sep-06 6:22
Don_Sartain2-Sep-06 6:22 
GeneralRe: Return? Pin
BSRK16-Nov-06 0:38
BSRK16-Nov-06 0:38 
GeneralLogonUser Pin
Donsw21-Aug-05 14:14
Donsw21-Aug-05 14:14 
GeneralRe: LogonUser Pin
Baber Saeed28-Aug-05 5:52
Baber Saeed28-Aug-05 5:52 
GeneralUseful technic Pin
Keyvan Nayyeri8-Jul-05 17:13
Keyvan Nayyeri8-Jul-05 17:13 
GeneralRe: Useful technic Pin
Baber Saeed8-Jul-05 23:51
Baber Saeed8-Jul-05 23:51 
GeneralRe: Useful technic Pin
Keyvan Nayyeri9-Jul-05 1:15
Keyvan Nayyeri9-Jul-05 1:15 
I mean it as this:
In NT Wins we have different kind of real and virtual users those are created by softwares or admins (i.e ASP_NET user or Keyvan user)
They have different access to system. For example Admins can do anything but Guests can view the Web and run applications and use them and ...
Is it any method to retrieve the list of all users with their access to something? (i,e Installing applications)
I don't know if it can be applicable or not but that's good to know Wink | ;)

Keyvan Nayyeri
http://www.nayyeri.net

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.