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 topCreate 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 ProjectName.cpp to open the file in Code view.
- 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:
- Click Project, and then click ProjectName Properties.
Note ProjectName represents the name of the project. - Expand Configuration Properties, and then click General.
- 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: - Compile and run the project. Notice that a command window
opens for approximately one minute and then closes.
back to the topVerify 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:
- Click Start, point to Programs, point to Administrative Tools, and then click Computer Management.
- 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:
- Click Start, and then click Control Panel.
- 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 topCode 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 topAdd 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 topSet 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 topAdd the Account to a Group
To add the account to a group, follow these steps:
- Define a variable of type DirectoryEntry.
- 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 topTroubleshooting
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