How to add a user account to the local system by using the DirectoryServices namespace in Visual C++ (306272)



The information in this article applies to:

  • Microsoft Visual C++ .NET (2002)
  • Microsoft Visual C++ .NET (2003)
  • Microsoft Visual C++ 2005 Express Edition

This article was previously published under Q306272

SUMMARY

This article describes how to use the DirectoryServices namespace to add a user account to the local system and to a group.

back to the top

Create the Sample

  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. On the File menu, point to New, and then click Project.
  3. In the New Project dialog box, click Visual C++ Projects under Project Types, and then click Managed C++ Application for Visual Studio .NET 2002 or Console Application (.NET) for Visual Studio .NET 2003 or CLR Console Application for Visual Studio 2005 under Templates.
  4. In Solution Explorer, double-click ProjectName.cpp to open the file in Code view.
  5. Replace the code in the ProjectName.cpp file with the following code:
    #include "stdafx.h"
    
    #using <mscorlib.dll>
    #using <System.dll>
    #using <system.directoryservices.dll>
    
    #include <tchar.h>
    
    using namespace System;
    using namespace System::DirectoryServices;
    
    
    // This is the entry point for this application.
    int _tmain(void)
    {
        try
        {
            //Build the path, and then bind to computer.
            String *adsPath = String::Format(S"WinNT://{0},computer", Environment::MachineName);
            DirectoryEntry *computerEntry = new DirectoryEntry(adsPath);
    
            DirectoryEntry *userEntry = computerEntry->Children->Add(S"TestUser1", S"user");
            userEntry->Properties->get_Item(S"Description")->Add(S"Test user from .NET");
    
            //Set the password, and then commit the changes.
            String *stringArray[] = new String *[1];
            stringArray[0] = S"#12353Abc";
            userEntry->Invoke(S"SetPassword", stringArray);
            userEntry->CommitChanges();
    
            //Add the new user to a group.
            DirectoryEntry *groupEntry = computerEntry->Children->Find(S"Administrators");
            stringArray[0] = userEntry->Path;
            groupEntry->Invoke(S"Add", stringArray);
        }
        catch(Exception *e)
        {
            Console::WriteLine(e);
        }
    		
        return 0;
    } 
    					
    Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile this code sample. To do this, follow these steps:
    1. Click Project, and then click ProjectName Properties.

      Note ProjectName represents the name of the project.
    2. Expand Configuration Properties, and then click General.
    3. Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting on the right pane, click Apply, and then click OK.
    For more information about the common language runtime support compiler options, visit the following Microsoft Web site:

    /clr (Common Language Runtime Compilation)
    http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

  6. Compile and run the project. Notice that a command window opens for approximately one minute and then closes.


back to the top

Verify That the New User Account Is Added to the Local System

Windows 2000

Follow these steps on a Microsoft Windows 2000-based computer to verify that the account is created and is added to the Guest group:
  1. Click Start, point to Programs, point to Administrative Tools, and then click Computer Management.
  2. Expand the Local Users and Groups node. The new account should appear under the Users node and the node for the Guest group.

Windows XP

Follow these steps on a Microsoft Windows XP-based computer to verify that the account is created and is added to the Guest group:
  1. Click Start, and then click Control Panel.
  2. Double-click User Accounts. The new user account should appear in the User Accounts dialog box.
NOTE: Remember to remove the new user account from the system after you complete the testing.

back to the top

Code Explanation

Create a New DirectoryEntry Class

When you create the DirectoryEntry class in this sample, it is assumed that the system is running Microsoft Windows NT, Microsoft Windows 2000, or Microsoft Windows XP. Note that the string that is passed to the DirectoryEntry constructor begins with "WinNT://".
//Build the path, and then bind to computer.
String adsPath = String::Format(S"WinNT://{0},computer", Environment::MachineName);
DirectoryEntry *computerEntry = new DirectoryEntry(adsPath);
				
back to the top

Add the DirectoryEntry to the Directory Tree

The following code adds a DirectoryEntry of type user with the value of TestUser1 to the directory tree:
DirectoryEntry *userEntry = computerEntry->Children->Add(S"TestUser1", S"user");
				
back to the top

Set the Password and the Description for the New User Account

The following code calls the Invoke method to invoke the SetPassword and the Put methods of the DirectoryEntry object:
//Set the password, and then commit the changes.
String *adsPath, *stringArray[] = new String *[1];
stringArray[0] = S"#12353Abc";
userEntry->Invoke(S"SetPassword", stringArray);
userEntry->CommitChanges();
				
This code sets the password and assigns a description to the user account. This code also calls the CommitChanges method to save the changes.

back to the top

Add the Account to a Group

To add the account to a group, follow these steps:
  1. Define a variable of type DirectoryEntry.
  2. Call the Find method of the Children member of the DirectoryEntry class to populate the variable. In this case, the Administrators group is the target of the search.
The following code completes these steps in this sample:
//Add the new user to a group.
DirectoryEntry *groupEntry = computerEntry->Children->Find(S"Administrators");
stringArray[0] = userEntry->Path;
groupEntry->Invoke(S"Add", stringArray);
				
back to the top

Troubleshooting

The code in this article does not work if you try to run the code without the correct permissions to create a user account. For the code to complete successfully, the user who is currently logged on must be a member of the Administrators group or must have specific permissions that allow the user to create user accounts.

back to the top

Modification Type:MajorLast Reviewed:1/18/2006
Keywords:kbDSWADSI2003Swept kbHOWTOmaster KB306272 kbAudDeveloper