Microsoft KB Archive/243726

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

Article ID: 243726

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 Q243726



SUMMARY
You can remove a member by using the following 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 RemoveMember (OLECHAR * strServerName, OLECHAR * strServerPort,                      OLECHAR * strOrganization, OLECHAR * strMemberName) {   HRESULT         hr = NULL; IADsContainer * pIADsContainer; 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 organization 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);

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

// Remove the member printf ("Removing the member.\n"); hr = pIADsContainer->Delete (L"member", strMemberObjectName); if (FAILED (hr)) return (hr);

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

void main (void) {   OLECHAR strServerName [100]; OLECHAR strServerPort [100]; OLECHAR strOrganization [100]; OLECHAR strUserName [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 ("User Name:\t"); gets (buffer); MultiByteToWideChar (CP_ACP, 0, buffer, -1, strUserName, 100);

printf ("Removing member...\n"); hr = RemoveMember (strServerName, strServerPort, strOrganization, strUserName); if (FAILED (hr)) printf ("Failed to remove member. Error = %8X\n", hr); else printf ("Member removed 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 KB243726

-

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

© Microsoft Corporation. All rights reserved.