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();

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值