Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

How To Recalculate Exchange Address Book Hierarchy


View products that this article applies to.

Summary

After programmatically creating a Microsoft Exchange Recipients container, you must recalculate the address book hierarchy. The Exchange Administrator program uses standard Windows Service Manager calls to send a control code to the directory service. The application programming interface (API) for accessing the Service Control Manager is documented in the Microsoft Developer Network (MSDN) Win32 API documentation. The control code needed by the Exchange Administrator is DS_SERVICE_CONTROL_RECALC_HIERARCHY. The value of this constant is 129.

This article demonstrates how to create a Recipients container and recalculate the address book hierarchy. Failure to recalculate the address book hierarchy will result in the container not being visible in the Exchange Administrator program until the recalculation has occurred. You can force a recalculation to occur by doing a refresh (by pressing F5) in the Exchange Administrator program.

↑ Back to the top


More information

The following Microsoft Visual Basic and Microsoft Visual C++ samples use Active Directory Services Interface (ADSI) to programmatically make a Recipients container in the Exchange Server directory and the Win32 API to send a control to the Exchange directory service requesting a recalculation of the address book hierarchy.

Visual Basic Sample

'Add a reference to ActiveDS Type library.
'Search for TODO in the following code to set configuration 
'information.
'
Private Type SERVICE_STATUS
  dwServiceType As Long
  dwCurrentState As Long
  dwControlsAccepted As Long
  dwWin32ExitCode As Long
  dwServiceSpecificExitCode As Long
  dwCheckPoint As Long
  dwWaitHint As Long
End Type

Private Const STANDARD_RIGHTS_REQUIRED = &HF0000
Private Const SC_MANAGER_CONNECT = &H1
Private Const SC_MANAGER_CREATE_SERVICE = &H2
Private Const SC_MANAGER_ENUMERATE_SERVICE = &H4
Private Const SC_MANAGER_LOCK = &H8
Private Const SC_MANAGER_MODIFY_BOOT_CONFIG = &H20
Private Const SC_MANAGER_QUERY_LOCK_STATUS = &H10
Private Const SC_MANAGER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or _
SC_MANAGER_CONNECT Or SC_MANAGER_CREATE_SERVICE Or _
SC_MANAGER_ENUMERATE_SERVICE Or SC_MANAGER_LOCK Or _
SC_MANAGER_QUERY_LOCK_STATUS Or SC_MANAGER_MODIFY_BOOT_CONFIG)
Private Const SERVICE_USER_DEFINED_CONTROL = &H100

Private Declare Function CloseServiceHandle Lib _
    "advapi32.dll" (ByVal hSCObject As Long) As Long
Private Declare Function OpenSCManager Lib _
    "advapi32.dll" Alias "OpenSCManagerA" (ByVal lpMachineName As String, ByVal lpDatabaseName As _
    String, ByVal dwDesiredAccess As Long) As Long
Private Declare Function OpenService Lib "advapi32.dll" Alias      "OpenServiceA" (ByVal hSCManager As Long, ByVal lpServiceName As       String, ByVal dwDesiredAccess As Long) As Long
Private Declare Function ControlService Lib "advapi32.dll" _
    (ByVal hService As Long, ByVal dwControl As Long, lpServiceStatus      As SERVICE_STATUS) As Long

        
Private Sub Main()
Dim objExistingCont As IADsContainer
Dim objNewCont As IADs
Dim sstStatus As SERVICE_STATUS
Dim hSCM As Long
Dim hService As Long

' Bind to an existing container.

//TODO : Change the ADsPath to reflect your Exchange Organization.
Set objExistingCont =       GetObject("LDAP://MyExchServer/ou=MySite,o=MyOrg")
' Make a new container<BR/>

//TODO: Set the new container directory name.
Set objNewCont = objExistingCont.Create("Container","cn=MyNewCont")
' Put the continer-info prop to tell Exchange this is a recipients cont.
objNewCont.Put "Container-Info", &H80000001

//TODO: Change the next line to set the Display name.
objNewCont.Put "Admin-Display-Name", "MyNewCont"
objNewCont.SetInfo
Set objCont = Nothing
Set objNewCR = Nothing<BR/>

