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

ID: Q243726


The information in this article applies to:
  • Microsoft Site Server version 3.0
  • Microsoft Site Server version 3.0, Commerce Edition


SUMMARY

You can remove a member by using the following steps:

  1. Create a new project - an empty Win32 Console Application.


  2. Create a new .cpp file and paste in the following code:


  3. 
    #include <comdef.h>
    #include <atlbase.h>
    #include <activeds.h>
    #include <stdio.h>
    #include <conio.h>
    
    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 ();
    }
     
  4. Select the Project Settings.


  5. From the Link tab, add the Adsiid.lib and Activeds.lib items to the Object/library modules.


  6. Compile the code.


Additional query words:

Keywords :
Version : winnt:3.0
Platform : winnt
Issue type : kbhowto


Last Reviewed: November 23, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.