tags:

views:

51

answers:

1

I am using Win32_NetworkLoginProfile or Win32_Account to get a list of the users for a system. Call to these objects return a lot of users, some local and some belonging to a domain (if the system is part of a domain).

Is there a way to obtain the current user name using WMI? If yes, how?

furthermore, using Win32_Account I can check whether the username I am listing is either local or part of a domain. Is there a way to achieve the same for the current user?

The code is in C for Windows XP or newer.

Thank you.

A: 

Is there a way to obtain the current user name using WMI?

Use the Win32_ComputerSystem.UserName property. It returns the user name along with the domain name, that is, Domain\User. If you want the user name only, simply extract it from this string based on \.

furthermore, using Win32_Account I can check whether the username I am listing is either local or part of a domain. Is there a way to achieve the same for the current user?

Well, you can do something like this:

  • Split the Win32_ComputerSystem.UserName value by the \ character to get the domain name and the user name separately.

  • Obtain the Win32_Account object corresponding to the specified domain and user name. These are the key properties of the Win32_Account class, so instead of running generic SELECT query, use IWbemServices::GetObject to retrieve a specific instance by its path:

    Win32_Account.Domain="<domain>",Name="<username>"
    
  • Check the LocalAccount property of the obtained Win32_Account object.

Helen