Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Win32_LoggedOnUser

6 views
Skip to first unread message

Chad Wilson (Noobie)

unread,
Feb 6, 2003, 10:40:05 AM2/6/03
to
I am working in VB.NET and I am trying to determine the
current logged on user by there remote computer name. I
was informed that the Win32_LoggedOnUser class would give
me the information I need, but I can not figure out how
to use this class. Would someone be able to give me an
example using this class.

Gwyn Cole

unread,
Feb 6, 2003, 11:56:19 AM2/6/03
to
This is how you would do it in C#:

ManagementClass mc = new ManagementClass("Win32_LoggedOnUser");

ManagementObjectCollection mcCollection = mc.GetInstances();

foreach( ManagementObject mo in mcCollection )

{

Console.WriteLine( "------------------------------------------");

Console.WriteLine( "User '{0}' has logon session '{1}'\n",

mo["Antecedent"], mo["Dependent"]);

ManagementObject user = new ManagementObject(mo["Antecedent"].ToString());

ManagementObject session = new ManagementObject(mo["Dependent"].ToString());


if ((uint)session["LogonType"] == 2)

Console.WriteLine( "Interactive User is '{0}'\n", user["Name"]);

}


--
Gwyn Cole
co-author of "Developing WMI Solutions"


Gwyn Cole

unread,
Feb 6, 2003, 12:20:57 PM2/6/03
to
Okay, here's a VB.NET version of the previous C# sample (my first bit of
VB!):

Dim mc As New ManagementClass("Win32_LoggedOnUser")

Dim mcCollection As ManagementObjectCollection = mc.GetInstances()

Dim mo As ManagementObject

For Each mo In mcCollection

Console.WriteLine("------------------------------------------")

Console.WriteLine("User '" & mo("Antecedent").ToString() & "' has logon
session '" & mo("Dependent").ToString() & "'")

Dim user As New ManagementObject(mo("Antecedent").ToString())

Dim session As New ManagementObject(mo("Dependent").ToString())

Dim logon As Integer = Convert.ToInt32(session("LogonType"))

If logon = 2 Then

Console.WriteLine("Interactive User is '{0}'", user("Name").ToString())

End If

Next mo

Mathieu Paquette

unread,
Feb 6, 2003, 4:54:29 PM2/6/03
to
Or this in WMI :

strComputer = "."

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colComputer = objWMIService.ExecQuery _
("Select * from Win32_ComputerSystem")
For Each objComputer in colComputer
Wscript.Echo objComputer.UserName &vbCrlf &vbCrlf
Next


"Gwyn Cole" <gwyn...@btinternet.com> wrote in message
news:OPstGQgzCHA.1900@TK2MSFTNGP10...

Chad Wilson

unread,
Feb 7, 2003, 8:58:18 AM2/7/03
to
Thank you both for the code samples I will give them a
try.
>.
>

Chad Wilson

unread,
Feb 7, 2003, 10:38:43 AM2/7/03
to
Once again thank you, Gwyn and Mathieu, for the samples.
Both examples worked great. However I am still in the
same predicament in regards to username. Both samples
only get the Username of the local machine. What I am
trying to do is pass my app a computer name and it pass
back the username of the person currently logged onto
that particular PC.

Gwyn, thank you for taking time out to learn enough
VB.NET to help me. I am actually learning C# myself so if
converting to VB.net is too much of a pain I can do the
conversion the other way.

>.
>

Chad Wilson

unread,
Feb 7, 2003, 11:14:50 AM2/7/03
to
Thank you to everyone that helped me out with my problem.
I found the final piece of my puzzle in the
microsoft.public.adsi.general group under my original
post. Basically, I replace the strComputer "." value,
from Mathieu's example, with the remote computer name.
Once again thank you.

Mathieu Paquette

unread,
Feb 7, 2003, 11:48:43 AM2/7/03
to
Bingo. Happy to help ! :-)

Mathieu

"Chad Wilson" <chad....@sertech.com> wrote in message
news:047e01c2cec4$0ab19fc0$3301...@phx.gbl...

0 new messages