'Now recalculate the address book hierarchy.

//TODO: change first paramater to the name of the Exchange server.
hSCM = OpenSCManager("\\MyExchServer", vbNullString, SC_MANAGER_ALL_ACCESS)
hService = OpenService(hSCM, "MSExchangeDS", SERVICE_USER_DEFINED_CONTROL)
lret = ControlService(hService, 129, sstStatus)
lret = CloseServiceHandle(hService)
lret = CloseServiceHandle(hSCM)
End Sub
				

Visual C++ Sample

//Link with activeds.lib and adsiid.lib.
//Search for TODO in the following code to set configuration 
//information.
// 
#include "activeds.h"
#include "Windows.h"
#define DS_SERVICE_CONTROL_RECALC_HIERARCHY	0x081
#define contType 0x80000001

int main(int argc, char* argv[])
{


HRESULT			hr		=	S_OK;                
IADsContainer	*pCont;
IDispatch		*pDisp	=	NULL;
IDispatch		*pDisp2	=	NULL;
IADs			*pNewCont;
VARIANT			var;
LPOLESTR szAttributeToSet = NULL;
SC_HANDLE		schSCManager,schService;
SERVICE_STATUS	ssStatus; 
//TODO: set this to the name of the Exchange server.
LPSTR			pmachine = "\\\\MyExchangeServer";



// You must initialize COM before calling any ADSI functions or interfaces.
CoInitialize(NULL);
//TODO : Change the ADsPath to reflect your Exchange Organization.
hr = ADsGetObject(L"LDAP://MyExchServer/ou=Mysite,o=MyOrg",
IID_IADsContainer, (void**) &pCont );
   
if ( !SUCCEEDED(hr) )
{
       return 0;
}
//TODO: Set the new container directory name.
hr = pCont->Create( L"container", L"cn=MyNewContainer", &pDisp );        

// Now Query Interface for IADs.
hr = pDisp->QueryInterface( IID_IADs, (void**) &pNewCont);
pDisp->Release();         

//Add Container-Info property to tell Exchange it is a recipients container.

szAttributeToSet = L"Container-Info";
        var.vt = VT_I4;
        var.lVal = contType;
        hr = pNewCont->Put(szAttributeToSet,var);
        VariantClear(&var);

//Add Admin-Display-Name property.
szAttributeToSet = L"Admin-Display-Name";
	var.vt = VT_BSTR;
//TODO: Change the next line to set the Display name
	var.bstrVal = SysAllocString(L"MyNewContainer");
	hr = pNewCont->Put(szAttributeToSet,var);
	VariantClear(&var);

// Now "commit" the Container Creation.
hr = pNewCont -> SetInfo();

pNewCont->Release();
pCont->Release();
CoUninitialize();


// Open a handle to the SC Manager database. 
 
schSCManager = OpenSCManager( 
    pmachine,                // Exchange server 
    NULL,                    // ServicesActive database 
    SC_MANAGER_ALL_ACCESS);  // full access rights 
 
if (schSCManager != NULL) 
{
	// Open a handle to the service. 
    schService = OpenService( 
        schSCManager,       // SCManager database 
        "MSExchangeDS",       // name of service 
        SERVICE_USER_DEFINED_CONTROL);             
 
    if (schService != NULL)  
	// Send a control value to the service. 
		ControlService( 
            schService,   // handle of service 
            DS_SERVICE_CONTROL_RECALC_HIERARCHY,   // control value to send 
            &ssStatus);  // address of status info
}

//cleanup:
if (schSCManager)
	CloseServiceHandle(schSCManager);
if (schService)
	CloseServiceHandle(schService);

return (hr);        

}
				

↑ Back to the top


References

For additional information, click the article number below to view the article in the Microsoft Knowledge Base:
171723 How To Build ADSI Code in Visual C++

↑ Back to the top


Properties

Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.

↑ Back to the top


Keywords: KB261229, kbmsg, kbhowto

↑ Back to the top

Article Info
Article ID : 261229
Revision : 5
Created on : 7/2/2004
Published on : 7/2/2004
Exists online : False
Views : 431