Platform SDK: Active Directory, ADSI, and Directory Services

Example Code for Adding a New Directory Entry

The following example creates LDAPMod structures for a new entry with four initial attributes, then constructs an array of these structures to pass to ldap_add (the asynchronous add function).

#include <winldap.h>
LDAPMod Name;
ULONG CallValue;
//Specify a distinguished name for the entry:
char *entry_dn = "cn = Kate Wheeler, ou = IST, ou = PNS, o = USSA, c = US"
//Note that you can specify multiple values for the cn attribute
char *cn_values[] = { "Kathleen Wheeler" "Kate Wheeler", NULL };
//attributes are Name, USSNum, and Team
Name.mod_op = LDAP_MOD_ADD;
Name.mod_type = "cn";
Name.mod_values = cn_values;. 
LDAPMod USSNum;
char *cis_values[] = { "PN8WKS121284", NULL };
USSNum.mod_op = LDAP_MOD_ADD;
USSNum.mod_type = "cis";
USSNum.mod_values = cis_values;. 
LDAPMod Team;
char *ou_values[] = { "IST", NULL };
Team.mod_op = LDAP_MOD_ADD;
Team.mod_type = "ou";
Team.mod_values = ou_values;
LDAPMod Division;
char *ou_values[] = { "PNS", NULL };
Division.mod_op = LDAP_MOD_ADD;
Division.mod_type = "ou";
Division.mod_values = ou_values;
//now build the array of attributes. 
LDAPMod *NewEntry[5];
LDAPMod Name, USSNum, Team;
NewEntry[0] = &Name;
NewEntry[1] = &USSNum;
NewEntry[2] = &Team;
NewEntry[3] = &Division;
NewEntry[4] = NULL;
//add the entry
CallValue = ldap_add( ld, entry_dn, NewEntry);
//can pass CallValue to ldap_result to check on the 
//status of the asynchronous operation.