Click here to Skip to main content
13,097,754 members (54,184 online)
Click here to Skip to main content
Add your own
alternative version


14 bookmarked
Posted 5 May 2006

Using WMI to enumerate local user accounts

, 5 May 2006
Rate this:
Please Sign up or sign in to vote.
How to use WMI to enumerate local user accounts.

Sample Image - EnumerateLocalUser.jpg


Sometimes, you want to get information about user accounts in your computer. This article will help you get that information using WMI.

Supported Platforms

Windows Server 2003


Windows XP


Windows 2000

Yes, except for the property LocalAccount

Windows NT 4.0

Yes, except for the property LocalAccount, and with WMI installed.

Windows 98

Yes, except for the property LocalAccount, and with WMI installed

How to use it?

Step 1: Initialize COM:


Step 2: Set COM security levels:

 //Security needs to be initialized in XP first
 // and this was the major problem
 //why it was not working in XP.
 if(CoInitializeSecurity( NULL,
  ) != S_OK)

Step 3: Obtain the initial locator to WMI:

 IWbemLocator * pIWbemLocator = NULL;
 IWbemServices * pWbemServices = NULL;
 IEnumWbemClassObject * pEnumObject  = NULL;

 BSTR bstrNamespace = (L"root\\cimv2");

 if(CoCreateInstance (
    NULL ,
    IID_IUnknown ,
    ( void ** ) & pIWbemLocator
            ) != S_OK)

Step 4: Connect to WMI through the IWbemLocator::ConnectServer method:

     bstrNamespace,  // Namespace
     NULL,          // Userid
     NULL,           // PW
     NULL,           // Locale
     0,              // flags
     NULL,           // Authority
     NULL,           // Context
                ) != S_OK)

Step 5: Use the IWbemServices pointer to make requests of WMI:

 BSTR strQuery = (L"Select * from Win32_UserAccount") + 
                 (L" Where LocalAccount = True");
 BSTR strQL = (L"WQL");
 hRes = pWbemServices->ExecQuery(strQL, strQuery, 
 if(hRes != S_OK)
  MessageBox("Could not execute Query");

Step 6: Get data from the query in step 5.

 ULONG uCount = 1, uReturned;
 IWbemClassObject * pClassObject = NULL;

 hRes = pEnumObject->Reset();

 if(hRes != S_OK)
  MessageBox("Could not Enumerate");

 int nItemIndex = 0;
 while (1)
  hRes = pEnumObject->Next(WBEM_INFINITE, uCount, 
         &pClassObject, &uReturned);
  if(hRes != S_OK)

  for (int i = enmAccountType; i < enmTotal; i ++)
   BSTR strClassProp = SysAllocString(arrStrFields[i]);
   hRes = pClassObject->Get(strClassProp, 0, &v, 0, 0);

   _bstr_t bstrPath = &v;  //Just to convert BSTR to ANSI
   char* strPath=(char*)bstrPath;

   if (SUCCEEDED(hRes))
    if (i == enmAccountType)
     m_lstUsers.InsertItem(nItemIndex, strPath);
     m_lstUsers.SetItemText(nItemIndex, i, strPath);

   VariantClear( &v );
  nItemIndex ++;

Step 7: Free memory.



For more information about the Win32_UserAccount class used in this script, click here.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Le Thanh Cong
Software Developer (Senior)
Vietnam Vietnam
Study at Hanoi University of Technology 2001-2006

You may also be interested in...


Comments and Discussions

GeneralWMI query that distinguish between local user account & local system account Pin
Vinamrata30-Jul-07 19:30
memberVinamrata30-Jul-07 19:30 
GeneralDoes not work! Pin
woj11730-Nov-06 22:40
memberwoj11730-Nov-06 22:40 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170813.1 | Last Updated 5 May 2006
Article Copyright 2006 by Le Thanh Cong
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid