Microsoft KB Archive/306272

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

Article ID: 306272

Article Last Modified on 1/18/2006

-

APPLIES TO


 * Microsoft Visual C++ .NET 2002 Standard Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ 2005 Express Edition

-



This article was previously published under Q306272



This article refers to the following Microsoft .NET Framework Class Library namespace:
 * System.DirectoryServices

IN THIS TASK
SUMMARY Create the Sample Verify That the New User Account Is Added to the Local System Code Explanation
 * Create a New DirectoryEntry Class
 * Add the DirectoryEntry to the Directory Tree
 * Set the Password and the Description for the New User Account
 * Add the Account to a Group

Troubleshooting



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
 Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. On the File menu, point to New, and then click Project. 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. In Solution Explorer, double-click  .cpp to open the file in Code view.  Replace the code in the .cpp file with the following code:
 * 1) include &quot;stdafx.h&quot;


 * 1) using 
 * 2) using 
 * 3) using 


 * 1) 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&quot;WinNT://{0},computer&quot;, Environment::MachineName); DirectoryEntry *computerEntry = new DirectoryEntry(adsPath);

DirectoryEntry *userEntry = computerEntry->Children->Add(S&quot;TestUser1&quot;, S&quot;user&quot;); userEntry->Properties->get_Item(S&quot;Description&quot;)->Add(S&quot;Test user from .NET&quot;);

//Set the password, and then commit the changes. String *stringArray[] = new String *[1]; stringArray[0] = S&quot;#12353Abc&quot;; userEntry->Invoke(S&quot;SetPassword&quot;, stringArray); userEntry->CommitChanges;

//Add the new user to a group. DirectoryEntry *groupEntry = computerEntry->Children->Find(S&quot;Administrators&quot;); stringArray[0] = userEntry->Path; groupEntry->Invoke(S&quot;Add&quot;, 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: <ol style="list-style-type: lower-alpha;"> Click Project, and then click ProjectName Properties.

Note ProjectName represents the name of the project.</li> Expand Configuration Properties, and then click General.</li> 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.</li></ol>

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

</li> Compile and run the project. Notice that a command window opens for approximately one minute and then closes.</li></ol>

back to the top

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

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 &quot;WinNT://&quot;. //Build the path, and then bind to computer. String adsPath = String::Format(S&quot;WinNT://{0},computer&quot;, 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&quot;TestUser1&quot;, S&quot;user&quot;); 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&quot;#12353Abc&quot;; userEntry->Invoke(S&quot;SetPassword&quot;, 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&quot;Administrators&quot;); stringArray[0] = userEntry->Path; groupEntry->Invoke(S&quot;Add&quot;, 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

Keywords: kbhowtomaster kbdswadsi2003swept KB306272

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.