Creating a User in a Domain and Creating Organizational Units

 


#include "stdafx.h"
#include "activeds.h"

int main(int argc, char* argv[])
{
    HRESULT hr;
    IADsContainer *pCont;
    IDispatch *pDisp=NULL;
    IADs *pUser;

     // Initialize COM before calling any ADSI functions or interfaces.
     CoInitialize(NULL);

    hr = ADsGetObject( L"LDAP://CN=users,DC=fabrikam,DC=com",
                               IID_IADsContainer,
                               (void**) &pCont );
   
    if ( !SUCCEEDED(hr) )
    {
        return 0;
    }

    //-----------------
    // Create a user
    //-----------------
   
    hr = pCont->Create(CComBSTR("user"), CComBSTR("cn=jeffsmith"), &pDisp );
   
    // Release the container object.   
    pCont->Release();
   
    if ( !SUCCEEDED(hr) )
    {
        return 0;
    }

    hr = pDisp->QueryInterface( IID_IADs, (void**) &pUser );

    // Release the dispatch interface.
    pDisp->Release();

    if ( !SUCCEEDED(hr) )
    {   
        return 0;
    }

    // Commit the object data to the directory.
    pUser->SetInfo();

    // Release the object.
    pUser->Release();

    CoUninitialize();
}

 


HRESULT CreateOU( BSTR bstrADsPath, BSTR bstrRelativeName)
{
   
    HRESULT hr = NULL;
   
    IADsContainer* pContainer = NULL;
   
    // Initialize COM.
    CoInitialize(NULL); 
   
    // Bind to the container.
    hr = ADsGetObject (
   
                        bstrADsPath,
                       
                        IID_IADsContainer,
                       
                        (void**) &pContainer;
                       
                      );
   
    if ( SUCCEEDED(hr) )
    {
       
        IDispatch* pDisp = NULL;
       
        IADs* pOrganizationalUnit = NULL;
       
        // Create the new organizational unit.
        hr = pContainer->Create(
       
                                 CComBSTR("organizationalUnit"),
                                
                                 bstrRelativeName,
                                
                                 &pDisp;
                                
                               );
       
        if ( SUCCEEDED(hr) )
       
        {
       
           hr = pDisp->QueryInterface(
                               
                                        IID_IADs,
                                       
                                        (void**) &pOrganizationalUnit;
  
                                     );
          
           if ( SUCCEEDED(hr) )
          
           {
           
                // Commit newly created organizational unit to the
                // directory.
                hr = pOrganizationalUnit->SetInfo();
               
                if ( !SUCCEEDED(hr) )
               
                {
               
                    wprintf(L"Error 0x%x occurred. Container not created.\n", hr);
                                       
                    return hr;
               
                }       
               
            }
           
           
            // Release the IDispatch object.
            pDisp->Release();
           
        }
       
        // Release the organizational unit object.
        pOrganizationalUnit->Release();
   
    }
   
    // Release the container object.
    pContainer->Release();
   
    CoUninitialize();

}

 

 

你可能感兴趣的:(Creating a User in a Domain and Creating Organizational Units)