Microsoft KB Archive/243725

= How to Add a Member Through ADSI Using Visual C++ =

Article ID: 243725

Article Last Modified on 6/24/2004

-

APPLIES TO


 * Microsoft Site Server 3.0 Standard Edition
 * Microsoft Site Server 3.0 Commerce Edition

-



This article was previously published under Q243725



SUMMARY
You can add a member through ADSI using Visual C++ by following these steps.

 Create a new project, an empty Win32 Console Application.  Create a new .cpp file and paste in the following code:
 * 1) include 
 * 2) include 
 * 3) include 
 * 4) include 
 * 5) include 

HRESULT AddMember (OLECHAR * strServerName, OLECHAR * strServerPort, OLECHAR * strOrganization,           OLECHAR * strMemberName, OLECHAR * strPassword) {   HRESULT     hr = NULL; IADsContainer * pIADsContainer; IADs *     pIADsNewMember; GUID       guid; wchar_t    szGUID[39]; int    cGUIDStrLen; CComBSTR       strPathName; CComBSTR       strMemberObjectName; CComBSTR       strAdminDN;

// Create our connection string strPathName = OLESTR ("LDAP://"); strPathName.Append (strServerName); strPathName.Append (OLESTR (":")); strPathName.Append (strServerPort); strPathName.Append (OLESTR ("/o=")); strPathName.Append (strOrganization); strPathName.Append (OLESTR ("/ou=members"));

// Create our Admin DN based on the organiation name strAdminDN = OLESTR ("cn=Administrator, ou=members, o="); strAdminDN.Append (strOrganization);

// Connect to the LDAP server using an authenticated bind printf ("Binding to LDAP.\n"); hr = ADsOpenObject (strPathName, strAdminDN, L"password", 0, IID_IADsContainer, (void**)&pIADsContainer); if (FAILED (hr)) return (hr);

// Generate a GUID and convert it to a text string printf ("Generating a GUID.\n"); hr = CoCreateGuid (&guid); if (FAILED (hr)) return (hr); cGUIDStrLen = ::StringFromGUID2 (guid, szGUID, 39); if (cGUIDStrLen == 0) return (E_FAIL);

// Prepend the member name with "cn=" strMemberObjectName = OLESTR ("cn="); strMemberObjectName.Append (strMemberName);

// Create the new member printf ("Creating the member.\n"); hr = pIADsContainer->Create (L"member", strMemberObjectName, (IDispatch**)&pIADsNewMember); if (FAILED (hr)) return (hr);

// Set our required attributes printf ("Saving our attributes.\n"); hr = pIADsNewMember->Put (L"GUID", (_variant_t)szGUID); hr = pIADsNewMember->Put (L"userPassword", (_variant_t)strPassword);

// Complete the transaction printf ("Calling SetInfo.\n"); hr = pIADsNewMember->SetInfo ; if (FAILED (hr)) return (hr);

// Clean up and return pIADsNewMember->Release ; pIADsContainer->Release ; return S_OK; }

void main (void) {   OLECHAR strServerName [100]; OLECHAR strServerPort [100]; OLECHAR strOrganization [100]; OLECHAR strMemberName [100]; OLECHAR strPassword [100]; char buffer [100];

CoInitialize (NULL);

HRESULT hr = NULL;

printf ("Server Name:\t"); gets (buffer); MultiByteToWideChar (CP_ACP, 0, buffer, -1, strServerName, 100); printf ("LDAP Port:\t"); gets (buffer); MultiByteToWideChar (CP_ACP, 0, buffer, -1, strServerPort, 100); printf ("Organization:\t"); gets (buffer); MultiByteToWideChar (CP_ACP, 0, buffer, -1, strOrganization, 100); printf ("Member Name:\t"); gets (buffer); MultiByteToWideChar (CP_ACP, 0, buffer, -1, strMemberName, 100); printf ("Password:\t"); gets (buffer); MultiByteToWideChar (CP_ACP, 0, buffer, -1, strPassword, 100);

printf ("\n\nCalling AddMember function...\n"); hr = AddMember (strServerName, strServerPort, strOrganization, strMemberName, strPassword); if (FAILED (hr)) printf ("Failed to create member. Error = %8X\n", hr); else printf ("New member created successfully.\n");

CoUninitialize ; }                    Select the Project Settings. From the Link tab, add the Adsiid.lib and Activeds.lib items to the Object/library modules Compile the code</li></ol>

Keywords: kbhowto KB243725

-

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

© Microsoft Corporation. All rights reserved.