none
Creating Active Directory Users with ADSI? RRS feed

  • Question

  • Now I want to access AD with ADSI .Because I was the first to use this interface and is not very familiar with it ,so I made reference to the following code(http://msdn.microsoft.com/en-us/library/aa746483(v=vs.85).aspx):

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

    I replace "CN=users,DC=fabrikam,DC=com" with "CN=users,DC=Mydomain,DC=com", But I found this code run failed . I can not the username I created. 

    Can you help me? Thank you!

    Saturday, June 8, 2013 8:07 AM

Answers

All replies